Skip to content

Fix some mp3/mp4 media files show as Raw #18458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 17 commits into from
Closed
6 changes: 5 additions & 1 deletion modules/setting/mime_type_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

package setting

import "strings"
import (
"mime"
"strings"
)

// MimeTypeMap defines custom mime type mapping settings
var MimeTypeMap = struct {
Expand All @@ -21,6 +24,7 @@ func newMimeTypeMap() {
m := make(map[string]string, len(keys))
for _, key := range keys {
m[strings.ToLower(key.Name())] = key.Value()
_ = mime.AddExtensionType(key.Name(), key.Value())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should ignore a error here.

}
MimeTypeMap.Map = m
if len(keys) > 0 {
Expand Down
22 changes: 22 additions & 0 deletions modules/typesniffer/typesniffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ package typesniffer
import (
"fmt"
"io"
"mime"
"net/http"
"path/filepath"
"regexp"
"strings"

Expand Down Expand Up @@ -87,6 +89,26 @@ func DetectContentType(data []byte) SniffedType {
return SniffedType{ct}
}

func DetectContentTypeExtFirst(name string, readSeekerOrBytes interface{}) (SniffedType, error) {
// we can merge `setting.MimeTypeMap` by mime.AddExtensionType()
ct := mime.TypeByExtension(filepath.Ext(name))
if ct != "" && !strings.Contains(ct, "text/") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to change Contains -> HasPrefix?

return SniffedType{ct}, nil
}
if r, ok := readSeekerOrBytes.(io.ReadSeeker); ok {
st, err := DetectContentTypeFromReader(r)
if nil != err {
return SniffedType{}, err
}
_, err = r.Seek(0, io.SeekStart)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is Seek needed?

if nil != err {
return SniffedType{}, err
}
return st, nil
}
return DetectContentType(readSeekerOrBytes.([]byte)), nil
}

// DetectContentTypeFromReader guesses the content type contained in the reader.
func DetectContentTypeFromReader(r io.Reader) (SniffedType, error) {
buf := make([]byte, sniffLen)
Expand Down
1 change: 1 addition & 0 deletions routers/common/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func ServeData(ctx *context.Context, name string, size int64, reader io.Reader)
// Google Chrome dislike commas in filenames, so let's change it to a space
name = strings.ReplaceAll(name, ",", " ")

// st, err := typesniffer.DetectContentTypeExtFirst(name, buf)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented code is confusing and we shouldn't have it. If you want to retain it for future reference, it's better to add a sentence or two about it.

st := typesniffer.DetectContentType(buf)

mappedMimeType := ""
Expand Down
2 changes: 1 addition & 1 deletion routers/web/repo/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
}
buf = buf[:n]

st = typesniffer.DetectContentType(buf)
st, _ = typesniffer.DetectContentTypeExtFirst(blob.Name(), buf)
isTextFile = st.IsText()

fileSize = meta.Size
Expand Down