feat: store thumbnails in project mounts with legacy fallback
Lint / Lint (push) Successful in 31s
Tests / Go backend (push) Successful in 45s
Tests / Dashboard checks (push) Successful in 50s
Docker / Docker Build & Push (push) Successful in 5m9s

- Add thumbnail upload, lookup, deletion, and scratch-fetch helpers
- Distinguish missing mounts from other listing failures
This commit is contained in:
Gabriel20xx committed 2026-10-15 13:56:24 +00:00
1 parent 483a1a278b
commit 6aec03fb3d
16 files changed
+846 -126

No files matched your search

@@ -14,6 +14,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
@@ -264,9 +265,19 @@ func (c *Client) ProxyMount(w http.ResponseWriter, r *http.Request, project, mou
c.Proxy(w, r, path)
}
// ErrMountNotFound is returned by ListMountFiles when every CarrierNode
// instance that answered said the directory does not exist and none failed
// some other way. It is a definitive "not there", unlike an unreachable
// instance or an error response, where the directory may well exist but could
// not be checked - callers can use errors.Is to tell the two apart.
var ErrMountNotFound = errors.New("carriernode mount directory not found")
// ListMountFiles lists the direct children of a project/type mount directory.
// An empty relative path lists the mount root. The CarrierNode endpoint
// returns both files and directories so callers can build directory browsers.
// When no instance can list the directory, the error wraps ErrMountNotFound
// only if every instance answered 404; any unreachable instance or other
// failure yields a plain error instead, since the directory may live there.
func (c *Client) ListMountFiles(ctx context.Context, project, mountType, relative string) ([]MountFileEntry, error) {
candidates := c.candidateBaseURLs(ctx)
if len(candidates) == 0 {
@@ -286,6 +297,7 @@ func (c *Client) ListMountFiles(ctx context.Context, project, mountType, relativ
var entries []MountFileEntry
var lastErr error
loaded := false
missing := false
for _, baseURL := range candidates {
request, err := c.newRequest(ctx, baseURL, http.MethodGet, path, nil)
if err != nil {
@@ -297,6 +309,11 @@ func (c *Client) ListMountFiles(ctx context.Context, project, mountType, relativ
lastErr = err
continue
}
if response.StatusCode == http.StatusNotFound {
response.Body.Close()
missing = true
continue
}
if response.StatusCode != http.StatusOK {
response.Body.Close()
lastErr = fmt.Errorf("carriernode mount listing failed: %s", response.Status)
@@ -314,8 +331,13 @@ func (c *Client) ListMountFiles(ctx context.Context, project, mountType, relativ
loaded = true
entries = append(entries, payload.Files...)
}
if !loaded && lastErr != nil {
return nil, lastErr
if !loaded {
if lastErr != nil {
return nil, lastErr
}
if missing {
return nil, ErrMountNotFound
}
}
unique := make([]MountFileEntry, 0, len(entries))
seen := make(map[string]struct{}, len(entries))
@@ -421,6 +443,103 @@ func (c *Client) DeleteMountFile(ctx context.Context, project, mountType, relati
return nil
}
// ThumbnailMountType is the mount type ProjectConfigurator configures for a project's
// generated thumbnails. Each thumbnail is one "<id>.jpg" file under that mount.
const ThumbnailMountType = "thumbnails"
// thumbnailFile is the file name a thumbnail id is stored under. Ids are UUIDs that
// ContentScanner generates, so anything that could name a path outside the thumbnails
// directory is refused.
func thumbnailFile(id string) (string, error) {
if id == "" || id == "." || id == ".." || strings.ContainsAny(id, `/\`) {
return "", fmt.Errorf("invalid thumbnail id %q", id)
}
return id + ".jpg", nil
}
// thumbnailPaths lists every CarrierNode path a thumbnail may live at, in lookup order: the
// project's thumbnails mount first, then CarrierNode's legacy thumbnail root. Thumbnails
// stored before the mount existed sit in the legacy root, and reads keep finding them there
// until they are regenerated or deleted.
func thumbnailPaths(project, id string) ([]string, error) {
file, err := thumbnailFile(id)
if err != nil {
return nil, err
}
var paths []string
if path := MountPath(project, ThumbnailMountType, file); path != "" {
paths = append(paths, path)
}
return append(paths, "/thumbnails/"+file), nil
}
// StoreThumbnail uploads a generated thumbnail into the project's thumbnails mount on the
// primary CarrierNode and returns its size in bytes. CarrierNode only accepts mount writes
// under its /mount volume, so the configured thumbnails path has to be there.
func (c *Client) StoreThumbnail(ctx context.Context, project, id, localPath string) (int64, error) {
file, err := thumbnailFile(id)
if err != nil {
return 0, err
}
info, err := os.Stat(localPath)
if err != nil {
return 0, err
}
if err := c.UploadMountFile(ctx, project, ThumbnailMountType, file, localPath); err != nil {
return 0, err
}
return info.Size(), nil
}
// DeleteStoredThumbnail removes a thumbnail from the project's thumbnails mount and from the
// legacy root, since one stored before the mount existed may still be there. A thumbnail
// missing from both is already deleted.
func (c *Client) DeleteStoredThumbnail(ctx context.Context, project, id string) error {
file, err := thumbnailFile(id)
if err != nil {
return err
}
mountErr := c.DeleteMountFile(ctx, project, ThumbnailMountType, file)
legacyErr := c.DeleteThumbnail(ctx, id)
return errors.Join(mountErr, legacyErr)
}
// ProxyThumbnail streams a stored thumbnail to the original client, looking in the project's
// thumbnails mount and then the legacy root. It reports false without writing anything when
// neither holds the thumbnail, so the caller can fall back to another source.
func (c *Client) ProxyThumbnail(w http.ResponseWriter, r *http.Request, project, id string) bool {
paths, err := thumbnailPaths(project, id)
if err != nil {
return false
}
for _, path := range paths {
if exists, _, _, probeErr := c.Probe(r.Context(), path); probeErr == nil && exists {
c.Proxy(w, r, path)
return true
}
}
return false
}
// FetchThumbnailToScratch downloads a stored thumbnail into an ephemeral local file, looking
// in the project's thumbnails mount and then the legacy root. Callers must invoke the returned
// cleanup once done.
func (c *Client) FetchThumbnailToScratch(ctx context.Context, project, id, suffix string) (string, func(), error) {
paths, err := thumbnailPaths(project, id)
if err != nil {
return "", nil, err
}
var lastErr error
for _, path := range paths {
scratch, cleanup, fetchErr := c.FetchToScratch(ctx, path, suffix)
if fetchErr == nil {
return scratch, cleanup, nil
}
lastErr = fetchErr
}
return "", nil, lastErr
}
// probeOne runs Probe's HEAD request against a single base URL.
func (c *Client) probeOne(ctx context.Context, baseURL, carrierNodePath string) (exists bool, size int64, modTime time.Time, err error) {
request, err := c.newRequest(ctx, baseURL, http.MethodHead, carrierNodePath, nil)