Delete attachments on field clear

This commit is contained in:
Mel O'Hagan 2022-08-12 11:29:57 +01:00
parent f15406794f
commit 404e5414b5
6 changed files with 41 additions and 1 deletions

View File

@ -17,6 +17,7 @@
export let disabled = false
export let fileSizeLimit = BYTES_IN_MB * 20
export let processFiles = null
export let deleteAttachments = null
export let handleFileTooLarge = null
export let handleTooManyFiles = null
export let gallery = true
@ -95,6 +96,7 @@
value.filter((x, idx) => idx !== selectedImageIdx)
)
selectedImageIdx = 0
await deleteAttachments(value.map(item => item.key))
}
function navigateLeft() {

View File

@ -47,6 +47,17 @@
}
}
const deleteAttachments = async fileList => {
try {
return await API.deleteAttachments({
keys: fileList,
tableId: formContext?.dataSource?.tableId,
})
} catch (error) {
return []
}
}
const handleChange = e => {
fieldApi.setValue(e.detail)
if (onChange) {
@ -72,6 +83,7 @@
error={fieldState.error}
on:change={handleChange}
{processFiles}
{deleteAttachments}
{handleFileTooLarge}
{handleTooManyFiles}
{maximum}

View File

@ -61,5 +61,19 @@ export const buildAttachmentEndpoints = API => {
})
return { publicUrl }
},
/**
* Deletes attachments from the bucket.
* @param keys the attachments to delete
* @param tableId the associated table ID
*/
deleteAttachments: async ({ keys, tableId }) => {
return await API.post({
url: `/api/attachments/${tableId}/delete`,
body: {
keys,
},
})
},
}
}

View File

@ -12,7 +12,7 @@ const {
} = require("../../../utilities/fileSystem")
const env = require("../../../environment")
const { clientLibraryPath } = require("../../../utilities")
const { upload } = require("../../../utilities/fileSystem")
const { upload, deleteFiles } = require("../../../utilities/fileSystem")
const { attachmentsRelativeURL } = require("../../../utilities")
const { DocumentType } = require("../../../db/utils")
const { getAppDB, getAppId } = require("@budibase/backend-core/context")
@ -97,6 +97,10 @@ export const uploadFile = async function (ctx: any) {
ctx.body = await Promise.all(uploads)
}
export const deleteObjects = async function (ctx: any) {
ctx.body = await deleteFiles(ObjectStoreBuckets.APPS, ctx.request.body.keys)
}
export const serveApp = async function (ctx: any) {
const db = getAppDB({ skip_setup: true })
const appInfo = await db.get(DocumentType.APP_METADATA)

View File

@ -45,6 +45,12 @@ router
authorized(PermissionTypes.TABLE, PermissionLevels.WRITE),
controller.uploadFile
)
.post(
"/api/attachments/:tableId/delete",
paramResource("tableId"),
authorized(PermissionTypes.TABLE, PermissionLevels.WRITE),
controller.deleteObjects
)
.get("/:appId/:path*", controller.serveApp)
.get("/app/:appUrl/:path*", controller.serveApp)
.post(

View File

@ -15,6 +15,7 @@ const {
streamUpload,
deleteFolder,
downloadTarball,
deleteFiles,
} = require("./utilities")
const { updateClientLibrary } = require("./clientLibrary")
const env = require("../../environment")
@ -327,5 +328,6 @@ exports.cleanup = appIds => {
exports.upload = upload
exports.retrieve = retrieve
exports.retrieveToTmp = retrieveToTmp
exports.deleteFiles = deleteFiles
exports.TOP_LEVEL_PATH = TOP_LEVEL_PATH
exports.NODE_MODULES_PATH = NODE_MODULES_PATH