Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

13.6 KB
Raw Blame

Uploads and File Ownership

Status: In Progress — 6.0 scope being built on feat/uploads-api (upload contract, parameter rename, store ownership, orphan purge) Target: Spree 6.0 (upload contract, multipart uploads, store ownership) · Spree 6.1 (enforcement, uploader) Depends on: 6.0-media-library.md (media rows, blob reuse), 6.0-mcp-server.md (MCP upload_file and export tools reach files through the Admin API), 6.0-digital-assets.md (private uploads) Author: Damian + Claude Last updated: 2026-10-10 Tracking: Linear V-3742 · GitHub spree/spree#14865

Summary

Files reach Spree through POST /api/v3/admin/direct_uploads, a presigning helper whose request and response follow Active Storage's own shape: nested parameters, a private flag, and a signed blob ID that never expires as the only reference. Two problems follow from that.

  • Blobs have no owner. Nothing records which store uploaded a file, so a signed_id from one store can be attached in another, and storage cannot be measured or cleaned up per store.
  • The contract describes the storage library, not the API. It breaks the flat-params rule for v3, a leaked reference stays attachable forever, and a caller that cannot run a presigned upload, such as the MCP server or the CLI, has no way to send a file at all.

6.0 replaces the endpoint with POST /api/v3/admin/files: a flat request, an expiring opaque reference, and a second way to upload where the bytes travel in the request itself. It also records which store owns every blob. The Admin and Seller APIs are not yet stable, so the old endpoint is removed in the same release, without an alias. 6.1 only tightens: attaching a file from another store is refused, and uploads record who made them. No model changes, so 6.x stays stable for everything built on attachments.

Key Decisions (do not deviate without discussion)

6.0

  • POST /api/v3/admin/files replaces /direct_uploads, with no alias. The Admin and Seller APIs are not yet stable, so the old endpoint and its request shape are removed in the same release, not deprecated. "Direct" stops being true once the endpoint also accepts the bytes themselves. /files names what the caller sends, in the API's own words rather than the storage library's; only create exists in 6.0, so it promises no listing or fetching yet (decided 2026-10-10 over /uploads). The Seller API gets the same endpoint.
  • Flat request. filename, content_type, byte_size, checksum (base64 MD5) and visibility (public or private) sit at the top level, following the flat-params rule for v3. The { blob: { … } } wrapper and the private: true flag are gone.
  • Two ways to upload, one response.
    • Presigned: JSON metadata returns a target the client PUTs the bytes to. The dashboard and seller panel use this, so large files go straight to storage.
    • Multipart: a file part in the request, and the server stores the bytes itself, returning the same response with upload: null. The MCP server and the CLI use this. A caller that cannot make a second request (a model calling a tool) needs it, and on disk storage it avoids a presigned target that points back at the same application.
  • signed_id keeps its name and is documented as opaque. Every write endpoint already accepts it. Clients pass it back unchanged and must not parse it, so the way references are issued can change without breaking anyone.
  • Signed IDs expire. Uploads mint signed_id(expires_in: 1.day), returned with expires_at. Responses that offer an existing file for reuse (the media library) mint a fresh one on each read. A leaked reference is no longer attachable forever.
  • Blobs belong to a store. A nullable store_id column on active_storage_blobs; Active Storage ignores columns it does not define. Set when every blob is created, and backfilled for existing rows by the 6.0 upgrade.
  • 6.0 checks and logs; 6.1 refuses. Attaching a blob whose store differs from its owner's, or that has no store, is logged in 6.0 and refused from 6.1. One release of logs finds every path that creates blobs without a store before anything depends on the check.
  • Image URLs are documented as opaque. Clients store and render them as given and never build or parse them, so the format and host can change (a CDN, a different storage service) without breaking anyone.
  • Every upload reference parameter is named signed_id or <slot>_signed_id (2026-10-10). Renamed in the same change as the endpoint, while the breaking window is open: avatar → avatar_signed_id, logo → logo_signed_id, image → image_signed_id, document → document_signed_id, attachment → attachment_signed_id, file → file_signed_id, po_document → po_document_signed_id, and so on for every slot. Clients no longer translate between names; the old names are removed, not aliased, in the Admin and Seller APIs, and the Store API change is in the upgrade guide.
  • Public uploads are allowlisted, private uploads are open (2026-10-10). Public: web image types, Spree::Config.video_content_types and text/csv. Private: any type, because digital products are ebooks, archives and audio, and the attachment's own validation is the real check. Limits are installation settings: Spree::Config.max_upload_size (5 GB) for presigned uploads and max_multipart_upload_size (10 MB, matching MCP upload_file) for multipart.
  • Never-attached uploads are purged by a daily core job (2026-10-10). Rails purges nothing on its own and neither did Spree. Spree::Uploads::PurgeUnattachedJob removes blobs that belong to a store and have no attachment, older than Spree::Config.unattached_upload_retention_days (default 2, longer than the 1-day reference expiry, so nothing a client can still attach is lost). An installation setting rather than a store one: storage is installation infrastructure.
  • The Store API purchase-order presigner adopts the same contract and stays nested (2026-10-10). POST /api/v3/store/carts/:cart_id/po_document keeps its cart-scoped route, because a publishable key is public and must not mint uploads freely, but takes the flat request, returns the same response shape with an expiring signed_id, and records the store. It is the one exception to "uploads go through /files only".
  • MCP is not on main yet (2026-10-10). The MCP server branch (6.0-mcp-server.md) adopts the multipart request when it rebases; this change ships the endpoint it will call.

6.1

  • The store check refuses. After a release of 6.0 logs, attaching a blob from another store, or one with no store, is refused.
  • Uploads record who made them (2026-10-10). uploaded_by_type / uploaded_by_id on active_storage_blobs, short type names per 6.0-action-actors.md, set by the same creation hook that sets store_id. One migration and one hook line answer "who uploaded this file" for audits and seller disputes.

Design Details

The upload contract

Presigned:

POST /api/v3/admin/files
Content-Type: application/json

{ "filename": "products.csv", "content_type": "text/csv", "byte_size": 48213,
  "checksum": "<base64 MD5>", "visibility": "private" }
201
{ "signed_id": "…opaque…",
  "filename": "products.csv", "content_type": "text/csv", "byte_size": 48213,
  "visibility": "private", "expires_at": "2026-10-11T12:00:00Z",
  "upload": { "method": "PUT", "url": "https://…", "headers": { "Content-MD5": "…" } } }

The client PUTs the bytes to upload.url with upload.headers, treating both as opaque, then passes signed_id to the endpoint that uses the file.

Multipart:

POST /api/v3/admin/files
Content-Type: multipart/form-data

filename, content_type, visibility, file=<bytes>

The server takes byte_size and checksum from the bytes it received and returns the same response with upload: null. The file is already stored.

Both ways:

  • The endpoint enforces the content-type allowlist for public uploads and the size limit for both. Multipart has a lower limit, because the bytes pass through the application; larger files use the presigned upload.
  • visibility selects the public or private storage service.
  • store_id is set on the blob (see below).
  • The Seller API's POST /api/v3/seller/files is identical, behind the seller's own scope.

Callers that move in the same change. The admin and seller SDKs replace directUploads.create() with files.create(), plus a helper that runs create and PUT and returns the signed_id in one call. These move together:

  • Dashboard and seller panel: product and variant media and posters, digital assets, imports, shipping labels, the store logo and mailer logo, avatars, tax exemption certificates, and seller requirement submissions.
  • MCP: upload_file dispatches the multipart request through the Admin API, replacing its direct blob creation.
  • CLI: spree api gains an upload command built on the multipart request.

Store ownership

  • Column: active_storage_blobs.store_id, nullable, indexed.
  • Set on creation:
    • POST /files passes store_id: current_store.id in both ways of uploading.
    • Blobs Spree creates internally (imports, exports, shipping labels, data requests, seeds) get the store from one ActiveSupport.on_load(:active_storage_blob) hook reading Spree::Current.store.
  • Checked on attachment: one check when an ActiveStorage::Attachment is created. The blob's store must equal the owning record's store: record.store_id, or the owner's store for records without one (media through its viewable, variants through their product). It lives on the attachment model, so controllers, workflows, extensions and console use are all covered.
    • In 6.0 a mismatch, or a blob with no store, is logged with the attachment slot and owner. From 6.1 it is refused.
    • Records with no store (global admin avatars) are exempt.
  • Backfill: spree:uploads:assign_stores runs as part of the 6.0 upgrade manifest (after db:migrate), as an ActiveJob::Continuable task with one set-based UPDATE … FROM active_storage_attachments JOIN <owner table> per attachment slot, in ID ranges, safe to rerun. A blob attached in two stores is reported, not resolved. Unattached blobs are left without a store, and the daily purge leaves them alone: it only deletes files that belong to a store, since a host app may keep blobs of its own outside attachments (2026-10-10, from review).

What ownership enables beyond the security check:

  • storage used per store;
  • cleanup of a store's orphaned uploads;
  • deleting a store's files when the store is deleted.

Migration Path

6.0

  1. Migration ([8.1]): store_id, indexed, on active_storage_blobs.
  2. POST /files for the Admin and Seller APIs: the flat request, visibility, presigned and multipart uploads, the response shape, expiring signed_id, and store_id on the blob. /direct_uploads and its controllers, concern and routes are deleted.
  3. The store hook on internal blob creation, and the store check on attachment creation, logging only.
  4. Admin and seller SDK files.create() plus the helper; directUploads removed. The dashboard and seller panel move every upload flow over. MCP upload_file dispatches the multipart request. spree api gains the upload command. Regenerate types, Zod schemas and OpenAPI.
  5. spree:uploads:assign_stores in the 6.0 upgrade manifest, with a report of blobs attached in two stores.
  6. Every upload reference parameter renamed to signed_id / <slot>_signed_id, and the daily purge of never-attached uploads.
  7. Docs: an uploads page (both ways of uploading, the opaque signed_id, expiry, visibility, size limits), image URLs and signed_id called out as opaque in the API introduction, and a changelog entry for the endpoint replacement.

6.1

  1. The store check refuses mismatches and blobs with no store.
  2. Uploader columns (uploaded_by_type, uploaded_by_id).

Constraints on Current Work

  • Uploads go through POST /files only. Do not add another way to create a blob from the API.
  • Never put a blob ID, storage key or Rails blob URL in an API response. Public images go through image_url_for / cdn_image_url. Upload references are signed_id values, minted with an expiry.
  • Every blob Spree creates belongs to a store. Pass store_id, or create it inside a request or job where Spree::Current.store is set. A 6.0 log line about a blob with no store is a bug to fix before 6.1 starts refusing.
  • No new has_many_attached. A record with several images uses media rows; a private document is a single has_one_attached slot.
  • An image a merchant picks or reuses is a media row, not a new attachment on the owner. Logos, covers and swatches added from now on go through the media library.
  • Upload references are parameters named signed_id or <slot>_signed_id. Do not add new names (document, file, attachment) for the same thing.
  • Do not build URLs with rails_blob_* or rails_representation_* helpers outside cdn_image. Keeping one place that builds image URLs is what lets their host and format change without touching serializers.

Open Questions

None.

References

  • docs/plans/6.0-media-library.md: media rows, blob reuse and deletion safety
  • docs/plans/6.0-mcp-server.md: MCP upload_file and export tools reach files only through the Admin API
  • docs/plans/6.0-action-actors.md: actor model for uploaded_by
  • spree/api/app/controllers/spree/api/v3/admin/direct_uploads_controller.rb, spree/api/app/controllers/concerns/spree/api/v3/direct_uploads.rb: the endpoint 6.0 replaces
  • spree/core/config/routes.rb (cdn_image): the one place public image URLs are built