all repos — homestead @ 65039b065ba634b9c4b4c7f4b42ebccdbfd40ce0

Code for my website

remove buffers in file writer

Alan Pearce
commit

65039b065ba634b9c4b4c7f4b42ebccdbfd40ce0

parent

f7d8beffaa09ecf863996d29c72f508eb3952c84

1 file changed, 24 insertions(+), 9 deletions(-)

changed files
M shared/storage/files/writer.goshared/storage/files/writer.go
@@ -13,7 +13,6 @@ "github.com/andybalholm/brotli"
"github.com/klauspost/compress/zstd" "alin.ovh/homestead/domain/content" - "alin.ovh/homestead/shared/buffer" "alin.ovh/homestead/shared/file" "alin.ovh/homestead/shared/storage" )
@@ -54,8 +53,8 @@ func (f *Files) OpenWrite(filename string) (io.WriteCloser, error) {
return openFileWrite(f.join(filename)) } -func (f *Files) WritePost(post *content.Post, content *buffer.Buffer) error { - fd, err := f.write(post.URL, content) +func (f *Files) WritePost(post *content.Post, wt io.WriterTo) error { + fd, err := f.write(post.URL, wt) if err != nil { return err }
@@ -76,8 +75,8 @@
return nil } -func (f *Files) Write(pathname string, _ string, content *buffer.Buffer) error { - fd, err := f.write(pathname, content) +func (f *Files) Write(pathname string, _ string, wt io.WriterTo) error { + fd, err := f.write(pathname, wt) if err != nil { return err }
@@ -97,8 +96,21 @@ Encodings: map[string]*os.File{},
} } -func (f *Files) WriteFile(file *storage.File, content *buffer.Buffer) error { - return f.Write(file.Path, file.Title, content) +func (f *Files) CopyFile(file *storage.File, src *os.File) error { + dst, err := f.write(file.Path, src) + if err != nil { + return err + } + + if err := dst.Close(); err != nil { + return fault.Wrap(err) + } + + return nil +} + +func (f *Files) WriteFile(file *storage.File, wt io.WriterTo) error { + return f.Write(file.Path, file.Title, wt) } func (f *Files) OpenFileAndVariants(filename string) (file.FileLike, error) {
@@ -118,7 +130,10 @@
return nil } -func (f *Files) write(pathname string, content *buffer.Buffer) (file.FileLike, error) { +func (f *Files) write( + pathname string, + writer io.WriterTo, +) (file.FileLike, error) { filename := pathNameToFileName(pathname) err := f.Mkdirp(filepath.Dir(filename)) if err != nil {
@@ -130,7 +145,7 @@ if err != nil {
return nil, fault.Wrap(err, fmsg.With("could not open output file")) } - if _, err := fd.Write(content.Bytes()); err != nil { + if _, err := writer.WriteTo(fd); err != nil { return nil, fault.Wrap(err, fmsg.With("could not write output file")) }