all repos — homestead @ 8efa0c0335cf673ce530785591f22d5f5ab5259d

Code for my website

enable sniffing content-type of files without extension

Alan Pearce
commit

8efa0c0335cf673ce530785591f22d5f5ab5259d

parent

8cf5dc9b43a926883465b6c5b21a4503fb28eb32

1 file changed, 11 insertions(+), 1 deletion(-)

changed files
M shared/storage/files/file.goshared/storage/files/file.go
@@ -6,6 +6,7 @@ "fmt"
"hash/fnv" "io" "mime" + "net/http" "os" "path/filepath" "strings"
@@ -46,7 +47,7 @@
file := &storage.File{ Path: path, FSPath: filename, - ContentType: mime.TypeByExtension(filepath.Ext(filename)), + ContentType: getContentType(filename, buf), LastModified: stat.ModTime(), Etag: etag, Encodings: map[string]*buffer.Buffer{
@@ -114,3 +115,12 @@ cutSuffix(filename, "index.html"),
".html", ) } + +func getContentType(filename string, buf *buffer.Buffer) string { + ext := filepath.Ext(filename) + if ext == "" { + return http.DetectContentType(buf.FirstBlock()) + } + + return mime.TypeByExtension(ext) +}