Download attachment api (returns only first for now)

This commit is contained in:
Adria Navarro 2024-04-02 11:13:36 +02:00
parent 94eefb8007
commit d91da45880
2 changed files with 26 additions and 0 deletions

View File

@ -1,4 +1,5 @@
import { quotas } from "@budibase/pro"
import { objectStore } from "@budibase/backend-core"
import * as internal from "./internal"
import * as external from "./external"
import { isExternalTableID } from "../../../integrations/utils"
@ -13,6 +14,7 @@ import {
PatchRowRequest,
PatchRowResponse,
Row,
RowAttachment,
SearchParams,
SearchRowRequest,
SearchRowResponse,
@ -251,3 +253,21 @@ export const exportRows = async (
ctx.attachment(fileName)
ctx.body = apiFileReturn(content)
}
export async function downloadAttachment(ctx: UserCtx) {
const { columnName } = ctx.params
const tableId = utils.getTableId(ctx)
const row = await pickApi(tableId).find(ctx)
if (!row[columnName]) {
ctx.throw(400, `'${columnName}' is not valid`)
}
const attachment: RowAttachment = row[columnName][0]
ctx.attachment(attachment.name)
ctx.body = await objectStore.getReadStream(
objectStore.ObjectStoreBuckets.APPS,
attachment.key
)
}

View File

@ -77,6 +77,12 @@ router
authorized(PermissionType.TABLE, PermissionLevel.WRITE),
rowController.exportRows
)
.get(
"/api/:sourceId/rows/:rowId/attachment/:columnName",
paramSubResource("sourceId", "rowId"),
authorized(PermissionType.TABLE, PermissionLevel.READ),
rowController.downloadAttachment
)
router.post(
"/api/v2/views/:viewId/search",