remove buffers in file writer
1 file changed, 37 insertions(+), 18 deletions(-)
changed files
M shared/storage/file.go → shared/storage/file.go
@@ -1,9 +1,8 @@ package storage import ( - "io" + "errors" "mime" - "net/http" "os" "path/filepath" "time"@@ -17,7 +16,38 @@ Etag string Title string Encodings map[string]*os.File - contentType string + ContentType string +} + +var ( + ErrEncodingNotFound = errors.New("encoding not found") +) + +func NewFile(urlPath string, filename string) (*File, error) { + f, err := os.Open(filename) + if err != nil { + return nil, err + } + + stat, err := os.Stat(filename) + if err != nil { + return nil, err + } + + file := &File{ + Path: urlPath, + FSPath: filename, + LastModified: stat.ModTime(), + Etag: "", + Title: filename, + Encodings: map[string]*os.File{ + "identity": f, + }, + } + + file.ContentType = file.getContentType() + + return file, nil } func (f *File) AvailableEncodings() []string {@@ -29,23 +59,12 @@ return encs } -func (f *File) GetContentType() string { - if f.contentType != "" { - return f.contentType - } - +func (f *File) getContentType() string { 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) + ext = ".html" } + f.ContentType = mime.TypeByExtension(ext) - return f.contentType + return f.ContentType }