From 5559e0134382a141f5edabdacf1dc81f12b55c27 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Sat, 15 Apr 2023 21:35:06 +0200 Subject: Implement asset.Loader This moved a bunch of logic out of http and into the asset package, making it available for gmit too. --- src/post/asset/loader.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/post/asset/loader.go (limited to 'src/post/asset/loader.go') diff --git a/src/post/asset/loader.go b/src/post/asset/loader.go new file mode 100644 index 0000000..f7a0f5e --- /dev/null +++ b/src/post/asset/loader.go @@ -0,0 +1,43 @@ +package asset + +import ( + "errors" + "io" + "path/filepath" +) + +var ( + ErrCannotResize = errors.New("cannot resize") +) + +// LoadOpts are optional parameters to Loader's Load method. Some may only apply +// to specific Loader implementations. +type LoadOpts struct { + + // ImageWidth is used by the ImageLoader to resize images on the fly. + ImageWidth int +} + +// Loader is used to load an asset and write its body into the given io.Writer. +// +// Errors: +// - ErrNotFound +// - ErrCannotResize (only if ImageLoader is used) +type Loader interface { + Load(path string, into io.Writer, opts LoadOpts) error +} + +type storeLoader struct { + store Store +} + +// NewStoreLoader returns a Loader which loads assets directly from the given +// Store, with no transformation. +func NewStoreLoader(store Store) Loader { + return &storeLoader{store} +} + +func (l *storeLoader) Load(path string, into io.Writer, opts LoadOpts) error { + id := filepath.Base(path) + return l.store.Get(id, into) +} -- cgit v1.2.3