Skip to content

A full-featured CRM built with React, shadcn/ui, and Supabase.

Only delete note attachments that nothing references anymore

François Zaninotto855862c7894e
diff --git a/AGENTS.md b/AGENTS.md
index f62240ef..7b6a2dcf 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -166,7 +166,7 @@ When modifying contact or company data structures:
7. Don't forget to update the related view (`contacts_summary`, `companies_summary`) in `03_views.sql`
8. Don't forget the export functions
9. Don't forget the contact merge logic
-10. If the new field stores files uploaded to the attachments bucket, add its column to the reference list in `supabase/functions/delete_note_attachments/index.ts`, otherwise these files get deleted as orphans
+10. If the new field stores files uploaded to the attachments bucket, add its column to the reference list in `supabase/functions/delete_note_attachments/index.ts`, otherwise a file it shares with a note gets deleted when that note drops it
### Running with Test Data
diff --git a/doc/src/content/docs/developers/custom-fields.mdx b/doc/src/content/docs/developers/custom-fields.mdx
index a367cf36..f5b19812 100644
--- a/doc/src/content/docs/developers/custom-fields.mdx
+++ b/doc/src/content/docs/developers/custom-fields.mdx
@@ -175,7 +175,7 @@ export const ContactAside = ({ link = "edit" }: { link?: "edit" | "show" }) => {
This will display the "Referred By" information in the contact sidebar when available.
:::caution
-If your custom field stores files (e.g. with a `<FileInput>` or `<ImageInput>` uploaded to the attachments bucket), add its column to the reference list in `supabase/functions/delete_note_attachments/index.ts`. This function regularly deletes the files of the bucket that no column references anymore, so files stored in an unlisted column can be deleted as soon as one hour after their upload.
+If your custom field stores files (e.g. with a `<FileInput>` or `<ImageInput>` uploaded to the attachments bucket), add its column to the reference list in `supabase/functions/delete_note_attachments/index.ts`. When a note attachment is removed or its note is deleted, this function deletes the file unless one of the listed columns still references it, so a file shared with an unlisted column would be deleted.
:::
![Contact Sidebar](../../images/contact-sidebar.png)
\ No newline at end of file
diff --git a/supabase/functions/delete_note_attachments/index.ts b/supabase/functions/delete_note_attachments/index.ts
index 94ec52a7..925b7151 100644
--- a/supabase/functions/delete_note_attachments/index.ts
+++ b/supabase/functions/delete_note_attachments/index.ts
@@ -7,75 +7,153 @@ import { supabaseAdmin } from "../_shared/supabaseAdmin.ts";
const ATTACHMENTS_BUCKET =
Deno.env.get("VITE_ATTACHMENTS_BUCKET") || "attachments";
-// Leaves time for a freshly uploaded file to be saved in its note, logo or avatar.
-const ORPHAN_GRACE_PERIOD = "1 hour";
-const MAX_FILES_PER_CALL = 1000;
-
-// Called by the note triggers, but the request body is ignored on purpose:
-// any authenticated user can call this endpoint, so it must never let the caller
-// pick which files to delete. Instead it removes every file of the bucket that no
-// row references anymore, which also catches files orphaned by changes the
-// triggers never see (MCP or SQL edits, lost webhook calls, replaced logos).
-const deleteOrphanAttachments = async (req: Request) => {
+const MAX_CANDIDATES_PER_CALL = 100;
+
+// The app uploads every file at the root of the bucket, named
+// `${Math.random()}${extension}`, e.g. `0.8262106278726917.pdf`.
+const UPLOADED_FILE_NAME = /^0\.[0-9]+(\.[^./]*)?$/;
+
+type NoteAttachment = {
+ path?: string | null;
+ src?: string | null;
+};
+
+type WebhookPayload = {
+ old_record?: { attachments?: NoteAttachment[] | null } | null;
+};
+
+// The note triggers call this function when a note is updated or deleted,
+// sending the note as it was before the change.
+//
+// Any logged-in user can call this endpoint with a made-up payload, so the
+// payload is not trusted: its attachments are only files we *may* delete.
+// Each of them is deleted only if no row of the database still uses it.
+// At worst, a caller can delete files that nothing uses anymore.
+const deleteNoteAttachments = async (req: Request) => {
if (req.method !== "POST") {
return jsonResponse({ error: "Method Not Allowed" }, 405);
}
+ let payload: WebhookPayload;
+ try {
+ payload = await req.json();
+ } catch {
+ return jsonResponse({ error: "Invalid JSON payload" }, 400);
+ }
+
+ const candidates = getCandidateNames(payload);
+ if (candidates.length === 0) {
+ return jsonResponse({ status: "skipped", reason: "no_candidate_files" });
+ }
+
let paths: string[];
try {
- paths = await getOrphanPaths();
+ paths = await getUnreferencedNames(candidates);
} catch (error) {
- console.error("Failed to list orphan attachments", { error });
- return jsonResponse({ error: "Failed to list orphan attachments" }, 500);
+ console.error("Failed to check note attachment references", { error });
+ return jsonResponse(
+ { error: "Failed to check note attachment references" },
+ 500,
+ );
}
if (paths.length === 0) {
- return jsonResponse({ status: "skipped", reason: "no_orphan_files" });
+ return jsonResponse({ status: "skipped", reason: "still_referenced" });
}
- const { error } = await supabaseAdmin.storage
+ const { data, error } = await supabaseAdmin.storage
.from(ATTACHMENTS_BUCKET)
.remove(paths);
if (error) {
- console.error("Failed to delete orphan attachments", { paths, error });
- return jsonResponse({ error: "Failed to delete orphan attachments" }, 500);
+ console.error("Failed to delete note attachments", { paths, error });
+ return jsonResponse({ error: "Failed to delete note attachments" }, 500);
}
- return jsonResponse({ status: "ok", deleted: paths.length });
+ return jsonResponse({ status: "ok", deleted: data.length });
};
Deno.serve(async (req: Request) =>
- AuthMiddleware(req, async (req: Request) => deleteOrphanAttachments(req)),
+ AuthMiddleware(req, async (req: Request) => deleteNoteAttachments(req)),
);
-// Uploads are named `${Math.random()}${extension}`. A file is referenced when its
-// numeric stem (`0.8262106278726917`) appears in a column that stores uploaded
-// files: the extension may be URL-encoded in a stored `src`, so it is ignored.
-// Every column holding uploaded files MUST be listed here, otherwise its files
-// get deleted. Files not named that way are never deleted.
-const getOrphanPaths = async (): Promise<string[]> => {
+const getCandidateNames = (payload: WebhookPayload): string[] => {
+ const attachments = payload?.old_record?.attachments;
+ if (!Array.isArray(attachments)) {
+ return [];
+ }
+
+ const names = attachments
+ .map(getFileName)
+ .filter((name): name is string => name != null);
+
+ return Array.from(new Set(names)).slice(0, MAX_CANDIDATES_PER_CALL);
+};
+
+// Returns the file name of an attachment, read from its `path`, or from the end
+// of its `src` URL when it has no path. Returns null for anything that does not
+// look like a file uploaded by the app (e.g. `dir/0.1.pdf` or `../logo.png`),
+// so the caller cannot point at other files of the bucket.
+const getFileName = (attachment?: NoteAttachment | null) => {
+ const name =
+ typeof attachment?.path === "string"
+ ? attachment.path
+ : getLastPathSegment(attachment?.src);
+ return name != null && UPLOADED_FILE_NAME.test(name) ? name : null;
+};
+
+const getLastPathSegment = (src?: string | null) => {
+ if (typeof src !== "string") {
+ return null;
+ }
+ try {
+ const pathname = new URL(src).pathname;
+ return safelyDecodePath(pathname.slice(pathname.lastIndexOf("/") + 1));
+ } catch {
+ return null;
+ }
+};
+
+const safelyDecodePath = (path: string) => {
+ try {
+ return decodeURIComponent(path);
+ } catch {
+ return path;
+ }
+};
+
+// Returns the files that no row uses anymore, among the given ones.
+//
+// A file counts as used when its number (`0.8262106278726917` for
+// `0.8262106278726917.pdf`) appears anywhere in one of the columns listed in
+// `refs`. We search for the number only, because the extension may be
+// URL-encoded in a stored URL.
+//
+// This runs after the note change is saved, so the edited note is checked with
+// its new attachments: a file the note still has is kept.
+//
+// Every column that can hold uploaded files MUST be listed in `refs`. Otherwise,
+// a file used both by a note and by that column gets deleted when the note
+// drops it.
+const getUnreferencedNames = async (names: string[]): Promise<string[]> => {
const { rows } = await sql<{ name: string }>`
- with refs as (
+ with candidates as (
+ select name, substring(name from '^0\\.[0-9]+') as stem
+ from jsonb_array_elements_text(${JSON.stringify(names)}::jsonb) as name
+ ),
+ refs as (
select attachments::text as text from public.contact_notes
union all select attachments::text from public.deal_notes
union all select logo::text from public.companies
union all select avatar::text from public.contacts
union all select avatar::text from public.sales
union all select config::text from public.configuration
- ),
- referenced_stems as (
- select distinct match[1] as stem
- from refs, regexp_matches(refs.text, '(0\\.[0-9]+)', 'g') as match
)
- select o.name
- from storage.objects o
- where o.bucket_id = ${ATTACHMENTS_BUCKET}
- and o.created_at < now() - ${ORPHAN_GRACE_PERIOD}::interval
- and substring(o.name from '^0\\.[0-9]+') not in (
- select stem from referenced_stems
- )
- limit ${MAX_FILES_PER_CALL}
+ select c.name
+ from candidates c
+ where not exists (
+ select 1 from refs where strpos(refs.text, c.stem) > 0
+ )
`.execute(db);
return rows.map((row) => row.name);