1
0
Fork 0
mirror of synced 2024-07-02 13:01:09 +12:00

Handle empty attachments

This commit is contained in:
Adria Navarro 2024-04-02 22:12:09 +02:00
parent ac5d578349
commit 151ea235f1
3 changed files with 15 additions and 5 deletions

View file

@ -406,7 +406,12 @@ const downloadFileHandler = async action => {
const { type } = action.parameters
if (type === "attachment") {
const { tableId, rowId, attachmentColumn } = action.parameters
const res = await API.downloadAttachment(tableId, rowId, attachmentColumn)
const res = await API.downloadAttachment(
tableId,
rowId,
attachmentColumn,
{ suppressErrors: true }
)
await downloadStream(res)
return
}
@ -429,7 +434,11 @@ const downloadFileHandler = async action => {
URL.revokeObjectURL(objectUrl)
} catch (e) {
console.error(e)
notificationStore.actions.error("File cannot be downloaded")
if (e.status === 404) {
notificationStore.actions.error("File is empty")
} else {
notificationStore.actions.error("File cannot be downloaded")
}
}
}

View file

@ -95,10 +95,11 @@ export const buildAttachmentEndpoints = API => {
* @param rowId
* @param columnName the attachments to delete
*/
downloadAttachment: async (tableId, rowId, columnName) => {
downloadAttachment: async (tableId, rowId, columnName, options) => {
return await API.get({
url: `/api/${tableId}/rows/${rowId}/attachment/${columnName}`,
parseResponse: response => response,
suppressErrors: options?.suppressErrors,
})
},
}

View file

@ -263,7 +263,8 @@ export async function downloadAttachment(ctx: UserCtx) {
const tableId = utils.getTableId(ctx)
const row = await pickApi(tableId).find(ctx)
if (!row[columnName]) {
const table = await sdk.tables.getTable(tableId)
if (!table.schema[columnName]) {
ctx.throw(400, `'${columnName}' is not valid`)
}
@ -293,7 +294,6 @@ export async function downloadAttachment(ctx: UserCtx) {
archive.append(attachmentStream, { name: attachment.name })
}
const table = await sdk.tables.getTable(tableId)
const displayName = row[table.primaryDisplay || "_id"]
ctx.attachment(`${displayName}_${columnName}.zip`)
archive.finalize()