Files
probo/pkg/server/api/files/v1/handler.go
Ludovic Vielle dbfd191bc5 Add cache control to Files API static assets
Brand assets served at /api/files/v1/static had no cache headers.
Introduce brand.Assets to own the embedded filesystem, content-hash
ETags, and HTTP serving. Responses now carry Cache-Control and ETag
so clients can cache and revalidate; stable email URLs stay
revalidatable (max-age=3600, no immutable).

Replace hardcoded Default*Path constants with StaticPathPrefix,
logical filename constants, and StaticPath(). NewAssets validates
required assets at startup so a rename fails fast instead of 404ing
in sent emails. The files handler keeps routing and 404 rendering;
ServeAssets sets cache headers and serves the file.

Signed-off-by: Ludovic Vielle <ludovic@probo.com>
2026-06-12 17:24:00 +02:00

187 lines
4.9 KiB
Go

// Copyright (c) 2026 Probo Inc <hello@probo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package files_v1
import (
"errors"
"fmt"
"net/http"
"time"
"github.com/go-chi/chi/v5"
"go.gearno.de/kit/log"
"go.probo.inc/probo/pkg/brand"
"go.probo.inc/probo/pkg/coredata"
"go.probo.inc/probo/pkg/filemanager"
"go.probo.inc/probo/pkg/gid"
"go.probo.inc/probo/pkg/iam"
"go.probo.inc/probo/pkg/probo"
"go.probo.inc/probo/pkg/securecookie"
"go.probo.inc/probo/pkg/server/api/authn"
"go.probo.inc/probo/pkg/server/jsonx"
)
const presignedURLExpiry = 1 * time.Hour
type Handler struct {
logger *log.Logger
fileSvc *filemanager.Service
probo *probo.Service
iamSvc *iam.Service
assets *brand.Assets
}
func NewMux(
logger *log.Logger,
fileSvc *filemanager.Service,
proboSvc *probo.Service,
iamSvc *iam.Service,
cookieConfig securecookie.Config,
tokenSecret string,
) *chi.Mux {
h := &Handler{
logger: logger,
fileSvc: fileSvc,
probo: proboSvc,
iamSvc: iamSvc,
assets: brand.NewAssets(),
}
r := chi.NewRouter()
r.Get("/static/{file}", h.handleGetStaticFile)
r.Get("/public/{fileID}", h.handleGetPublicFile)
r.Group(func(r chi.Router) {
r.Use(authn.NewSessionMiddleware(iamSvc, cookieConfig))
r.Use(authn.NewAPIKeyMiddleware(iamSvc, tokenSecret))
r.Use(authn.NewOAuth2AccessTokenMiddleware(iamSvc))
r.Use(authn.NewIdentityPresenceMiddleware())
r.Get("/{fileID}", h.handleGetFile)
})
return r
}
func (h *Handler) handleGetStaticFile(w http.ResponseWriter, r *http.Request) {
file := chi.URLParam(r, "file")
if _, statErr := h.assets.Stat(file); statErr != nil {
jsonx.RenderNotFound(w, fmt.Errorf("file not found"))
return
}
// ServeAssets honors the ETag header we set above for If-None-Match (and
// If-Range), so it emits 304 Not Modified and handles range requests without
// any extra conditional logic here.
h.assets.ServeAssets(w, r, file)
}
func (h *Handler) handleGetPublicFile(w http.ResponseWriter, r *http.Request) {
fileIDStr := chi.URLParam(r, "fileID")
fileID, err := gid.ParseGID(fileIDStr)
if err != nil {
jsonx.RenderNotFound(w, fmt.Errorf("file not found"))
return
}
file, err := h.fileSvc.GetPublicFile(r.Context(), fileID)
if err != nil {
if errors.Is(err, coredata.ErrResourceNotFound) {
jsonx.RenderNotFound(w, fmt.Errorf("file not found"))
return
}
h.logger.ErrorCtx(
r.Context(),
"cannot get public file URL",
log.Error(err),
log.String("file_id", fileIDStr),
)
jsonx.RenderInternalServerError(w)
return
}
presignedURL, err := h.fileSvc.GeneratePresignedURL(r.Context(), file, presignedURLExpiry)
if err != nil {
h.logger.ErrorCtx(
r.Context(),
"cannot get public file URL",
log.Error(err),
log.String("file_id", fileIDStr),
)
jsonx.RenderInternalServerError(w)
return
}
http.Redirect(w, r, presignedURL, http.StatusTemporaryRedirect)
}
func (h *Handler) handleGetFile(w http.ResponseWriter, r *http.Request) {
fileIDStr := chi.URLParam(r, "fileID")
fileID, err := gid.ParseGID(fileIDStr)
if err != nil {
jsonx.RenderNotFound(w, fmt.Errorf("file not found"))
return
}
ctx := r.Context()
identity := authn.IdentityFromContext(ctx)
session := authn.SessionFromContext(ctx)
params := iam.AuthorizeParams{
Principal: identity.ID,
Resource: fileID,
Action: probo.ActionFileGet,
ResourceAttributes: make(map[string]string),
}
if session != nil {
params.Session = &session.ID
}
scope, err := h.iamSvc.Authorizer.Authorize(ctx, params)
if err != nil {
jsonx.RenderNotFound(w, fmt.Errorf("file not found"))
return
}
f, err := h.probo.Files.Get(ctx, scope, fileID)
if err != nil {
if errors.Is(err, coredata.ErrResourceNotFound) {
jsonx.RenderNotFound(w, fmt.Errorf("file not found"))
return
}
h.logger.ErrorCtx(ctx, "cannot get file", log.Error(err), log.String("file_id", fileIDStr))
jsonx.RenderInternalServerError(w)
return
}
presignedURL, err := h.fileSvc.GeneratePresignedURL(ctx, f, presignedURLExpiry)
if err != nil {
h.logger.ErrorCtx(ctx, "cannot generate file URL", log.Error(err), log.String("file_id", fileIDStr))
jsonx.RenderInternalServerError(w)
return
}
http.Redirect(w, r, presignedURL, http.StatusTemporaryRedirect)
}