feat(album): add core Media Library module, migrations, and surf routes
This commit is contained in:
120
modules/surf/media.go
Normal file
120
modules/surf/media.go
Normal file
@@ -0,0 +1,120 @@
|
||||
package surf
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/album"
|
||||
"git.golem15.com/golem15/summercms/modules/backpack"
|
||||
"git.golem15.com/golem15/summercms/modules/pact"
|
||||
"gocloud.dev/blob"
|
||||
)
|
||||
|
||||
// mediaPluginID owns the framework media routes in the route table.
|
||||
const mediaPluginID = "summercms.album"
|
||||
|
||||
// mountMedia installs *album.Service on app and mounts the public zero-DB-hit
|
||||
// media static route (GET <prefix>/{short_id}/{file}) when album.ServeEnabled
|
||||
// is true, plus the raw single-use signed upload routes
|
||||
// (PUT and POST <prefix>/upload/{token}) behind an inline rate limit when
|
||||
// media serving, admin backend, or MCP is enabled.
|
||||
//
|
||||
// When static serving is enabled, the route owns the prefix against plugin
|
||||
// route collisions, matching mountUploads.
|
||||
func mountMedia(r *Router, app *backpack.App) error {
|
||||
if r == nil || app == nil {
|
||||
return nil
|
||||
}
|
||||
svc, err := album.Install(app)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if app.Config == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
prefix, err := mediaPrefix(app.Config.String(album.ConfigPublicPathPrefix))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
serveStatic := album.ServeEnabled(app.Config)
|
||||
// When media.serve is not explicitly set and only inherited from
|
||||
// storage.uploads.serve, do not collide if the app explicitly configured
|
||||
// storage.uploads.public_path_prefix to the same path (e.g. /media).
|
||||
if serveStatic && !app.Config.Has(album.ConfigServe) && app.Config.Bool("storage.uploads.serve") {
|
||||
if upPrefix, uErr := uploadsPrefix(app.Config.String("storage.uploads.public_path_prefix")); uErr == nil {
|
||||
if upPrefix == prefix || strings.HasPrefix(upPrefix, prefix+"/") || strings.HasPrefix(prefix, upPrefix+"/") {
|
||||
serveStatic = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mountUpload := serveStatic ||
|
||||
app.Config.Bool("mcp.enabled") ||
|
||||
app.Config.Bool("backend.enabled") ||
|
||||
app.Config.Has(album.ConfigServe) ||
|
||||
app.Config.Has(album.ConfigPublicPathPrefix) ||
|
||||
app.Config.Has(album.ConfigMaxBytes)
|
||||
|
||||
if !serveStatic && !mountUpload {
|
||||
return nil
|
||||
}
|
||||
|
||||
if serveStatic {
|
||||
for _, rt := range r.routes {
|
||||
if rt.path == prefix || strings.HasPrefix(rt.path, prefix+"/") {
|
||||
return fmt.Errorf("surf: route %s %s (plugin %q) is under the media prefix %s %s, which media.serve mounts", rt.method, rt.path, rt.pluginID, album.ConfigPublicPathPrefix, prefix)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
r.BindPlugin(mediaPluginID)
|
||||
r.GroupRaw(prefix, nil, func(g pact.Router) {
|
||||
if serveStatic {
|
||||
g.Get("/{short_id}/{file}", mediaStaticHandler(app, r.pages, prefix))
|
||||
}
|
||||
if mountUpload {
|
||||
uploadHandler := album.SignedUploadHandler(app, svc).ServeHTTP
|
||||
g.Put("/upload/{token}", uploadHandler, "throttle:60,1")
|
||||
g.Post("/upload/{token}", uploadHandler, "throttle:60,1")
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// mediaPrefix validates media.public_path_prefix with the same rules as uploadsPrefix.
|
||||
func mediaPrefix(raw string) (string, error) {
|
||||
value := strings.TrimSpace(raw)
|
||||
if value == "" {
|
||||
return album.DefaultPublicPathPrefix, nil
|
||||
}
|
||||
prefix := strings.TrimSuffix(value, "/")
|
||||
bad := !strings.HasPrefix(prefix, "/") || strings.ContainsAny(prefix, "{}?# \t\r\n\\")
|
||||
if !bad {
|
||||
for _, seg := range strings.Split(prefix[1:], "/") {
|
||||
if seg == "" || seg == "." || seg == ".." {
|
||||
bad = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if bad {
|
||||
return "", fmt.Errorf("surf: %s must be an absolute URL path such as /media (no scheme, host, query or wildcard), got %q", album.ConfigPublicPathPrefix, raw)
|
||||
}
|
||||
return prefix, nil
|
||||
}
|
||||
|
||||
func mediaStaticHandler(app *backpack.App, pages *errorPages, prefix string) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, req *http.Request) {
|
||||
bucket, ok := app.Lookup[*blob.Bucket]()
|
||||
if !ok || bucket == nil {
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.Header().Set("Content-Security-Policy", album.MediaContentSecurityPolicy)
|
||||
pages.notFound(w, req)
|
||||
return
|
||||
}
|
||||
album.StaticHandler(bucket, prefix).ServeHTTP(w, req)
|
||||
}
|
||||
}
|
||||
@@ -747,6 +747,9 @@ func (r *Router) build(app *backpack.App, plugins []party.Plugin) error {
|
||||
if err := mountUploads(r, app); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := mountMedia(r, app); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := mountAPIDocs(r, app, plugins); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user