Repository navigation
feat(content-editor): upload an image or an author's photo instead of pasting its address (00272) - #1474
Conversation
… pasting its address (00272) Most people have the screenshot or photo on their computer, not at an address. 00272 adds a public content-images bucket (JPEG, PNG, WebP or GIF, 5 MB) where a member uploads only into their organization's folder. The browser sends the file straight to Storage and the address fills the field: the toolbar's image dialog and Settings › Authors' photo get "Upload from your computer", with why a file can't go up. Co-Authored-By: Claude Opus 5.5 <[email protected]>
gkhngyk
left a comment
There was a problem hiding this comment.
A few fixes before merge:
-
Limit who can upload, and how much (
00272_content_images_storage.sql:27–37). The bucket is public, and nothing can delete from it yet.- Scope the upload policy to organizations that have the Content Editor. On the managed cloud, that's an active or trialing subscription, the same gate the dashboard uses.
- Cap uploads per organization, for example a count over the last day in a
security definerhelper that the policy calls.
The description lists the quota as a follow-up. With a public bucket, both should be in before this goes live. The delete for the organization's own folder and the cleanup when an organization is deleted can follow.
-
00272_content_images_storage.sql:33and:47: tighten the folder check to the exact name the client builds: one folder deep, the app's file name, and an extension that matches the allowed types. For example, in the insert policy:and name ~ ('^' || (select p.organization_id::text from public.profiles p where p.id = auth.uid()) || '/[0-9a-f-]{32,36}\.(jpg|png|webp|gif)$')
The
{32,36}covers both namesimage-upload.tsbuilds:randomUUIDand the 32-hex fallback.
Dry-run on prod, rolled back, after 00270 and 00271:
- The bucket is created public, with a 5 MB limit and JPEG, PNG, WebP and GIF only (no SVG or HTML).
- A member can insert into their own folder. Another organization's folder, a top-level name and a leading
/are refused. - Members can't update or delete.
- Another organization's member sees nothing in the folder. Anon can't insert or list.
- Storage had no policies before this, so no older policy widens access.
The client names files {organization_id}/{uuid}.{ext}, taking the extension from the MIME type. In the editor, the address goes through safeImageSrc. For authors, it goes through the https-only photo check.
…ge-upload # Conflicts: # supabase/schema.sql
…plan and a daily cap
Review fixes on image uploads (00272):
- only the app knows a managed cloud from a self-host, so it says who may
upload: prepareImageUpload checks the session and, on the cloud, an
active or trialing subscription (the dashboard's gate) and 100 uploads
per organization a day; a self-host has no limits
- the app makes the name ({organization_id}/{uuid}.{ext}, the type's
extension) and a one-time upload address; the browser sends the file
straight to it, so no server action body limit
- the bucket has no member policies: nothing reaches it but through that
address
- the upload says why it can't go: the plan, or the day's uploads made
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Pre-push review: the cap counted the files that had arrived, so addresses asked for in a loop (or at once) could pass it and be used later. Each address given on the cloud is now kept in content_image_uploads (00272, service role only) before the count; a refused one isn't kept, and ones older than a day are cleared. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks! Fixed in 7a44b2c and 600e417 (main merged forward first,
Pre-push review found a hole in my first cap, fixed in 600e417. I first counted the files that had arrived in the folder. But an address can be used later, so addresses asked for in a loop or all at once could all pass.
Delete for the organization's own folder and the cleanup on organization deletion stay as follow-ups, as you said. The PR description is updated. Checks: |
gkhngyk
left a comment
There was a problem hiding this comment.
All items done. Looks good, with one small follow-up.
- Who may upload, and how much:
prepareImageUploadhandles this now.- It checks the session and takes the organization from the member's profile.
- On the cloud, it needs an active or trialing subscription and allows 100 addresses a day.
- It records each address before counting, so the cap also holds under concurrent requests. A request passes only if the count, its own row included, is at most 100. Rows that passed are never deleted inside the window.
- A self-host has no limits, the same as the other plan limits.
- Naming: the app makes the name,
{organization_id}/{uuid}.{ext}, with the extension from an own-property lookup. The bucket has no member policies, so the folder check isn't needed.
Also checked: the signed address is valid for two hours and is signed without upsert. Each address puts up one new file and can't overwrite an existing one.
Low, can follow:
00272_content_images_storage.sql:31–41: other service-only tables (00264, 00270) also runrevoke all on table ... from anon, authenticatedandgrant all ... to service_role. RLS with no policies already refuses both roles (tested below), so this is only extra safety. 00272 is applied on prod as it is, so please add them in the next migration.
Dry-run on prod, rolled back, after 00270 and 00271:
- The bucket is created public, with a 5 MB limit and JPEG, PNG, WebP and GIF only.
- Storage had no policies before 00272 and has none after it. A member, another organization's member and anon can't insert, list, update or delete in the bucket, or change the bucket's limits.
content_image_uploads: members and anon can't insert or read rows. The service role's insert, day count and cleanup work.
Summary
Images in the Content Editor, and an author's photo, could only be added by pasting an address. Most people have the screenshot or photo on their computer, not at an address. This PR lets them upload it to Ansvisor storage.
Storage (00272)
content-images: JPEG, PNG, WebP or GIF, 5 MB at most (the bucket's own limits, enforced by Storage).{organization_id}/{uuid}.{ext}, with the extension taken from the type. Nothing is overwritten (a new name each time) or deleted from the app.Who may upload, and how much (
prepareImageUpload)subscription_status,incomplete. So the gate is a server action, not a storage policy:content_image_uploads(service role only) before the count, so addresses asked for in a loop can't pass the cap.createSignedUploadUrl, service role). The browser then sends the file straight to that address withuploadToSignedUrl, which avoids a server action's body limit, and gets back the image's public address.Upload from your computer
Known limits, for follow-ups (@gkhngyk)
Related issue
User report (tested on app.ansvisor.com): "Most people won't give an image URL; adding a photo should be possible, also for the Author."
Type of change
feat— New featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsHow to test
content_image_uploadslog.{your organization id}/.NEXT_PUBLIC_IS_CLOUD=true): with the organization'ssubscription_statusset tocanceled, the upload says it needs an active plan.The browser check (both themes, 1024 px, a real upload) is still owed, batched with the other editor fixes.
Migration order: 00272 follows 00270 (#1467) and 00271 (#1473). I'll renumber if they merge out of order.
Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/)yarn typecheckpasses (run fromweb/)yarn format:checkpasses (run fromweb/)Web:
yarn test(CHECK_TESTS) andyarn buildpass.supabase/schema.sqlrebuilt.🤖 Generated with Claude Code