all repos — homestead @ f7d8beffaa09ecf863996d29c72f508eb3952c84

Code for my website

use files instead of buffers for serving

Alan Pearce
commit

f7d8beffaa09ecf863996d29c72f508eb3952c84

parent

40f8ccfdf0c8a5855cdc211096f86459b28a261b

1 file changed, 29 insertions(+), 4 deletions(-)

changed files
M shared/storage/file.goshared/storage/file.go
@@ -1,19 +1,23 @@
package storage import ( + "io" + "mime" + "net/http" + "os" + "path/filepath" "time" - - "alin.ovh/homestead/shared/buffer" ) type File struct { Path string FSPath string - ContentType string LastModified time.Time Etag string Title string - Encodings map[string]*buffer.Buffer + Encodings map[string]*os.File + + contentType string } func (f *File) AvailableEncodings() []string {
@@ -24,3 +28,24 @@ }
return encs } + +func (f *File) GetContentType() string { + if f.contentType != "" { + return f.contentType + } + + ext := filepath.Ext(f.FSPath) + if ext == "" { + scent := make([]byte, 0, 512) + _, err := f.Encodings["identity"].Read(scent) + if err != nil && err != io.EOF { + return "" + } + + f.contentType = http.DetectContentType(scent) + } else { + f.contentType = mime.TypeByExtension(ext) + } + + return f.contentType +}