mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 18:12:04 +00:00
feat(KoBoToolbox Node): Add support for Media file API (#4578)
This commit is contained in:
@@ -1,11 +1,19 @@
|
||||
import { IExecuteFunctions } from 'n8n-core';
|
||||
|
||||
import { IDataObject, INodeExecutionData, INodeType, INodeTypeDescription } from 'n8n-workflow';
|
||||
import {
|
||||
IBinaryData,
|
||||
IBinaryKeyData,
|
||||
IDataObject,
|
||||
INodeExecutionData,
|
||||
INodeType,
|
||||
INodeTypeDescription,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import {
|
||||
downloadAttachments,
|
||||
formatSubmission,
|
||||
koBoToolboxApiRequest,
|
||||
koBoToolboxRawRequest,
|
||||
loadForms,
|
||||
parseStringList,
|
||||
} from './GenericFunctions';
|
||||
@@ -16,6 +24,8 @@ import { submissionFields, submissionOperations } from './SubmissionDescription'
|
||||
|
||||
import { hookFields, hookOperations } from './HookDescription';
|
||||
|
||||
import { fileFields, fileOperations } from './FileDescription';
|
||||
|
||||
export class KoBoToolbox implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'KoBoToolbox',
|
||||
@@ -43,6 +53,10 @@ export class KoBoToolbox implements INodeType {
|
||||
type: 'options',
|
||||
noDataExpression: true,
|
||||
options: [
|
||||
{
|
||||
name: 'File',
|
||||
value: 'file',
|
||||
},
|
||||
{
|
||||
name: 'Form',
|
||||
value: 'form',
|
||||
@@ -65,6 +79,8 @@ export class KoBoToolbox implements INodeType {
|
||||
...hookFields,
|
||||
...submissionOperations,
|
||||
...submissionFields,
|
||||
...fileOperations,
|
||||
...fileFields,
|
||||
],
|
||||
};
|
||||
|
||||
@@ -129,7 +145,21 @@ export class KoBoToolbox implements INodeType {
|
||||
scroll: this.getNodeParameter('returnAll', i),
|
||||
});
|
||||
}
|
||||
|
||||
if (operation === 'redeploy') {
|
||||
// ----------------------------------
|
||||
// Form: redeploy
|
||||
// ----------------------------------
|
||||
const formId = this.getNodeParameter('formId', i) as string;
|
||||
responseData = [
|
||||
await koBoToolboxApiRequest.call(this, {
|
||||
method: 'PATCH',
|
||||
url: `/api/v2/assets/${formId}/deployment/`,
|
||||
}),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
if (resource === 'submission') {
|
||||
// *********************************************************************
|
||||
// Submissions
|
||||
@@ -342,6 +372,102 @@ export class KoBoToolbox implements INodeType {
|
||||
}
|
||||
}
|
||||
|
||||
if (resource === 'file') {
|
||||
// *********************************************************************
|
||||
// File
|
||||
// *********************************************************************
|
||||
const formId = this.getNodeParameter('formId', i) as string;
|
||||
|
||||
if (operation === 'getAll') {
|
||||
responseData = [
|
||||
await koBoToolboxApiRequest.call(this, {
|
||||
url: `/api/v2/assets/${formId}/files`,
|
||||
qs: {
|
||||
file_type: 'form_media',
|
||||
},
|
||||
scroll: true,
|
||||
}),
|
||||
];
|
||||
}
|
||||
|
||||
if (operation === 'get') {
|
||||
const fileId = this.getNodeParameter('fileId', i) as string;
|
||||
const download = this.getNodeParameter('download', i) as boolean;
|
||||
|
||||
responseData = [
|
||||
await koBoToolboxApiRequest.call(this, {
|
||||
url: `/api/v2/assets/${formId}/files/${fileId}`,
|
||||
}),
|
||||
];
|
||||
|
||||
if (responseData && responseData[0] && download) {
|
||||
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i) as string;
|
||||
|
||||
const binaryItem: INodeExecutionData = {
|
||||
json: responseData[0],
|
||||
binary: {},
|
||||
};
|
||||
|
||||
const response = await koBoToolboxRawRequest.call(this, {
|
||||
url: `/api/v2/assets/${formId}/files/${fileId}/content`,
|
||||
encoding: 'arraybuffer',
|
||||
});
|
||||
|
||||
console.dir(response);
|
||||
|
||||
binaryItem.binary![binaryPropertyName] = await this.helpers.prepareBinaryData(
|
||||
response,
|
||||
responseData[0].metadata.filename,
|
||||
);
|
||||
|
||||
binaryItems.push(binaryItem);
|
||||
}
|
||||
}
|
||||
|
||||
if (operation === 'delete') {
|
||||
const fileId = this.getNodeParameter('fileId', i) as string;
|
||||
responseData = [
|
||||
await koBoToolboxApiRequest.call(this, {
|
||||
method: 'DELETE',
|
||||
url: `/api/v2/assets/${formId}/files/${fileId}`,
|
||||
}),
|
||||
];
|
||||
}
|
||||
|
||||
if (operation === 'create') {
|
||||
const fileMode = this.getNodeParameter('fileMode', i) as string;
|
||||
const body: IDataObject = {
|
||||
description: 'Uploaded file',
|
||||
file_type: 'form_media',
|
||||
};
|
||||
|
||||
if ('binary' === fileMode) {
|
||||
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i) as string;
|
||||
const item = items[i].binary as IBinaryKeyData;
|
||||
const binaryData = item[binaryPropertyName] as IBinaryData;
|
||||
|
||||
body.base64Encoded = 'data:' + binaryData.mimeType + ';base64,' + binaryData.data;
|
||||
body.metadata = {
|
||||
filename: binaryData.fileName,
|
||||
};
|
||||
} else {
|
||||
const fileUrl = this.getNodeParameter('fileUrl', i) as string;
|
||||
|
||||
body.metadata = {
|
||||
redirect_url: fileUrl,
|
||||
};
|
||||
}
|
||||
|
||||
responseData = [
|
||||
await koBoToolboxApiRequest.call(this, {
|
||||
method: 'POST',
|
||||
url: `/api/v2/assets/${formId}/files/`,
|
||||
body,
|
||||
}),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
returnData = returnData.concat(responseData);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user