all repos — homestead @ 8efa0c0335cf673ce530785591f22d5f5ab5259d

Code for my website

enable sniffing content-type of files without extension

Alan Pearce
commit

8efa0c0335cf673ce530785591f22d5f5ab5259d

parent

8cf5dc9b43a926883465b6c5b21a4503fb28eb32

2 files changed, 17 insertions(+), 1 deletion(-)

changed files
M shared/buffer/buffer.goshared/buffer/buffer.go
@@ -10,6 +10,8 @@ pos int
len int } +const blockSize = 512 + func NewBuffer(buf []byte) *Buffer { return &Buffer{ buf: buf,
@@ -100,3 +102,7 @@
func (b *Buffer) Bytes() []byte { return b.buf[:b.len] } + +func (b *Buffer) FirstBlock() []byte { + return b.buf[:min(b.len, blockSize)] +}
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) +}