all repos — homestead @ f7d8beffaa09ecf863996d29c72f508eb3952c84

Code for my website

use files instead of buffers for serving

Alan Pearce
commit

f7d8beffaa09ecf863996d29c72f508eb3952c84

parent

40f8ccfdf0c8a5855cdc211096f86459b28a261b

M domain/content/builder/builder.godomain/content/builder/builder.go
@@ -158,7 +158,6 @@ }
file := &storage.File{ Title: title, LastModified: matchingPosts[0].Date, - ContentType: "application/xml", Path: path.Join("/tags", tag, "atom.xml"), FSPath: path.Join("/tags", tag, "atom.xml"), }
@@ -191,7 +190,6 @@ }
file := &storage.File{ Title: config.Title, LastModified: cc.Posts[0].Date, - ContentType: "application/xml", Path: "/atom.xml", FSPath: "/atom.xml", }
@@ -237,7 +235,6 @@ return fault.Wrap(err)
} } file := stor.NewFileFromPost(post) - file.ContentType = "text/html; charset=utf-8" if err := stor.WriteFile(file, buf); err != nil { return fault.Wrap(err) }
M domain/web/mux.godomain/web/mux.go
@@ -68,8 +68,10 @@ enc := nego.NegotiateContentEncoding(r, file.AvailableEncodings()...)
if enc != "" { w.Header().Add("Content-Encoding", enc) } - w.Header().Add("Content-Type", file.ContentType) - if file.ContentType == "application/xml" { + mime := file.GetContentType() + w.Header().Add("Content-Type", mime) + + if mime == "application/xml" { for k, v := range feedHeaders { w.Header().Add(k, v) }
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 +}
M shared/storage/files/file.goshared/storage/files/file.go
@@ -5,16 +5,12 @@ "errors"
"fmt" "hash/fnv" "io" - "mime" - "net/http" "os" - "path/filepath" "strings" "github.com/Southclaws/fault" "github.com/Southclaws/fault/fmsg" - "alin.ovh/homestead/shared/buffer" "alin.ovh/homestead/shared/storage" )
@@ -29,18 +25,12 @@ f, err := os.Open(filename)
if err != nil { return nil, fault.Wrap(err, fmsg.With("could not open file for reading")) } - defer f.Close() stat, err := f.Stat() if err != nil { return nil, fault.Wrap(err, fmsg.With("could not stat file")) } - buf := new(buffer.Buffer) - if _, err := f.WriteTo(buf); err != nil { - return nil, fault.Wrap(err, fmsg.With("could not read file")) - } - - etag, err := etag(buf) + etag, err := etag(f) if err != nil { return nil, fault.Wrap(err, fmsg.With("could not calculate etag")) }
@@ -48,11 +38,10 @@
file := &storage.File{ Path: path, FSPath: filename, - ContentType: getContentType(filename, buf), LastModified: stat.ModTime(), Etag: etag, - Encodings: map[string]*buffer.Buffer{ - "identity": buf, + Encodings: map[string]*os.File{ + "identity": f, }, }
@@ -68,15 +57,14 @@ err,
fmsg.With(fmt.Sprintf("could not stat file %s", filename+suffix)), ) } - bytes, err := os.ReadFile(filename + suffix) + f, err := os.Open(filename + suffix) if err != nil { return nil, fault.Wrap( err, fmsg.With(fmt.Sprintf("could not read file %s", filename+suffix)), ) } - buf := buffer.NewBuffer(bytes) - file.Encodings[enc] = buf + file.Encodings[enc] = f } return file, nil
@@ -111,12 +99,3 @@ 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) -}
M shared/storage/files/writer.goshared/storage/files/writer.go
@@ -93,7 +93,7 @@ func (f *Files) NewFileFromPost(post *content.Post) *storage.File {
return &storage.File{ Path: post.URL, FSPath: pathNameToFileName(post.URL), - Encodings: map[string]*buffer.Buffer{}, + Encodings: map[string]*os.File{}, } }