Skip to content

feat(content-editor): upload an image or an author's photo instead of pasting its address (00272) - #1474

Merged
gkhngyk merged 5 commits into
ansvisor:mainfrom
cihan-gyk:feature/content-image-upload
Oct 25, 2026
Merged

gkhngyk merged 5 commits into
ansvisor:mainfrom
cihan-gyk:feature/content-image-upload

Conversation

@cihan-gyk

@cihan-gyk cihan-gyk commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator

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)

  • A public bucket, content-images: JPEG, PNG, WebP or GIF, 5 MB at most (the bucket's own limits, enforced by Storage).
  • Each organization's images go under its own folder: {organization_id}/{uuid}.{ext}, with the extension taken from the type. Nothing is overwritten (a new name each time) or deleted from the app.
  • The images are public by address, as on any published page. The page and the CMS show them by it.
  • No member policies on the bucket. Nothing reaches it except through a one-time upload address that the app gives (see below).

Who may upload, and how much (prepareImageUpload)

  • Only the app knows the managed cloud from a self-host. The database can't tell them apart: a self-hosted organization keeps the default subscription_status, incomplete. So the gate is a server action, not a storage policy:
    • It checks the session and the member's organization.
    • On the cloud, the organization needs an active or trialing subscription (the dashboard's gate), and gets 100 upload addresses a day. Each address given is kept in content_image_uploads (service role only) before the count, so addresses asked for in a loop can't pass the cap.
    • A self-host has no limits, as with the other plan limits.
  • It makes the name and a one-time upload address (createSignedUploadUrl, service role). The browser then sends the file straight to that address with uploadToSignedUrl, which avoids a server action's body limit, and gets back the image's public address.
  • Tested end to end on the dev project:
    • an upload through the one-time address goes up and reads back publicly;
    • a direct upload is refused;
    • an SVG is refused by the bucket's types.
    • The test file was removed afterwards.
    • The upload log's insert, count and cleanup work, and anon sees no rows.

Upload from your computer

  • The address the upload gives back fills the field beside the button.
  • Wired into:
    • the editor toolbar's Insert an image dialog, under the address field, with a hint "JPG, PNG, WebP or GIF, up to 5 MB…";
    • Settings › Authors, the author's photo.
  • The visual proof dialog (feat(content-editor): show visual proof spots in the draft's margin, with Add image (CE-049, part c) #1469) gets the same button once both are in.
  • A wrong type or a file over 5 MB is refused before sending, with a clear message. So are a lapsed plan ("Uploads come with an active plan…") and a day's uploads made. A failed upload says so, and the same file can be picked again.

Known limits, for follow-ups (@gkhngyk)

  • Deleting: nothing deletes an uploaded image yet. There is no delete policy and no cleanup when an organization is deleted. Author photos are personal data, so a follow-up should add both (a delete for the organization's own folder, and a service-role cleanup on deletion).
  • Quota: the daily cap counts uploads, not bytes. A per-plan storage quota can follow if needed.
  • http self-hosts: a self-host whose Supabase runs on plain http gets an http address, and the author photo field still accepts https only, by the form, its schema and 00256's check.
  • Closing mid-upload: closing a dialog during an upload leaves the file unused in the folder.

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 feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

  1. Apply 00272. It creates the bucket and the content_image_uploads log.
  2. In the editor, open the toolbar's image dialog, click Upload from your computer and pick a PNG. The address fills in; Insert puts the image in the page.
  3. Try a PDF, or an image over 5 MB: it says why, and nothing goes up.
  4. In Settings › Authors, edit an author and upload a photo. Save, and the photo shows.
  5. In Storage, the file is under {your organization id}/.
  6. Cloud (NEXT_PUBLIC_IS_CLOUD=true): with the organization's subscription_status set to canceled, 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

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

Web: yarn test (CHECK_TESTS) and yarn build pass. supabase/schema.sql rebuilt.

🤖 Generated with Claude Code

… 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]>
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 24, 2026

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few fixes before merge:

  1. 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 definer helper 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.

  2. 00272_content_images_storage.sql:33 and :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 names image-upload.ts builds: randomUUID and 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.

cihan-gyk and others added 3 commits October 25, 2026 00:14
…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]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

Thanks! Fixed in 7a44b2c and 600e417 (main merged forward first, schema.sql rebuilt). One change from what you suggested, and why:

  1. Who may upload, and how much, is now the app's gate, not a storage policy.
    • The database can't tell the managed cloud from a self-host. A self-hosted organization keeps the default subscription_status (incomplete), and isCloud() lives only in the app's env. A policy on active/trialing would have shut self-hosts out of uploads.
    • So a server action, prepareImageUpload, decides:
      • it checks the session and the member's organization;
      • on the cloud, it needs an active or trialing subscription (the dashboard's gate, isSubscriptionActive) and allows 100 upload addresses per organization a day;
      • a self-host has no limits, as with the other plan limits.
    • The action then makes a one-time upload address with the service role (createSignedUploadUrl). The browser sends the file straight to it with uploadToSignedUrl, so there's still no server action body limit.
    • The upload says why it can't go: "Uploads come with an active plan…", or the day's uploads made.
  2. The name is the app's: {organization_id}/{uuid}.{ext}, with the extension from the type (an own-property lookup, so constructor isn't a type). The bucket now has no member policies at all: nothing reaches it except through that address. That's tighter than the name regex, so 00272 only creates the bucket.
    • Tested end to end on the dev project:
      • an upload through the one-time address goes up and reads back publicly (200, image/png);
      • a direct upload with the anon key is refused;
      • an SVG through a one-time address is refused by the bucket's types;
    • I removed the test file afterwards and dropped the old draft's two policies from the dev DB.
    • Tests cover the action: the types, the plan on the cloud, the daily cap with uploads older than a day left out, no limits on a self-host, and the failures. They also cover the client: the ticket, then the upload, and each error.

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.

  • Each address given on the cloud is now kept in content_image_uploads (00272: service role only, RLS on, no policies) before the count. A refused one isn't kept, and the ones older than a day are cleared.
  • Inserting before counting closes both the loop and the race.
  • Tested on dev: the insert, the count and the cleanup work, and anon sees no rows. The probe row was removed.
  • The action's tests use the real isSubscriptionActive.

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: yarn format, lint (0 errors), typecheck, test (3,625 passed) and build pass on 7a44b2c. After 600e417, I re-ran lint, typecheck, build and the upload tests (26 passed).

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All items done. Looks good, with one small follow-up.

  1. Who may upload, and how much: prepareImageUpload handles 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.
  2. 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:

  1. 00272_content_images_storage.sql:31–41: other service-only tables (00264, 00270) also run revoke all on table ... from anon, authenticated and grant 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.

@gkhngyk
gkhngyk merged commit 7c6f904 into ansvisor:main Oct 25, 2026
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-migration Adds or edits a migration — apply it to the hosted database

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants