diff --git a/modules/git/blob.go b/modules/git/blob.go
index 14ca2b1445..4eef5f0e2a 100644
--- a/modules/git/blob.go
+++ b/modules/git/blob.go
@@ -220,7 +220,7 @@ func (b *Blob) GuessContentType() (typesniffer.SniffedType, error) {
}
defer r.Close()
- return typesniffer.DetectContentTypeFromReader(r)
+ return typesniffer.DetectContentTypeFromReader(r, b.Name())
}
// GetBlob finds the blob object in the repository.
diff --git a/modules/httplib/serve.go b/modules/httplib/serve.go
index c5f0658d4e..d385ac21c9 100644
--- a/modules/httplib/serve.go
+++ b/modules/httplib/serve.go
@@ -99,7 +99,7 @@ func setServeHeadersByFile(r *http.Request, w http.ResponseWriter, filePath stri
Filename: path.Base(filePath),
}
- sniffedType := typesniffer.DetectContentType(mineBuf)
+ sniffedType := typesniffer.DetectContentType(mineBuf, opts.Filename)
// the "render" parameter came from year 2016: 638dd24c, it doesn't have clear meaning, so I think it could be removed later
isPlain := sniffedType.IsText() || r.FormValue("render") != ""
diff --git a/modules/indexer/code/bleve/bleve.go b/modules/indexer/code/bleve/bleve.go
index c53b7a2e6d..4c8b5f2a86 100644
--- a/modules/indexer/code/bleve/bleve.go
+++ b/modules/indexer/code/bleve/bleve.go
@@ -177,7 +177,7 @@ func (b *Indexer) addUpdate(ctx context.Context, batchWriter git.WriteCloserErro
fileContents, err := io.ReadAll(io.LimitReader(batchReader, size))
if err != nil {
return err
- } else if !typesniffer.DetectContentType(fileContents).IsText() {
+ } else if !typesniffer.DetectContentType(fileContents, update.Filename).IsText() {
// FIXME: UTF-16 files will probably fail here
// Even if the file is not recognized as a "text file", we could still put its name into the indexers to make the filename become searchable, while leave the content to empty.
fileContents = nil
diff --git a/modules/indexer/code/elasticsearch/elasticsearch.go b/modules/indexer/code/elasticsearch/elasticsearch.go
index 3903d77fe0..9b11f56fb7 100644
--- a/modules/indexer/code/elasticsearch/elasticsearch.go
+++ b/modules/indexer/code/elasticsearch/elasticsearch.go
@@ -144,7 +144,7 @@ func (b *Indexer) addUpdate(ctx context.Context, batchWriter git.WriteCloserErro
fileContents, err := io.ReadAll(io.LimitReader(batchReader, size))
if err != nil {
return nil, err
- } else if !typesniffer.DetectContentType(fileContents).IsText() {
+ } else if !typesniffer.DetectContentType(fileContents, update.Filename).IsText() {
// FIXME: UTF-16 files will probably fail here
return nil, nil
}
diff --git a/modules/typesniffer/typesniffer.go b/modules/typesniffer/typesniffer.go
index 262feb2b05..8cb1513a88 100644
--- a/modules/typesniffer/typesniffer.go
+++ b/modules/typesniffer/typesniffer.go
@@ -124,7 +124,7 @@ func (ct SniffedType) GetMimeType() string {
}
// DetectContentType extends http.DetectContentType with more content types. Defaults to text/unknown if input is empty.
-func DetectContentType(data []byte) SniffedType {
+func DetectContentType(data []byte, filename string) SniffedType {
if len(data) == 0 {
return SniffedType{"text/unknown"}
}
@@ -176,6 +176,13 @@ func DetectContentType(data []byte) SniffedType {
}
}
+ if ct == "application/octet-stream" &&
+ filename != "" &&
+ !strings.HasSuffix(strings.ToUpper(filename), ".LCOM") &&
+ bytes.Contains(data, []byte("(DEFINE-FILE-INFO ")) {
+ ct = "text/vnd.interlisp"
+ }
+
// GLTF is unsupported by http.DetectContentType
// hexdump -n 4 -C glTF.glb
if bytes.HasPrefix(data, []byte("glTF")) {
@@ -186,7 +193,7 @@ func DetectContentType(data []byte) SniffedType {
}
// DetectContentTypeFromReader guesses the content type contained in the reader.
-func DetectContentTypeFromReader(r io.Reader) (SniffedType, error) {
+func DetectContentTypeFromReader(r io.Reader, filename string) (SniffedType, error) {
buf := make([]byte, sniffLen)
n, err := util.ReadAtMost(r, buf)
if err != nil {
@@ -194,5 +201,5 @@ func DetectContentTypeFromReader(r io.Reader) (SniffedType, error) {
}
buf = buf[:n]
- return DetectContentType(buf), nil
+ return DetectContentType(buf, filename), nil
}
diff --git a/modules/typesniffer/typesniffer_test.go b/modules/typesniffer/typesniffer_test.go
index 176d3658bb..d2b7ed4f21 100644
--- a/modules/typesniffer/typesniffer_test.go
+++ b/modules/typesniffer/typesniffer_test.go
@@ -16,63 +16,63 @@ import (
func TestDetectContentTypeLongerThanSniffLen(t *testing.T) {
// Pre-condition: Shorter than sniffLen detects SVG.
- assert.Equal(t, "image/svg+xml", DetectContentType([]byte(``)).contentType)
+ assert.Equal(t, "image/svg+xml", DetectContentType([]byte(``), "").contentType)
// Longer than sniffLen detects something else.
- assert.NotEqual(t, "image/svg+xml", DetectContentType([]byte(``)).contentType)
+ assert.NotEqual(t, "image/svg+xml", DetectContentType([]byte(``), "").contentType)
}
func TestIsTextFile(t *testing.T) {
- assert.True(t, DetectContentType([]byte{}).IsText())
- assert.True(t, DetectContentType([]byte("lorem ipsum")).IsText())
+ assert.True(t, DetectContentType([]byte{}, "").IsText())
+ assert.True(t, DetectContentType([]byte("lorem ipsum"), "").IsText())
}
func TestIsSvgImage(t *testing.T) {
- assert.True(t, DetectContentType([]byte("")).IsSvgImage())
- assert.True(t, DetectContentType([]byte(" ")).IsSvgImage())
- assert.True(t, DetectContentType([]byte(``)).IsSvgImage())
- assert.True(t, DetectContentType([]byte(``)).IsSvgImage())
+ assert.True(t, DetectContentType([]byte(""), "").IsSvgImage())
+ assert.True(t, DetectContentType([]byte(" "), "").IsSvgImage())
+ assert.True(t, DetectContentType([]byte(``), "").IsSvgImage())
+ assert.True(t, DetectContentType([]byte(``), "").IsSvgImage())
assert.True(t, DetectContentType([]byte(`
- `)).IsSvgImage())
+ `), "").IsSvgImage())
assert.True(t, DetectContentType([]byte(`
- `)).IsSvgImage())
+ `), "").IsSvgImage())
assert.True(t, DetectContentType([]byte(`
- `)).IsSvgImage())
+ `), "").IsSvgImage())
assert.True(t, DetectContentType([]byte(`
- `)).IsSvgImage())
+ `), "").IsSvgImage())
assert.True(t, DetectContentType([]byte(`
- `)).IsSvgImage())
+ `), "").IsSvgImage())
assert.True(t, DetectContentType([]byte(`
- `)).IsSvgImage())
+ `), "").IsSvgImage())
assert.True(t, DetectContentType([]byte(`
- `)).IsSvgImage())
+ `), "").IsSvgImage())
assert.True(t, DetectContentType([]byte(`
- `)).IsSvgImage())
+ `), "").IsSvgImage())
// the DetectContentType should work for incomplete data, because only beginning bytes are used for detection
- assert.True(t, DetectContentType([]byte(`