about summary refs log tree commit diff stats
path: root/internal/storage/files/file.go
diff options
context:
space:
mode:
authorAlan Pearce2025-01-29 22:00:45 +0100
committerAlan Pearce2025-01-29 23:27:45 +0100
commit3162ceaa0f7997742f8c2fce1c9660e8e86ad5bb (patch)
tree14c424b8ef8b9238e69393cd7da1d0af6833526e /internal/storage/files/file.go
parenta93b5ad88ea3cf742cf03fdeeb95f63865f08374 (diff)
downloadwebsite-3162ceaa0f7997742f8c2fce1c9660e8e86ad5bb.tar.lz
website-3162ceaa0f7997742f8c2fce1c9660e8e86ad5bb.tar.zst
website-3162ceaa0f7997742f8c2fce1c9660e8e86ad5bb.zip
use buffers as interface to storage
Diffstat (limited to 'internal/storage/files/file.go')
-rw-r--r--internal/storage/files/file.go45
1 files changed, 19 insertions, 26 deletions
diff --git a/internal/storage/files/file.go b/internal/storage/files/file.go
index a71811c..b79c43c 100644
--- a/internal/storage/files/file.go
+++ b/internal/storage/files/file.go
@@ -10,19 +10,18 @@ import (
 	"strings"
 
 	"gitlab.com/tozd/go/errors"
+	"go.alanpearce.eu/website/internal/buffer"
 	"go.alanpearce.eu/website/internal/storage"
 )
 
-type File struct {
-	storage.File
-}
+type Content io.ReadSeekCloser
 
 var encodings = map[string]string{
 	"br":   ".br",
 	"gzip": ".gz",
 }
 
-func (r *Reader) OpenFile(path string, filename string) (*File, error) {
+func (r *Reader) OpenFile(path string, filename string) (*storage.File, error) {
 	f, err := os.Open(filename)
 	if err != nil {
 		return nil, errors.WithMessage(err, "could not open file for reading")
@@ -37,15 +36,18 @@ func (r *Reader) OpenFile(path string, filename string) (*File, error) {
 		return nil, errors.WithMessage(err, "could not calculate etag")
 	}
 
-	file := &File{
-		File: storage.File{
-			Path:         path,
-			ContentType:  mime.TypeByExtension(filepath.Ext(filename)),
-			LastModified: stat.ModTime(),
-			Etag:         etag,
-			Encodings: map[string]io.ReadSeekCloser{
-				"identity": f,
-			},
+	buf := new(buffer.Buffer)
+	if _, err := f.WriteTo(buf); err != nil {
+		return nil, errors.WithMessage(err, "could not read file")
+	}
+
+	file := &storage.File{
+		Path:         path,
+		ContentType:  mime.TypeByExtension(filepath.Ext(filename)),
+		LastModified: stat.ModTime(),
+		Etag:         etag,
+		Encodings: map[string]*buffer.Buffer{
+			"identity": buf,
 		},
 	}
 
@@ -58,10 +60,12 @@ func (r *Reader) OpenFile(path string, filename string) (*File, error) {
 
 			return nil, errors.WithMessagef(err, "could not stat file %s", filename+suffix)
 		}
-		file.Encodings[enc], err = os.Open(filename + suffix)
+		bytes, err := os.ReadFile(filename + suffix)
 		if err != nil {
 			return nil, errors.WithMessagef(err, "could not read file %s", filename+suffix)
 		}
+		buf := buffer.NewBuffer(bytes)
+		file.Encodings[enc] = buf
 	}
 
 	return file, nil
@@ -76,23 +80,12 @@ func etag(f io.Reader) (string, error) {
 	return fmt.Sprintf(`W/"%x"`, hash.Sum(nil)), nil
 }
 
-func (f *File) Close() error {
-	var errs []error
-	for _, enc := range f.Encodings {
-		if err := enc.Close(); err != nil {
-			errs = append(errs, err)
-		}
-	}
-
-	return errors.Join(errs...)
-}
-
 func pathNameToFileName(pathname string) string {
 	if strings.HasSuffix(pathname, "/") {
 		pathname = pathname + "index.html"
 	}
 
-	return pathname
+	return strings.TrimPrefix(pathname, "/")
 }
 
 func fileNameToPathName(filename string) string {