diff options
author | Brian Picciano <mediocregopher@gmail.com> | 2023-04-15 21:35:06 +0200 |
---|---|---|
committer | Brian Picciano <mediocregopher@gmail.com> | 2023-04-17 04:17:45 +0200 |
commit | 5559e0134382a141f5edabdacf1dc81f12b55c27 (patch) | |
tree | 0cd897880e52572f1e12334dfe94535a140549ce /src/post/asset/loader.go | |
parent | 7872296b838f4d1b26c6a0a01d79d27fe5ab44cc (diff) |
Implement asset.Loader
This moved a bunch of logic out of http and into the asset package,
making it available for gmit too.
Diffstat (limited to 'src/post/asset/loader.go')
-rw-r--r-- | src/post/asset/loader.go | 43 |
1 files changed, 43 insertions, 0 deletions
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) +} |