diff options
author | Florian Engelhardt <flo@dotbox.org> | 2020-09-25 09:58:04 +0300 |
---|---|---|
committer | Florian Engelhardt <flo@dotbox.org> | 2020-10-20 14:10:46 +0300 |
commit | dfbdb2790a5db95cd4ad3f8754aca8c664a35d3a (patch) | |
tree | 2ec191f07a5b2bed3c3fc5158e426f4743d236da | |
parent | bb6ede83449725e0240f5c59efa73ed3acf7e104 (diff) |
PATCH-44819 Add mimetype the official way
-rw-r--r-- | internal/serving/disk/helpers.go | 9 | ||||
-rw-r--r-- | internal/serving/disk/serving.go | 17 |
2 files changed, 18 insertions, 8 deletions
diff --git a/internal/serving/disk/helpers.go b/internal/serving/disk/helpers.go index 90057da9..5456724a 100644 --- a/internal/serving/disk/helpers.go +++ b/internal/serving/disk/helpers.go @@ -39,8 +39,7 @@ func endsWithoutHTMLExtension(path string) bool { // Implementation is adapted from Golang's `http.serveContent()` // See https://github.com/golang/go/blob/902fc114272978a40d2e65c2510a18e870077559/src/net/http/fs.go#L194 func (reader *Reader) detectContentType(ctx context.Context, root vfs.Root, path string) (string, error) { - fileExt := filepath.Ext(path) - contentType := mime.TypeByExtension(fileExt) + contentType := mime.TypeByExtension(filepath.Ext(path)) if contentType == "" { var buf [512]byte @@ -58,12 +57,6 @@ func (reader *Reader) detectContentType(ctx context.Context, root vfs.Root, path contentType = http.DetectContentType(buf[:n]) } - // see https://gitlab.com/gitlab-org/gitlab-pages/-/issues/460 - // packages mime and http currently do not know about the avif file format - if contentType == "application/octet-stream" && fileExt == ".avif" { - contentType = "image/avif" - } - return contentType, nil } diff --git a/internal/serving/disk/serving.go b/internal/serving/disk/serving.go index 11b1689e..c0f5417d 100644 --- a/internal/serving/disk/serving.go +++ b/internal/serving/disk/serving.go @@ -1,8 +1,11 @@ package disk import ( + "mime" "os" + "gitlab.com/gitlab-org/labkit/log" + "gitlab.com/gitlab-org/gitlab-pages/internal/httperrors" "gitlab.com/gitlab-org/gitlab-pages/internal/serving" "gitlab.com/gitlab-org/gitlab-pages/internal/vfs" @@ -43,6 +46,8 @@ func (s *Disk) ServeNotFoundHTTP(h serving.Handler) { // New returns a serving instance that is capable of reading files // from the VFS func New(vfs vfs.VFS) serving.Serving { + addExtraMIMETypes(extraMIMETypes) + return &Disk{ reader: Reader{ fileSizeMetric: metrics.DiskServingFileSize, @@ -50,3 +55,15 @@ func New(vfs vfs.VFS) serving.Serving { }, } } + +var extraMIMETypes = map[string]string{ + ".avif": "image/avif", +} + +func addExtraMIMETypes(mimeTypes map[string]string) { + for ext, mimeType := range mimeTypes { + if err := mime.AddExtensionType(ext, mimeType); err != nil { + log.WithError(err).Errorf("failed to add extension: %q with MIME type: %q", ext, mimeType) + } + } +} |