Browse Source

feat(nocodb): ncAttachmentUpgrader

pull/4931/head
Wing-Kam Wong 2 years ago
parent
commit
ea0083a991
  1. 53
      packages/nocodb/src/lib/version-upgrader/ncAttachmentUpgrader.ts

53
packages/nocodb/src/lib/version-upgrader/ncAttachmentUpgrader.ts

@ -16,12 +16,13 @@ import { UITypes } from 'nocodb-sdk';
// in this way, if the base url is changed, the url will be broken // in this way, if the base url is changed, the url will be broken
// this upgrader is to convert the existing local attachment object to the following format // this upgrader is to convert the existing local attachment object to the following format
// [{ // [{
// "url": "download/noco/xcdb/Sheet-1/title5/39A410.jpeg", // "url": "http://localhost:8080/download/noco/xcdb/Sheet-1/title5/39A410.jpeg",
// "path": "download/noco/xcdb/Sheet-1/title5/39A410.jpeg",
// "title": "foo.jpeg", // "title": "foo.jpeg",
// "mimetype": "image/jpeg", // "mimetype": "image/jpeg",
// "size": 6494 // "size": 6494
// }] // }]
// the url will be constructed by `${ncSiteUrl}/${path}` in UI. // the new url will be constructed by `${ncSiteUrl}/${path}` in UI. the old url will be used for fallback
// while other non-local attachments will remain unchanged // while other non-local attachments will remain unchanged
function getTnPath(knex: XKnex, tb: Model) { function getTnPath(knex: XKnex, tb: Model) {
@ -60,30 +61,44 @@ export default async function ({ ncMeta }: NcUpgraderCtx) {
...attachmentColumns, ...attachmentColumns,
]); ]);
for (const record of records) { for (const record of records) {
console.log(record);
for (const attachmentColumn of attachmentColumns) { for (const attachmentColumn of attachmentColumns) {
let attachmentMeta = let attachmentMeta =
typeof record[attachmentColumn] === 'string' typeof record[attachmentColumn] === 'string'
? JSON.parse(record[attachmentColumn]) ? JSON.parse(record[attachmentColumn])
: record[attachmentColumn]; : record[attachmentColumn];
if (attachmentMeta) { if (attachmentMeta) {
// TODO: check if it is local attachment const newAttachmentMeta = [];
if ('url' in attachmentMeta) { for (const attachment of attachmentMeta) {
const ncSiteUrl = 'TODO'; if ('url' in attachment) {
attachmentMeta.url = attachmentMeta.url.split(ncSiteUrl)[1]; const match = attachment.url.match(/^(.*)\/download\/(.*)$/);
console.log( if (match) {
'update', // e.g. http://localhost:8080/download/noco/xcdb/Sheet-1/title5/ee2G8p_nute_gunray.png
knex(getTnPath(knex, model)) // match[1] = http://localhost:8080
.update({ meta: attachmentMeta }) // match[2] = download/noco/xcdb/Sheet-1/title5/ee2G8p_nute_gunray.png
.where(primaryKeys.map((pk) => ({ [pk]: record[pk] }))) const path = `download/${match[2]}`;
.toQuery()
); newAttachmentMeta.push({
updateRecords.push( ...attachment,
await knex(getTnPath(knex, model)) path,
.update({ meta: attachmentMeta }) });
.where(primaryKeys.map((pk) => ({ [pk]: record[pk] }))) } else {
); // keep it as it is
newAttachmentMeta.push(attachment);
}
}
} }
const where = primaryKeys
.map((key) => {
return { [key]: record[key] };
})
.reduce((acc, val) => Object.assign(acc, val), {});
updateRecords.push(
await knex(getTnPath(knex, model))
.update({
[attachmentColumn]: JSON.stringify(newAttachmentMeta),
})
.where(where)
);
} }
} }
} }

Loading…
Cancel
Save