The Background panel, the Box editor and the canvas admin bar could only take a new upload, so reusing a picture meant uploading it again or pasting its URL. Each now has a picker (UploadPicker.vue): a grid of the public uploads, newest first, filterable by name. It lists through a new admin-only GET /api/uploads (Uploads.List), the union of the Garage bucket and the disk fallback, since nginx serves from both; a bucket that can't list falls back to the disk's files. Admin-only because upload URLs are public but unguessable, and a public listing would give every one away. Private files and anything off the UploadTypes allow-list are never listed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
90 lines
3.0 KiB
Go
90 lines
3.0 KiB
Go
package handlers
|
|
|
|
import (
|
|
"errors"
|
|
"log"
|
|
"net/http"
|
|
|
|
"adam-french.co.uk/backend/services"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/golang-jwt/jwt/v5"
|
|
)
|
|
|
|
// Upload backs POST /uploads (login required): the one endpoint the frontend
|
|
// stores a file through (vue/src/uploads.js). Multipart form:
|
|
//
|
|
// file the file (required)
|
|
// private "true" to store it as an admin-only chat attachment
|
|
//
|
|
// It answers {"url": "/uploads/<name>"}, or {"error": "..."} with 400 when
|
|
// the file was refused (wrong type, too large) and 500 when storing failed.
|
|
// Validation, naming and storage are all services.Uploads.Save; this handler
|
|
// only reads the form and decides who may use `private`.
|
|
//
|
|
// `private` is admin-only because private messages themselves are: an
|
|
// attachment URL leaking out is the same disclosure as the message leaking.
|
|
func (store *Store) Upload(ctx *gin.Context) {
|
|
private := ctx.PostForm("private") == "true"
|
|
if private && !requestIsAdmin(ctx) {
|
|
ctx.JSON(http.StatusForbidden, gin.H{"error": "admin access required"})
|
|
return
|
|
}
|
|
|
|
file, err := ctx.FormFile("file")
|
|
if err != nil {
|
|
ctx.JSON(http.StatusBadRequest, gin.H{"error": "file is required"})
|
|
return
|
|
}
|
|
|
|
url, err := store.Uploads.Save(ctx.Request.Context(), file, private)
|
|
var rejected *services.UploadError
|
|
switch {
|
|
case errors.As(err, &rejected):
|
|
ctx.JSON(http.StatusBadRequest, gin.H{"error": rejected.Message})
|
|
case err != nil:
|
|
log.Printf("[upload] storing %s failed: %v", file.Filename, err)
|
|
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "failed to save file"})
|
|
default:
|
|
ctx.JSON(http.StatusOK, gin.H{"url": url})
|
|
}
|
|
}
|
|
|
|
// ListUploads backs GET /uploads (admin): every public upload, newest first,
|
|
// for the upload picker on the canvas pages (vue/src/components/util/
|
|
// UploadPicker.vue). It answers {"files": [{name, url, size, modified}]}.
|
|
//
|
|
// Admin-only because the listing is the one thing that turns unguessable
|
|
// upload URLs into known ones; see services.Uploads.List.
|
|
func (store *Store) ListUploads(ctx *gin.Context) {
|
|
files, err := store.Uploads.List(ctx.Request.Context())
|
|
if err != nil {
|
|
log.Printf("[upload] listing failed: %v", err)
|
|
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "failed to list uploads"})
|
|
return
|
|
}
|
|
if files == nil {
|
|
files = []services.StoredFile{} // [] rather than null for the frontend
|
|
}
|
|
ctx.JSON(http.StatusOK, gin.H{"files": files})
|
|
}
|
|
|
|
// requestIsAdmin reports whether the caller's access token carries admin=true.
|
|
//
|
|
// This route sits in the `protected` group, which runs AuthMiddlewear but not
|
|
// AdminMiddleware — the endpoint has to stay usable by any signed-in user for
|
|
// public attachments — so the admin check is made here from the same
|
|
// "userClaims" value AdminMiddleware would have read.
|
|
func requestIsAdmin(ctx *gin.Context) bool {
|
|
claims, exists := ctx.Get("userClaims")
|
|
if !exists {
|
|
return false
|
|
}
|
|
mapClaims, ok := claims.(*jwt.MapClaims)
|
|
if !ok {
|
|
return false
|
|
}
|
|
admin, ok := (*mapClaims)["admin"].(bool)
|
|
|
|
return ok && admin
|
|
}
|