|
| 1 | +package s3store |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "strconv" |
| 9 | + |
| 10 | + "github.com/tus/tusd/v2/pkg/handler" |
| 11 | + |
| 12 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 13 | + awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http" |
| 14 | + "github.com/aws/aws-sdk-go-v2/service/s3" |
| 15 | +) |
| 16 | + |
| 17 | +func (store S3Store) AsServableUpload(upload handler.Upload) handler.ServableUpload { |
| 18 | + return upload.(*s3Upload) |
| 19 | +} |
| 20 | + |
| 21 | +func (upload *s3Upload) ServeContent(ctx context.Context, w http.ResponseWriter, r *http.Request) error { |
| 22 | + input := &s3.GetObjectInput{ |
| 23 | + Bucket: aws.String(upload.store.Bucket), |
| 24 | + Key: upload.store.keyWithPrefix(upload.objectId), |
| 25 | + } |
| 26 | + |
| 27 | + // Forward the Range, If-Match, If-None-Match, If-Unmodified-Since, If-Modified-Since headers if present |
| 28 | + if val := r.Header.Get("Range"); val != "" { |
| 29 | + input.Range = aws.String(val) |
| 30 | + } |
| 31 | + if val := r.Header.Get("If-Match"); val != "" { |
| 32 | + input.IfMatch = aws.String(val) |
| 33 | + } |
| 34 | + if val := r.Header.Get("If-None-Match"); val != "" { |
| 35 | + input.IfNoneMatch = aws.String(val) |
| 36 | + } |
| 37 | + if val := r.Header.Get("If-Modified-Since"); val != "" { |
| 38 | + t, err := http.ParseTime(val) |
| 39 | + if err == nil { |
| 40 | + input.IfModifiedSince = aws.Time(t) |
| 41 | + } |
| 42 | + } |
| 43 | + if val := r.Header.Get("If-Unmodified-Since"); val != "" { |
| 44 | + t, err := http.ParseTime(val) |
| 45 | + if err == nil { |
| 46 | + input.IfUnmodifiedSince = aws.Time(t) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // Let S3 handle the request |
| 51 | + result, err := upload.store.Service.GetObject(ctx, input) |
| 52 | + if err != nil { |
| 53 | + // Delete the headers set by tusd's handler. We don't need them for errors. |
| 54 | + w.Header().Del("Content-Type") |
| 55 | + w.Header().Del("Content-Disposition") |
| 56 | + |
| 57 | + var respErr *awshttp.ResponseError |
| 58 | + if errors.As(err, &respErr) { |
| 59 | + if respErr.HTTPStatusCode() == http.StatusNotFound || respErr.HTTPStatusCode() == http.StatusForbidden { |
| 60 | + // If the object cannot be found, it means that the upload is not yet complete and we cannot serve it. |
| 61 | + // At this stage it is not possible that the upload itself does not exist, because the handler |
| 62 | + // alredy checked this case. Therefore, we can safely assume that the upload is still in progress. |
| 63 | + return errIncompleteUpload |
| 64 | + } |
| 65 | + |
| 66 | + if respErr.HTTPStatusCode() == http.StatusNotModified { |
| 67 | + // Content-Location, Date, ETag, Vary, Cache-Control and Expires should be set |
| 68 | + // for 304 Not Modified responses. See https://httpwg.org/specs/rfc9110.html#status.304 |
| 69 | + for _, header := range []string{"Content-Location", "Date", "ETag", "Vary", "Cache-Control", "Expires"} { |
| 70 | + if val := respErr.Response.Header.Get(header); val != "" { |
| 71 | + w.Header().Set(header, val) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + w.WriteHeader(http.StatusNotModified) |
| 76 | + return nil |
| 77 | + } |
| 78 | + |
| 79 | + if respErr.HTTPStatusCode() == http.StatusRequestedRangeNotSatisfiable { |
| 80 | + // Content-Range should be set for 416 Request Range Not Satisfiable responses. |
| 81 | + // See https://httpwg.org/specs/rfc9110.html#status.304 |
| 82 | + // Note: AWS S3 does not seem to include this header in its response. |
| 83 | + if val := respErr.Response.Header.Get("Content-Range"); val != "" { |
| 84 | + w.Header().Set("Content-Range", val) |
| 85 | + } |
| 86 | + |
| 87 | + w.WriteHeader(http.StatusRequestedRangeNotSatisfiable) |
| 88 | + return nil |
| 89 | + } |
| 90 | + } |
| 91 | + return err |
| 92 | + } |
| 93 | + defer result.Body.Close() |
| 94 | + |
| 95 | + // Add Accept-Ranges,Content-*, Cache-Control, ETag, Expires, Last-Modified headers if present in S3 response |
| 96 | + if result.AcceptRanges != nil { |
| 97 | + w.Header().Set("Accept-Ranges", *result.AcceptRanges) |
| 98 | + } |
| 99 | + if result.ContentDisposition != nil { |
| 100 | + w.Header().Set("Content-Disposition", *result.ContentDisposition) |
| 101 | + } |
| 102 | + if result.ContentEncoding != nil { |
| 103 | + w.Header().Set("Content-Encoding", *result.ContentEncoding) |
| 104 | + } |
| 105 | + if result.ContentLanguage != nil { |
| 106 | + w.Header().Set("Content-Language", *result.ContentLanguage) |
| 107 | + } |
| 108 | + if result.ContentLength != nil { |
| 109 | + w.Header().Set("Content-Length", strconv.FormatInt(*result.ContentLength, 10)) |
| 110 | + } |
| 111 | + if result.ContentRange != nil { |
| 112 | + w.Header().Set("Content-Range", *result.ContentRange) |
| 113 | + } |
| 114 | + if result.ContentType != nil { |
| 115 | + w.Header().Set("Content-Type", *result.ContentType) |
| 116 | + } |
| 117 | + if result.CacheControl != nil { |
| 118 | + w.Header().Set("Cache-Control", *result.CacheControl) |
| 119 | + } |
| 120 | + if result.ETag != nil { |
| 121 | + w.Header().Set("ETag", *result.ETag) |
| 122 | + } |
| 123 | + if result.ExpiresString != nil { |
| 124 | + w.Header().Set("Expires", *result.ExpiresString) |
| 125 | + } |
| 126 | + if result.LastModified != nil { |
| 127 | + w.Header().Set("Last-Modified", result.LastModified.Format(http.TimeFormat)) |
| 128 | + } |
| 129 | + |
| 130 | + statusCode := http.StatusOK |
| 131 | + if result.ContentRange != nil { |
| 132 | + // Use 206 Partial Content for range requests |
| 133 | + statusCode = http.StatusPartialContent |
| 134 | + } else if result.ContentLength != nil && *result.ContentLength == 0 { |
| 135 | + statusCode = http.StatusNoContent |
| 136 | + } |
| 137 | + w.WriteHeader(statusCode) |
| 138 | + |
| 139 | + _, err = io.Copy(w, result.Body) |
| 140 | + return err |
| 141 | +} |
0 commit comments