mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-15 17:14:05 -08:00
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:
parent
9765219002
commit
95b66a0226
|
@ -55,25 +55,6 @@ export const assetUploadDescription: AssetProperties = [
|
||||||
},
|
},
|
||||||
default: '',
|
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',
|
displayName: 'Property Name',
|
||||||
name: 'dataPropertyName',
|
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',
|
description: 'Name of the binary property which contains the data for the file to be written',
|
||||||
},
|
},
|
||||||
/*{
|
{
|
||||||
displayName: 'Replace',
|
displayName: 'Replace existing file',
|
||||||
name: 'replace',
|
name: 'replace',
|
||||||
type: 'boolean',
|
type: 'boolean',
|
||||||
default: false,
|
default: true,
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
resource: ['asset'],
|
resource: ['asset'],
|
||||||
operation: ['upload'],
|
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.',
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
|
@ -6,13 +6,12 @@ export async function upload(
|
||||||
this: IExecuteFunctions,
|
this: IExecuteFunctions,
|
||||||
index: number,
|
index: number,
|
||||||
): Promise<INodeExecutionData[]> {
|
): Promise<INodeExecutionData[]> {
|
||||||
// step 1: upload file to base
|
|
||||||
const uploadColumn = this.getNodeParameter('uploadColumn', index) as any;
|
const uploadColumn = this.getNodeParameter('uploadColumn', index) as any;
|
||||||
const uploadColumnType = uploadColumn.split(':::')[1];
|
const uploadColumnType = uploadColumn.split(':::')[1];
|
||||||
|
const uploadColumnName = uploadColumn.split(':::')[0];
|
||||||
const dataPropertyName = this.getNodeParameter('dataPropertyName', index) as string;
|
const dataPropertyName = this.getNodeParameter('dataPropertyName', index) as string;
|
||||||
const tableName = this.getNodeParameter('tableName', index) as string;
|
const tableName = this.getNodeParameter('tableName', index) as string;
|
||||||
const rowId = this.getNodeParameter('rowId', index) as string;
|
const rowId = this.getNodeParameter('rowId', index) as string;
|
||||||
const workspaceId = this.getNodeParameter('workspaceId', index) as string;
|
|
||||||
const uploadLink = (await seaTableApiRequest.call(
|
const uploadLink = (await seaTableApiRequest.call(
|
||||||
this,
|
this,
|
||||||
{},
|
{},
|
||||||
|
@ -21,11 +20,43 @@ export async function upload(
|
||||||
)) as IUploadLink;
|
)) as IUploadLink;
|
||||||
const relativePath =
|
const relativePath =
|
||||||
uploadColumnType === 'image' ? uploadLink.img_relative_path : uploadLink.file_relative_path;
|
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 fileBufferData = await this.helpers.getBinaryDataBuffer(index, dataPropertyName);
|
||||||
const binaryData = this.helpers.assertBinaryData(index, dataPropertyName);
|
const binaryData = this.helpers.assertBinaryData(index, dataPropertyName);
|
||||||
// Create our request option
|
|
||||||
const options = {
|
const options = {
|
||||||
formData: {
|
formData: {
|
||||||
file: {
|
file: {
|
||||||
|
@ -36,12 +67,12 @@ export async function upload(
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
parent_dir: uploadLink.parent_path,
|
parent_dir: uploadLink.parent_path,
|
||||||
replace: '0',
|
replace: replace ? '1' : '0',
|
||||||
relative_path: relativePath,
|
relative_path: relativePath,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
// Send the request
|
// Send the upload request
|
||||||
let uploadAsset = await seaTableApiRequest.call(
|
let uploadAsset = await seaTableApiRequest.call(
|
||||||
this,
|
this,
|
||||||
{},
|
{},
|
||||||
|
@ -53,7 +84,7 @@ export async function upload(
|
||||||
options,
|
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++) {
|
for (let c = 0; c < uploadAsset.length; c++) {
|
||||||
const body = {
|
const body = {
|
||||||
table_name: tableName,
|
table_name: tableName,
|
||||||
|
@ -62,17 +93,24 @@ export async function upload(
|
||||||
} as IDataObject;
|
} as IDataObject;
|
||||||
let rowInput = {} as IRowObject;
|
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') {
|
if (uploadColumnType === 'image') {
|
||||||
rowInput[uploadColumn.split(':::')[0]] = [filePath];
|
rowInput[uploadColumnName] = [filePath];
|
||||||
} else if (uploadColumnType === 'file') {
|
} else if (uploadColumnType === 'file') {
|
||||||
rowInput[uploadColumn.split(':::')[0]] = uploadAsset;
|
rowInput[uploadColumnName] = uploadAsset;
|
||||||
uploadAsset[c].type = 'file';
|
uploadAsset[c].type = 'file';
|
||||||
uploadAsset[c].url = filePath;
|
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;
|
body.row = rowInput;
|
||||||
|
|
||||||
|
// attach assets to table row
|
||||||
const responseData = await seaTableApiRequest.call(
|
const responseData = await seaTableApiRequest.call(
|
||||||
this,
|
this,
|
||||||
{},
|
{},
|
||||||
|
|
Loading…
Reference in a new issue