Handle single files on imports

This commit is contained in:
Adria Navarro 2024-04-04 21:05:56 +02:00
parent e5843929f1
commit 6928e7454c
3 changed files with 28 additions and 6 deletions

View File

@ -5,6 +5,7 @@ import {
Automation, Automation,
AutomationTriggerStepId, AutomationTriggerStepId,
RowAttachment, RowAttachment,
FieldType,
} from "@budibase/types" } from "@budibase/types"
import { getAutomationParams } from "../../../db/utils" import { getAutomationParams } from "../../../db/utils"
import { budibaseTempDir } from "../../../utilities/budibaseDir" import { budibaseTempDir } from "../../../utilities/budibaseDir"
@ -58,10 +59,19 @@ export async function updateAttachmentColumns(prodAppId: string, db: Database) {
updatedRows = updatedRows.concat( updatedRows = updatedRows.concat(
rows.map(row => { rows.map(row => {
for (let column of columns) { for (let column of columns) {
if (Array.isArray(row[column])) { const columnType = table.schema[column].type
if (
columnType === FieldType.ATTACHMENTS &&
Array.isArray(row[column])
) {
row[column] = row[column].map((attachment: RowAttachment) => row[column] = row[column].map((attachment: RowAttachment) =>
rewriteAttachmentUrl(prodAppId, attachment) rewriteAttachmentUrl(prodAppId, attachment)
) )
} else if (
columnType === FieldType.ATTACHMENT_SINGLE &&
row[column]
) {
row[column] = rewriteAttachmentUrl(prodAppId, row[column])
} }
} }
return row return row

View File

@ -30,7 +30,10 @@ export async function getRowsWithAttachments(appId: string, table: Table) {
const db = dbCore.getDB(appId) const db = dbCore.getDB(appId)
const attachmentCols: string[] = [] const attachmentCols: string[] = []
for (let [key, column] of Object.entries(table.schema)) { for (let [key, column] of Object.entries(table.schema)) {
if (column.type === FieldType.ATTACHMENTS) { if (
column.type === FieldType.ATTACHMENTS ||
column.type === FieldType.ATTACHMENT_SINGLE
) {
attachmentCols.push(key) attachmentCols.push(key)
} }
} }

View File

@ -31,9 +31,13 @@ describe("should be able to re-write attachment URLs", () => {
sourceType: TableSourceType.INTERNAL, sourceType: TableSourceType.INTERNAL,
schema: { schema: {
photo: { photo: {
type: FieldType.ATTACHMENTS, type: FieldType.ATTACHMENT_SINGLE,
name: "photo", name: "photo",
}, },
gallery: {
type: FieldType.ATTACHMENTS,
name: "gallery",
},
otherCol: { otherCol: {
type: FieldType.STRING, type: FieldType.STRING,
name: "otherCol", name: "otherCol",
@ -43,7 +47,8 @@ describe("should be able to re-write attachment URLs", () => {
for (let i = 0; i < FIND_LIMIT * 4; i++) { for (let i = 0; i < FIND_LIMIT * 4; i++) {
await config.api.row.save(table._id!, { await config.api.row.save(table._id!, {
photo: [attachment], photo: { ...attachment },
gallery: [{ ...attachment }, { ...attachment }],
otherCol: "string", otherCol: "string",
}) })
} }
@ -56,8 +61,12 @@ describe("should be able to re-write attachment URLs", () => {
) )
for (const row of rows) { for (const row of rows) {
expect(row.otherCol).toBe("string") expect(row.otherCol).toBe("string")
expect(row.photo[0].url).toBe("") expect(row.photo.url).toBe("")
expect(row.photo[0].key).toBe(`${db.name}/attachments/a.png`) expect(row.photo.key).toBe(`${db.name}/attachments/a.png`)
expect(row.gallery[0].url).toBe("")
expect(row.gallery[0].key).toBe(`${db.name}/attachments/a.png`)
expect(row.gallery[1].url).toBe("")
expect(row.gallery[1].key).toBe(`${db.name}/attachments/a.png`)
} }
}) })
}) })