improved "upload asset":

- input workspace_id is not required anymore
- two new options: `Replace existing file` and `Append to column`
This commit is contained in:
Christoph Dyllick-Brenzinger 2024-01-08 23:17:21 +01:00
parent 9765219002
commit 95b66a0226
2 changed files with 68 additions and 34 deletions

View file

@ -55,25 +55,6 @@ export const assetUploadDescription: AssetProperties = [
},
default: '',
},
{
displayName: 'Workspace ID',
name: 'workspaceId',
type: 'number',
typeOptions: {
minValue: 1,
numberStepSize: 1,
},
required: true,
displayOptions: {
show: {
resource: ['asset'],
operation: ['upload'],
},
},
default: '',
description:
'How to get the workspace ID: https://seatable.io/docs/arbeiten-mit-gruppen/workspace-id-einer-gruppe-ermitteln/?lang=auto',
},
{
displayName: 'Property Name',
name: 'dataPropertyName',
@ -88,17 +69,32 @@ export const assetUploadDescription: AssetProperties = [
},
description: 'Name of the binary property which contains the data for the file to be written',
},
/*{
displayName: 'Replace',
{
displayName: 'Replace existing file',
name: 'replace',
type: 'boolean',
default: false,
default: true,
displayOptions: {
show: {
resource: ['asset'],
operation: ['upload'],
},
},
description: 'Replace existing file if the file/image already exists with same name.',
},*/
description:
'Replace existing asset with the same name. Otherwise a new version with another name (numeral in parentheses) will be uploaded.',
},
{
displayName: 'Append to column',
name: 'append',
type: 'boolean',
default: true,
displayOptions: {
show: {
resource: ['asset'],
operation: ['upload'],
},
},
description:
'Keep existing files/images in the column and append the new asset. Otherwise the existing files/images are remove from the column.',
},
];

View file

@ -6,13 +6,12 @@ export async function upload(
this: IExecuteFunctions,
index: number,
): Promise<INodeExecutionData[]> {
// step 1: upload file to base
const uploadColumn = this.getNodeParameter('uploadColumn', index) as any;
const uploadColumnType = uploadColumn.split(':::')[1];
const uploadColumnName = uploadColumn.split(':::')[0];
const dataPropertyName = this.getNodeParameter('dataPropertyName', index) as string;
const tableName = this.getNodeParameter('tableName', index) as string;
const rowId = this.getNodeParameter('rowId', index) as string;
const workspaceId = this.getNodeParameter('workspaceId', index) as string;
const uploadLink = (await seaTableApiRequest.call(
this,
{},
@ -21,11 +20,43 @@ export async function upload(
)) as IUploadLink;
const relativePath =
uploadColumnType === 'image' ? uploadLink.img_relative_path : uploadLink.file_relative_path;
const replace = this.getNodeParameter('replace', index) as string;
const append = this.getNodeParameter('append', index) as string;
// Get the binary data
// get server url
const credentials = await this.getCredentials('seaTableApi');
const serverURL = credentials.domain ?? 'https://cloud.seatable.io';
// get workspaceId
const workspaceId = (
await this.helpers.request({
headers: {
Authorization: `Token ${credentials.token}`,
},
uri: `${serverURL}/api/v2.1/dtable/app-access-token/`,
json: true,
})
).workspace_id;
// if there are already assets attached to the column
let existingAssetArray = [];
if (append) {
let rowToUpdate = await seaTableApiRequest.call(
this,
{},
'GET',
'/dtable-server/api/v1/dtables/{{dtable_uuid}}/rows/' + rowId,
{},
{
table_name: tableName,
},
);
existingAssetArray = rowToUpdate[uploadColumnName];
}
// Get the binary data and prepare asset for upload
const fileBufferData = await this.helpers.getBinaryDataBuffer(index, dataPropertyName);
const binaryData = this.helpers.assertBinaryData(index, dataPropertyName);
// Create our request option
const options = {
formData: {
file: {
@ -36,12 +67,12 @@ export async function upload(
},
},
parent_dir: uploadLink.parent_path,
replace: '0',
replace: replace ? '1' : '0',
relative_path: relativePath,
},
};
// Send the request
// Send the upload request
let uploadAsset = await seaTableApiRequest.call(
this,
{},
@ -53,7 +84,7 @@ export async function upload(
options,
);
// now step 2 (attaching the file to a column in a base)
// now step 2 (attaching the asset to a column in a base)
for (let c = 0; c < uploadAsset.length; c++) {
const body = {
table_name: tableName,
@ -62,17 +93,24 @@ export async function upload(
} as IDataObject;
let rowInput = {} as IRowObject;
const filePath = `/workspace/${workspaceId}${uploadLink.parent_path}/${relativePath}/${uploadAsset[c].name}`;
const filePath = `${serverURL}/workspace/${workspaceId}${uploadLink.parent_path}/${relativePath}/${uploadAsset[c].name}`;
if (uploadColumnType === 'image') {
rowInput[uploadColumn.split(':::')[0]] = [filePath];
rowInput[uploadColumnName] = [filePath];
} else if (uploadColumnType === 'file') {
rowInput[uploadColumn.split(':::')[0]] = uploadAsset;
rowInput[uploadColumnName] = uploadAsset;
uploadAsset[c].type = 'file';
uploadAsset[c].url = filePath;
}
// merge with existing assets in this column or with [] and remove duplicates
rowInput[uploadColumnName] = [
// @ts-ignore:
...new Set([...rowInput[uploadColumnName], ...existingAssetArray]),
];
body.row = rowInput;
// attach assets to table row
const responseData = await seaTableApiRequest.call(
this,
{},