diff options
Diffstat (limited to 'src/gmi')
-rw-r--r-- | src/gmi/gmi.go | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/src/gmi/gmi.go b/src/gmi/gmi.go index 127f6b5..e0c4626 100644 --- a/src/gmi/gmi.go +++ b/src/gmi/gmi.go @@ -2,13 +2,16 @@ package gmi import ( + "bytes" "context" "errors" "fmt" + "io" "mime" "net/url" "os" "path" + "path/filepath" "strings" "git.sr.ht/~adnano/go-gemini" @@ -27,8 +30,8 @@ type Params struct { Logger *mlog.Logger Cache cache.Cache - PostStore post.Store - PostAssetStore asset.Store + PostStore post.Store + PostAssetLoader asset.Loader PublicURL *url.URL ListenAddr string @@ -208,16 +211,18 @@ func (a *api) assetsMiddleware() gemini.Handler { r *gemini.Request, ) { - id := path.Base(r.URL.Path) - mimeType := mime.TypeByExtension(path.Ext(id)) + path := strings.TrimPrefix(r.URL.Path, "/assets/") + mimeType := mime.TypeByExtension(filepath.Ext(path)) - ctx = mctx.Annotate(ctx, "assetID", id, "mimeType", mimeType) + ctx = mctx.Annotate(ctx, "assetPath", path, "mimeType", mimeType) if mimeType != "" { rw.SetMediaType(mimeType) } - err := a.params.PostAssetStore.Get(id, rw) + buf := new(bytes.Buffer) + + err := a.params.PostAssetLoader.Load(path, buf, asset.LoadOpts{}) if errors.Is(err, asset.ErrNotFound) { rw.WriteHeader(gemini.StatusNotFound, "Asset not found, sorry!") @@ -228,6 +233,12 @@ func (a *api) assetsMiddleware() gemini.Handler { rw.WriteHeader(gemini.StatusTemporaryFailure, err.Error()) return } + + if _, err := io.Copy(rw, buf); err != nil { + a.params.Logger.Error(ctx, "error copying asset", err) + rw.WriteHeader(gemini.StatusTemporaryFailure, err.Error()) + return + } }) } |