summaryrefslogtreecommitdiff
path: root/src/gmi
diff options
context:
space:
mode:
authorBrian Picciano <mediocregopher@gmail.com>2023-04-15 21:35:06 +0200
committerBrian Picciano <mediocregopher@gmail.com>2023-04-17 04:17:45 +0200
commit5559e0134382a141f5edabdacf1dc81f12b55c27 (patch)
tree0cd897880e52572f1e12334dfe94535a140549ce /src/gmi
parent7872296b838f4d1b26c6a0a01d79d27fe5ab44cc (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/gmi')
-rw-r--r--src/gmi/gmi.go23
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
+ }
})
}