diff --git a/packages/server/src/api/controllers/row/internal.ts b/packages/server/src/api/controllers/row/internal.ts index 1adeb71341..a251724b0a 100644 --- a/packages/server/src/api/controllers/row/internal.ts +++ b/packages/server/src/api/controllers/row/internal.ts @@ -79,7 +79,7 @@ export async function patch(ctx: UserCtx) { table, })) as Row // check if any attachments removed - await AttachmentCleanup.rowUpdate(table, row, oldRow) + await AttachmentCleanup.rowUpdate(table, { row, oldRow }) if (isUserTable) { // the row has been updated, need to put it into the ctx diff --git a/packages/server/src/utilities/rowProcessor/attachments.ts b/packages/server/src/utilities/rowProcessor/attachments.ts index cc32be0c2f..3b670cbc05 100644 --- a/packages/server/src/utilities/rowProcessor/attachments.ts +++ b/packages/server/src/utilities/rowProcessor/attachments.ts @@ -50,9 +50,6 @@ export class AttachmentCleanup { return AttachmentCleanup.tableChange(table, rows, { deleting: true }) } - /** - * Cleanup attachments when updating a table. - */ static async tableUpdate( table: Table, rows: Row[], @@ -61,9 +58,6 @@ export class AttachmentCleanup { return AttachmentCleanup.tableChange(table, rows, opts) } - /** - * Cleanup attachments when deleting rows. - */ static async rowDelete(table: Table, rows: Row[]) { return AttachmentCleanup.coreCleanup(() => { let files: string[] = [] @@ -81,10 +75,7 @@ export class AttachmentCleanup { }) } - /** - * Remove attachments when updating a row, if new row doesn't have the attachments. - */ - static rowUpdate(table: Table, row: Row, oldRow: Row) { + static rowUpdate(table: Table, opts: { row: Row; oldRow: Row }) { return AttachmentCleanup.coreCleanup(() => { let files: string[] = [] for (let [key, schema] of Object.entries(table.schema)) { @@ -92,9 +83,12 @@ export class AttachmentCleanup { continue } const oldKeys = - oldRow[key]?.map((attachment: RowAttachment) => attachment.key) || [] + opts.oldRow[key]?.map( + (attachment: RowAttachment) => attachment.key + ) || [] const newKeys = - row[key]?.map((attachment: RowAttachment) => attachment.key) || [] + opts.row[key]?.map((attachment: RowAttachment) => attachment.key) || + [] files = files.concat( oldKeys.filter((key: string) => newKeys.indexOf(key) === -1) ) diff --git a/packages/server/src/utilities/rowProcessor/tests/attachments.spec.ts b/packages/server/src/utilities/rowProcessor/tests/attachments.spec.ts index 024f140960..762ec3bb8c 100644 --- a/packages/server/src/utilities/rowProcessor/tests/attachments.spec.ts +++ b/packages/server/src/utilities/rowProcessor/tests/attachments.spec.ts @@ -91,7 +91,10 @@ describe("attachment cleanup", () => { it("should handle row updates", async () => { const updatedRow = row() delete updatedRow.attach - await AttachmentCleanup.rowUpdate(table(), updatedRow, row()) + await AttachmentCleanup.rowUpdate(table(), { + row: updatedRow, + oldRow: row(), + }) expect(mockedDeleteFiles).toBeCalledWith(BUCKET, [FILE_NAME]) }) @@ -101,7 +104,7 @@ describe("attachment cleanup", () => { }) it("shouldn't cleanup attachments if row not updated", async () => { - await AttachmentCleanup.rowUpdate(table(), row(), row()) + await AttachmentCleanup.rowUpdate(table(), { row: row(), oldRow: row() }) expect(mockedDeleteFiles).not.toBeCalled() }) })