drop storage interface (only one implementation)
8 files changed, 106 insertions(+), 140 deletions(-)
M domain/content/builder/build/main.go → domain/content/builder/build/main.go
@@ -10,7 +10,7 @@ "github.com/ardanlabs/conf/v3" "alin.ovh/homestead/domain/content/builder" "alin.ovh/homestead/shared/config" - "alin.ovh/homestead/shared/storage/files" + "alin.ovh/homestead/shared/storage" "alin.ovh/homestead/shared/vcs" )@@ -42,10 +42,10 @@ panic("could not open repository: " + err.Error()) } options.Repo = repo - options.Storage, err = files.NewWriter( + options.Storage, err = storage.NewWriter( options.Destination, log.Named("storage"), - &files.Options{ + &storage.Options{ Compress: options.Compress, }, )
M domain/content/builder/builder.go → domain/content/builder/builder.go
@@ -30,7 +30,7 @@ Source string `conf:"default:.,short:s,flag:src"` Development bool `conf:"default:false,flag:dev"` VCSRemoteURL config.URL `conf:"default:https://git.alin.ovh/website"` - Storage storage.Writer `conf:"-"` + Storage *storage.Writer `conf:"-"` Repo *vcs.Repository `conf:"-"` }@@ -40,7 +40,7 @@ return filepath.Join(src, rel) } } -func copyFile(storage storage.Writer, src string, rel string) error { +func copyFile(storage *storage.Writer, src string, rel string) error { sf, err := os.Open(src) if err != nil { return fault.Wrap(err)
M domain/web/website.go → domain/web/website.go
@@ -23,7 +23,6 @@ "alin.ovh/homestead/shared/events" "alin.ovh/homestead/shared/file" ihttp "alin.ovh/homestead/shared/http" "alin.ovh/homestead/shared/storage" - "alin.ovh/homestead/shared/storage/files" ) type Options struct {@@ -46,7 +45,7 @@ config *config.Config siteSettings *templates.SiteSettings counter analytics.Counter log *log.Logger - reader storage.Reader + reader *storage.Reader identity *identity.Service *server.App }@@ -140,7 +139,7 @@ } website.Domain = cfg.BaseURL.Hostname() - website.reader, err = files.NewReader(root, log.Named("reader")) + website.reader, err = storage.NewReader(root, log.Named("reader")) if err != nil { log.Panic("could not create database reader", "error", err) }
D shared/storage/files/file.go
@@ -1,87 +0,0 @@ -package files - -import ( - "errors" - "fmt" - "hash/fnv" - "io" - "os" - "strings" - - "github.com/Southclaws/fault" - "github.com/Southclaws/fault/fmsg" - - "alin.ovh/homestead/shared/storage" -) - -var encodings = map[string]string{ - "br": ".br", - "gzip": ".gz", - "zstd": ".zstd", -} - -func (r *Reader) OpenFile(path string, filename string) (*storage.File, error) { - file, err := storage.NewFile(path, filename) - if err != nil { - return nil, fault.Wrap(err, fmsg.With("could not init file")) - } - - file.Etag, err = etag(file.Encodings["identity"]) - if err != nil { - return nil, err - } - - for enc, suffix := range encodings { - _, err := os.Stat(filename + suffix) - if err != nil { - if errors.Is(err, os.ErrNotExist) { - continue - } - - return nil, fault.Wrap( - err, - fmsg.With(fmt.Sprintf("could not stat file %s", 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)), - ) - } - file.Encodings[enc] = f - } - - return file, nil -} - -func etag(f io.Reader) (string, error) { - hash := fnv.New64a() - if _, err := io.Copy(hash, f); err != nil { - return "", fault.Wrap(err, fmsg.With("could not hash file")) - } - - return fmt.Sprintf(`W/"%x"`, hash.Sum(nil)), nil -} - -func pathNameToFileName(pathname string) string { - if strings.HasSuffix(pathname, "/") { - pathname += "index.html" - } - - return strings.TrimPrefix(pathname, "/") -} - -func cutSuffix(s, suffix string) string { - out, _ := strings.CutSuffix(s, suffix) - - return out -} - -func fileNameToPathName(filename string) string { - return cutSuffix( - cutSuffix(filename, "index.html"), - ".html", - ) -}
D shared/storage/interface.go
@@ -1,22 +0,0 @@ -package storage - -import ( - "io" - - "alin.ovh/homestead/domain/content" -) - -type Reader interface { - GetFile(path string) (*File, error) - CanonicalisePath(path string) (string, bool) -} - -type Writer interface { - Mkdirp(path string) error - - NewFileFromPost(*content.Post) *File - - Write(string, string, io.WriterTo) error - WritePost(*content.Post, io.WriterTo) error - WriteFile(*File, io.WriterTo) error -}