2023-01-27 03:22:44 -08:00
import type {
2019-06-23 03:35:23 -07:00
IDataObject ,
2023-03-06 08:33:32 -08:00
IExecuteFunctions ,
2024-02-14 07:29:09 -08:00
IHttpRequestMethods ,
2019-06-23 03:35:23 -07:00
INodeExecutionData ,
INodeType ,
2020-10-01 05:01:39 -07:00
INodeTypeDescription ,
2019-06-23 03:35:23 -07:00
} from 'n8n-workflow' ;
2023-01-27 03:22:44 -08:00
import { NodeOperationError } from 'n8n-workflow' ;
2020-06-04 08:16:33 -07:00
2020-07-23 18:08:34 -07:00
import {
2021-03-11 00:16:05 -08:00
dropboxApiRequest ,
dropboxpiRequestAllItems ,
2021-03-25 15:25:05 -07:00
getCredentials ,
2021-03-11 00:16:05 -08:00
getRootDirectory ,
simplify ,
2020-07-23 18:08:34 -07:00
} from './GenericFunctions' ;
2020-06-04 08:16:33 -07:00
2019-06-23 03:35:23 -07:00
export class Dropbox implements INodeType {
description : INodeTypeDescription = {
displayName : 'Dropbox' ,
name : 'dropbox' ,
2021-03-11 00:16:05 -08:00
icon : 'file:dropbox.svg' ,
2019-06-23 03:35:23 -07:00
group : [ 'input' ] ,
version : 1 ,
2019-07-14 05:26:09 -07:00
subtitle : '={{$parameter["operation"] + ": " + $parameter["resource"]}}' ,
2019-06-23 03:35:23 -07:00
description : 'Access data on Dropbox' ,
defaults : {
name : 'Dropbox' ,
} ,
inputs : [ 'main' ] ,
outputs : [ 'main' ] ,
credentials : [
{
name : 'dropboxApi' ,
required : true ,
2020-06-03 07:24:17 -07:00
displayOptions : {
show : {
2022-08-01 13:47:55 -07:00
authentication : [ 'accessToken' ] ,
2020-07-23 18:08:34 -07:00
} ,
} ,
2020-06-03 07:24:17 -07:00
} ,
{
name : 'dropboxOAuth2Api' ,
required : true ,
displayOptions : {
show : {
2022-08-01 13:47:55 -07:00
authentication : [ 'oAuth2' ] ,
2020-07-23 18:08:34 -07:00
} ,
} ,
} ,
2019-06-23 03:35:23 -07:00
] ,
properties : [
2020-06-03 07:24:17 -07:00
{
displayName : 'Authentication' ,
name : 'authentication' ,
type : 'options' ,
options : [
{
name : 'Access Token' ,
2020-07-23 18:08:34 -07:00
value : 'accessToken' ,
2020-06-03 07:24:17 -07:00
} ,
{
name : 'OAuth2' ,
2020-07-23 18:08:34 -07:00
value : 'oAuth2' ,
2020-10-22 06:46:03 -07:00
} ,
2020-06-03 07:24:17 -07:00
] ,
default : 'accessToken' ,
2022-05-06 14:01:25 -07:00
description : 'Means of authenticating with the service' ,
2020-06-03 07:24:17 -07:00
} ,
2019-07-14 05:26:09 -07:00
{
displayName : 'Resource' ,
name : 'resource' ,
type : 'options' ,
2022-05-20 14:47:24 -07:00
noDataExpression : true ,
2019-07-14 05:26:09 -07:00
options : [
{
name : 'File' ,
2020-06-04 08:16:33 -07:00
value : 'file' ,
2019-07-14 05:26:09 -07:00
} ,
{
name : 'Folder' ,
2020-06-04 08:16:33 -07:00
value : 'folder' ,
} ,
2021-03-11 00:16:05 -08:00
{
name : 'Search' ,
value : 'search' ,
} ,
2019-07-14 05:26:09 -07:00
] ,
default : 'file' ,
} ,
// ----------------------------------
// operations
// ----------------------------------
2019-06-23 03:35:23 -07:00
{
displayName : 'Operation' ,
name : 'operation' ,
type : 'options' ,
2022-05-20 14:47:24 -07:00
noDataExpression : true ,
2019-07-14 05:26:09 -07:00
displayOptions : {
show : {
2022-08-01 13:47:55 -07:00
resource : [ 'file' ] ,
2020-06-04 08:16:33 -07:00
} ,
2019-07-14 05:26:09 -07:00
} ,
2019-06-23 03:35:23 -07:00
options : [
{
name : 'Copy' ,
value : 'copy' ,
2020-06-04 08:16:33 -07:00
description : 'Copy a file' ,
2022-07-10 13:50:51 -07:00
action : 'Copy a file' ,
2019-06-23 03:35:23 -07:00
} ,
{
name : 'Delete' ,
value : 'delete' ,
2020-06-04 08:16:33 -07:00
description : 'Delete a file' ,
2022-07-10 13:50:51 -07:00
action : 'Delete a file' ,
2019-06-23 03:35:23 -07:00
} ,
{
2019-07-14 05:26:09 -07:00
name : 'Download' ,
value : 'download' ,
2020-06-04 08:16:33 -07:00
description : 'Download a file' ,
2022-07-10 13:50:51 -07:00
action : 'Download a file' ,
2019-06-23 03:35:23 -07:00
} ,
{
name : 'Move' ,
value : 'move' ,
2020-06-04 08:16:33 -07:00
description : 'Move a file' ,
2022-07-10 13:50:51 -07:00
action : 'Move a file' ,
2019-06-23 03:35:23 -07:00
} ,
{
2019-07-14 05:26:09 -07:00
name : 'Upload' ,
value : 'upload' ,
2020-06-04 08:16:33 -07:00
description : 'Upload a file' ,
2022-07-10 13:50:51 -07:00
action : 'Upload a file' ,
2020-06-04 08:16:33 -07:00
} ,
2019-06-23 03:35:23 -07:00
] ,
2019-07-14 05:26:09 -07:00
default : 'upload' ,
2019-06-23 03:35:23 -07:00
} ,
2019-07-14 05:26:09 -07:00
{
displayName : 'Operation' ,
name : 'operation' ,
type : 'options' ,
2022-05-20 14:47:24 -07:00
noDataExpression : true ,
2019-07-14 05:26:09 -07:00
displayOptions : {
show : {
2022-08-01 13:47:55 -07:00
resource : [ 'folder' ] ,
2020-06-04 08:16:33 -07:00
} ,
2019-07-14 05:26:09 -07:00
} ,
options : [
{
name : 'Copy' ,
value : 'copy' ,
2020-06-04 08:16:33 -07:00
description : 'Copy a folder' ,
2022-07-10 13:50:51 -07:00
action : 'Copy a folder' ,
2019-07-14 05:26:09 -07:00
} ,
{
name : 'Create' ,
value : 'create' ,
2020-06-04 08:16:33 -07:00
description : 'Create a folder' ,
2022-07-10 13:50:51 -07:00
action : 'Create a folder' ,
2019-07-14 05:26:09 -07:00
} ,
{
name : 'Delete' ,
value : 'delete' ,
2020-06-04 08:16:33 -07:00
description : 'Delete a folder' ,
2022-07-10 13:50:51 -07:00
action : 'Delete a folder' ,
2019-07-14 05:26:09 -07:00
} ,
{
name : 'List' ,
value : 'list' ,
2020-06-04 08:16:33 -07:00
description : 'Return the files and folders in a given folder' ,
2022-07-10 13:50:51 -07:00
action : 'List a folder' ,
2019-07-14 05:26:09 -07:00
} ,
{
name : 'Move' ,
value : 'move' ,
2020-06-04 08:16:33 -07:00
description : 'Move a folder' ,
2022-07-10 13:50:51 -07:00
action : 'Move a folder' ,
2020-06-04 08:16:33 -07:00
} ,
2019-07-14 05:26:09 -07:00
] ,
default : 'create' ,
} ,
2019-06-23 03:35:23 -07:00
2021-03-11 00:16:05 -08:00
{
displayName : 'Operation' ,
name : 'operation' ,
type : 'options' ,
2022-05-20 14:47:24 -07:00
noDataExpression : true ,
2021-03-11 00:16:05 -08:00
displayOptions : {
show : {
2022-08-01 13:47:55 -07:00
resource : [ 'search' ] ,
2021-03-11 00:16:05 -08:00
} ,
} ,
options : [
{
name : 'Query' ,
value : 'query' ,
2022-07-10 13:50:51 -07:00
action : 'Query' ,
2021-03-11 00:16:05 -08:00
} ,
] ,
default : 'query' ,
} ,
2019-06-23 03:35:23 -07:00
// ----------------------------------
2019-07-14 05:26:09 -07:00
// file
// ----------------------------------
// ----------------------------------
// file/folder:copy
2019-06-23 03:35:23 -07:00
// ----------------------------------
{
displayName : 'From Path' ,
name : 'path' ,
type : 'string' ,
default : '' ,
required : true ,
displayOptions : {
show : {
2022-08-01 13:47:55 -07:00
operation : [ 'copy' ] ,
resource : [ 'file' , 'folder' ] ,
2020-06-04 08:16:33 -07:00
} ,
2019-06-23 03:35:23 -07:00
} ,
placeholder : '/invoices/original.txt' ,
2022-05-06 14:01:25 -07:00
description : 'The path of file or folder to copy' ,
2019-06-23 03:35:23 -07:00
} ,
{
displayName : 'To Path' ,
name : 'toPath' ,
type : 'string' ,
default : '' ,
required : true ,
displayOptions : {
show : {
2022-08-01 13:47:55 -07:00
operation : [ 'copy' ] ,
resource : [ 'file' , 'folder' ] ,
2020-06-04 08:16:33 -07:00
} ,
2019-06-23 03:35:23 -07:00
} ,
placeholder : '/invoices/copy.txt' ,
2022-05-06 14:01:25 -07:00
description : 'The destination path of file or folder' ,
2019-06-23 03:35:23 -07:00
} ,
// ----------------------------------
2019-07-14 05:26:09 -07:00
// file/folder:delete
2019-06-23 03:35:23 -07:00
// ----------------------------------
{
displayName : 'Delete Path' ,
name : 'path' ,
type : 'string' ,
default : '' ,
required : true ,
displayOptions : {
show : {
2022-08-01 13:47:55 -07:00
operation : [ 'delete' ] ,
resource : [ 'file' , 'folder' ] ,
2020-06-04 08:16:33 -07:00
} ,
2019-06-23 03:35:23 -07:00
} ,
placeholder : '/invoices/2019/invoice_1.pdf' ,
2020-06-04 08:16:33 -07:00
description : 'The path to delete. Can be a single file or a whole folder.' ,
2019-06-23 03:35:23 -07:00
} ,
// ----------------------------------
2019-07-14 05:26:09 -07:00
// file/folder:move
2019-06-23 03:35:23 -07:00
// ----------------------------------
{
2019-07-14 05:26:09 -07:00
displayName : 'From Path' ,
2019-06-23 03:35:23 -07:00
name : 'path' ,
type : 'string' ,
default : '' ,
required : true ,
displayOptions : {
show : {
2022-08-01 13:47:55 -07:00
operation : [ 'move' ] ,
resource : [ 'file' , 'folder' ] ,
2020-06-04 08:16:33 -07:00
} ,
2019-06-23 03:35:23 -07:00
} ,
2019-07-14 05:26:09 -07:00
placeholder : '/invoices/old_name.txt' ,
2022-05-06 14:01:25 -07:00
description : 'The path of file or folder to move' ,
2019-06-23 03:35:23 -07:00
} ,
{
2019-07-14 05:26:09 -07:00
displayName : 'To Path' ,
name : 'toPath' ,
2019-06-23 03:35:23 -07:00
type : 'string' ,
default : '' ,
required : true ,
displayOptions : {
show : {
2022-08-01 13:47:55 -07:00
operation : [ 'move' ] ,
resource : [ 'file' , 'folder' ] ,
2020-06-04 08:16:33 -07:00
} ,
2019-06-23 03:35:23 -07:00
} ,
2019-07-14 05:26:09 -07:00
placeholder : '/invoices/new_name.txt' ,
2022-05-06 14:01:25 -07:00
description : 'The new path of file or folder' ,
2019-06-23 03:35:23 -07:00
} ,
// ----------------------------------
2019-07-14 05:26:09 -07:00
// file:download
2019-06-23 03:35:23 -07:00
// ----------------------------------
{
2019-07-14 05:26:09 -07:00
displayName : 'File Path' ,
2019-06-23 03:35:23 -07:00
name : 'path' ,
type : 'string' ,
default : '' ,
required : true ,
displayOptions : {
show : {
2022-08-01 13:47:55 -07:00
operation : [ 'download' ] ,
resource : [ 'file' ] ,
2020-06-04 08:16:33 -07:00
} ,
2019-06-23 03:35:23 -07:00
} ,
2019-07-14 05:26:09 -07:00
placeholder : '/invoices/2019/invoice_1.pdf' ,
2020-06-04 08:16:33 -07:00
description : 'The file path of the file to download. Has to contain the full path.' ,
2019-06-23 03:35:23 -07:00
} ,
{
2024-01-03 03:08:16 -08:00
displayName : 'Put Output File in Field' ,
2019-07-14 05:26:09 -07:00
name : 'binaryPropertyName' ,
2019-06-23 03:35:23 -07:00
type : 'string' ,
required : true ,
2019-07-14 05:26:09 -07:00
default : 'data' ,
2019-06-23 03:35:23 -07:00
displayOptions : {
show : {
2022-08-01 13:47:55 -07:00
operation : [ 'download' ] ,
resource : [ 'file' ] ,
2020-06-04 08:16:33 -07:00
} ,
2019-06-23 03:35:23 -07:00
} ,
2024-01-03 03:08:16 -08:00
hint : 'The name of the output binary field to put the file in' ,
2019-06-23 03:35:23 -07:00
} ,
// ----------------------------------
2019-07-14 05:26:09 -07:00
// file:upload
2019-06-23 03:35:23 -07:00
// ----------------------------------
{
displayName : 'File Path' ,
name : 'path' ,
type : 'string' ,
default : '' ,
required : true ,
displayOptions : {
show : {
2022-08-01 13:47:55 -07:00
operation : [ 'upload' ] ,
resource : [ 'file' ] ,
2020-06-04 08:16:33 -07:00
} ,
2019-06-23 03:35:23 -07:00
} ,
placeholder : '/invoices/2019/invoice_1.pdf' ,
2022-08-01 13:47:55 -07:00
description :
'The file path of the file to upload. Has to contain the full path. The parent folder has to exist. Existing files get overwritten.' ,
2019-06-23 03:35:23 -07:00
} ,
{
2024-01-03 03:08:16 -08:00
displayName : 'Binary File' ,
2019-06-23 03:35:23 -07:00
name : 'binaryData' ,
type : 'boolean' ,
default : false ,
displayOptions : {
show : {
2022-08-01 13:47:55 -07:00
operation : [ 'upload' ] ,
resource : [ 'file' ] ,
2020-06-04 08:16:33 -07:00
} ,
2019-06-23 03:35:23 -07:00
} ,
2022-06-20 07:54:01 -07:00
description : 'Whether the data to upload should be taken from binary field' ,
2019-06-23 03:35:23 -07:00
} ,
{
displayName : 'File Content' ,
name : 'fileContent' ,
type : 'string' ,
default : '' ,
displayOptions : {
show : {
2022-08-01 13:47:55 -07:00
operation : [ 'upload' ] ,
resource : [ 'file' ] ,
binaryData : [ false ] ,
2020-06-04 08:16:33 -07:00
} ,
2019-06-23 03:35:23 -07:00
} ,
placeholder : '' ,
2022-05-06 14:01:25 -07:00
description : 'The text content of the file to upload' ,
2019-06-23 03:35:23 -07:00
} ,
{
2024-01-03 03:08:16 -08:00
displayName : 'Input Binary Field' ,
2019-06-23 03:35:23 -07:00
name : 'binaryPropertyName' ,
type : 'string' ,
default : 'data' ,
required : true ,
displayOptions : {
show : {
2022-08-01 13:47:55 -07:00
operation : [ 'upload' ] ,
resource : [ 'file' ] ,
binaryData : [ true ] ,
2020-06-04 08:16:33 -07:00
} ,
2019-06-23 03:35:23 -07:00
} ,
placeholder : '' ,
2024-01-03 03:08:16 -08:00
hint : 'The name of the input binary field containing the file to be uploaded' ,
2019-06-23 03:35:23 -07:00
} ,
2019-07-14 05:26:09 -07:00
2021-03-11 00:16:05 -08:00
// ----------------------------------
// search:query
// ----------------------------------
{
displayName : 'Query' ,
name : 'query' ,
type : 'string' ,
default : '' ,
required : true ,
displayOptions : {
show : {
2022-08-01 13:47:55 -07:00
operation : [ 'query' ] ,
resource : [ 'search' ] ,
2021-03-11 00:16:05 -08:00
} ,
} ,
2022-08-01 13:47:55 -07:00
description :
'The string to search for. May match across multiple fields based on the request arguments.' ,
2021-03-11 00:16:05 -08:00
} ,
{
displayName : 'File Status' ,
name : 'fileStatus' ,
type : 'options' ,
options : [
{
name : 'Active' ,
value : 'active' ,
} ,
{
name : 'Deleted' ,
value : 'deleted' ,
} ,
] ,
default : 'active' ,
displayOptions : {
show : {
2022-08-01 13:47:55 -07:00
operation : [ 'query' ] ,
resource : [ 'search' ] ,
2021-03-11 00:16:05 -08:00
} ,
} ,
2022-08-01 13:47:55 -07:00
description :
'The string to search for. May match across multiple fields based on the request arguments.' ,
2021-03-11 00:16:05 -08:00
} ,
{
displayName : 'Return All' ,
name : 'returnAll' ,
type : 'boolean' ,
displayOptions : {
show : {
2022-08-01 13:47:55 -07:00
operation : [ 'query' ] ,
resource : [ 'search' ] ,
2021-03-11 00:16:05 -08:00
} ,
} ,
default : false ,
2022-05-06 14:01:25 -07:00
description : 'Whether to return all results or only up to a given limit' ,
2021-03-11 00:16:05 -08:00
} ,
{
displayName : 'Limit' ,
name : 'limit' ,
type : 'number' ,
2022-05-20 14:47:24 -07:00
typeOptions : {
minValue : 1 ,
} ,
2021-03-11 00:16:05 -08:00
displayOptions : {
show : {
2022-08-01 13:47:55 -07:00
resource : [ 'search' ] ,
operation : [ 'query' ] ,
returnAll : [ false ] ,
2021-03-11 00:16:05 -08:00
} ,
} ,
default : 100 ,
2022-05-06 14:01:25 -07:00
description : 'Max number of results to return' ,
2021-03-11 00:16:05 -08:00
} ,
{
2022-05-20 14:47:24 -07:00
displayName : 'Simplify' ,
2021-03-11 00:16:05 -08:00
name : 'simple' ,
type : 'boolean' ,
displayOptions : {
show : {
2022-08-01 13:47:55 -07:00
operation : [ 'query' ] ,
resource : [ 'search' ] ,
2021-03-11 00:16:05 -08:00
} ,
} ,
default : true ,
2022-08-01 13:47:55 -07:00
description :
'Whether to return a simplified version of the response instead of the raw data' ,
2021-03-11 00:16:05 -08:00
} ,
{
displayName : 'Filters' ,
name : 'filters' ,
type : 'collection' ,
placeholder : 'Add Filter' ,
default : { } ,
displayOptions : {
show : {
2022-08-01 13:47:55 -07:00
resource : [ 'search' ] ,
operation : [ 'query' ] ,
2021-03-11 00:16:05 -08:00
} ,
} ,
options : [
{
displayName : 'File Categories' ,
name : 'file_categories' ,
type : 'multiOptions' ,
options : [
{
2022-06-03 10:23:49 -07:00
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-miscased
name : 'Audio (mp3, qav, mid, etc.)' ,
2021-03-11 00:16:05 -08:00
value : 'audio' ,
} ,
{
2022-06-03 10:23:49 -07:00
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-miscased
2021-03-11 00:16:05 -08:00
name : 'Document (doc, docx, txt, etc.)' ,
value : 'document' ,
} ,
2022-06-20 07:54:01 -07:00
{
name : 'Dropbox Paper' ,
value : 'paper' ,
} ,
2021-03-11 00:16:05 -08:00
{
name : 'Folder' ,
value : 'folder' ,
} ,
{
2022-06-03 10:23:49 -07:00
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-miscased
2021-03-11 00:16:05 -08:00
name : 'Image (jpg, png, gif, etc.)' ,
value : 'image' ,
} ,
{
name : 'Other' ,
value : 'other' ,
} ,
{
name : 'PDF' ,
value : 'pdf' ,
} ,
{
2022-06-03 10:23:49 -07:00
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-miscased
2021-03-11 00:16:05 -08:00
name : 'Presentation (ppt, pptx, key, etc.)' ,
value : 'presentation' ,
} ,
{
2022-06-03 10:23:49 -07:00
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-miscased
2021-03-11 00:16:05 -08:00
name : 'Spreadsheet (xlsx, xls, csv, etc.)' ,
value : 'spreadsheet' ,
} ,
{
2022-06-03 10:23:49 -07:00
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-miscased
2021-03-11 00:16:05 -08:00
name : 'Video (avi, wmv, mp4, etc.)' ,
value : 'video' ,
} ,
] ,
default : [ ] ,
} ,
{
displayName : 'File Extensions' ,
name : 'file_extensions' ,
type : 'string' ,
default : '' ,
2022-08-01 13:47:55 -07:00
description :
'Multiple file extensions can be set separated by comma. Example: jpg,pdf.' ,
2021-03-11 00:16:05 -08:00
} ,
{
displayName : 'Folder' ,
name : 'path' ,
type : 'string' ,
default : '' ,
description : 'If this field is not specified, this module searches the entire Dropbox' ,
} ,
] ,
} ,
2020-06-04 08:16:33 -07:00
2019-07-14 05:26:09 -07:00
// ----------------------------------
// folder
// ----------------------------------
// ----------------------------------
// folder:create
// ----------------------------------
{
displayName : 'Folder' ,
name : 'path' ,
type : 'string' ,
default : '' ,
required : true ,
displayOptions : {
show : {
2022-08-01 13:47:55 -07:00
operation : [ 'create' ] ,
resource : [ 'folder' ] ,
2020-06-04 08:16:33 -07:00
} ,
2019-07-14 05:26:09 -07:00
} ,
placeholder : '/invoices/2019' ,
2020-06-04 08:16:33 -07:00
description : 'The folder to create. The parent folder has to exist.' ,
2019-07-14 05:26:09 -07:00
} ,
// ----------------------------------
// folder:list
// ----------------------------------
{
displayName : 'Folder Path' ,
name : 'path' ,
type : 'string' ,
default : '' ,
displayOptions : {
show : {
2022-08-01 13:47:55 -07:00
operation : [ 'list' ] ,
resource : [ 'folder' ] ,
2020-06-04 08:16:33 -07:00
} ,
2019-07-14 05:26:09 -07:00
} ,
placeholder : '/invoices/2019/' ,
2022-05-06 14:01:25 -07:00
description : 'The path of which to list the content' ,
2020-06-04 08:16:33 -07:00
} ,
2021-03-11 00:16:05 -08:00
{
displayName : 'Return All' ,
name : 'returnAll' ,
type : 'boolean' ,
displayOptions : {
show : {
2022-08-01 13:47:55 -07:00
operation : [ 'list' ] ,
resource : [ 'folder' ] ,
2021-03-11 00:16:05 -08:00
} ,
} ,
default : false ,
2022-05-06 14:01:25 -07:00
description : 'Whether to return all results or only up to a given limit' ,
2021-03-11 00:16:05 -08:00
} ,
{
displayName : 'Limit' ,
name : 'limit' ,
type : 'number' ,
2022-05-20 14:47:24 -07:00
typeOptions : {
minValue : 1 ,
} ,
2021-03-11 00:16:05 -08:00
displayOptions : {
show : {
2022-08-01 13:47:55 -07:00
resource : [ 'folder' ] ,
operation : [ 'list' ] ,
returnAll : [ false ] ,
2021-03-11 00:16:05 -08:00
} ,
} ,
default : 100 ,
2022-05-06 14:01:25 -07:00
description : 'Max number of results to return' ,
2021-03-11 00:16:05 -08:00
} ,
{
displayName : 'Filters' ,
name : 'filters' ,
type : 'collection' ,
placeholder : 'Add Filter' ,
default : { } ,
displayOptions : {
show : {
2022-08-01 13:47:55 -07:00
resource : [ 'folder' ] ,
operation : [ 'list' ] ,
2021-03-11 00:16:05 -08:00
} ,
} ,
options : [
{
displayName : 'Include Deleted' ,
name : 'include_deleted' ,
type : 'boolean' ,
default : false ,
2022-08-01 13:47:55 -07:00
description :
'Whether the results will include entries for files and folders that used to exist but were deleted. The default for this field is False.' ,
2021-03-11 00:16:05 -08:00
} ,
{
2022-05-20 14:47:24 -07:00
displayName : 'Include Shared Members' ,
2021-03-11 00:16:05 -08:00
name : 'include_has_explicit_shared_members' ,
type : 'boolean' ,
default : false ,
2022-08-01 13:47:55 -07:00
description :
'Whether the results will include a flag for each file indicating whether or not that file has any explicit members. The default for this field is False.' ,
2021-03-11 00:16:05 -08:00
} ,
{
2022-05-20 14:47:24 -07:00
displayName : 'Include Mounted Folders' ,
2021-03-11 00:16:05 -08:00
name : 'include_mounted_folders' ,
type : 'boolean' ,
default : true ,
2022-08-01 13:47:55 -07:00
description :
'Whether the results will include entries under mounted folders which includes app folder, shared folder and team folder. The default for this field is True.' ,
2021-03-11 00:16:05 -08:00
} ,
{
2022-05-20 14:47:24 -07:00
displayName : 'Include Non Downloadable Files' ,
2021-03-11 00:16:05 -08:00
name : 'include_non_downloadable_files' ,
type : 'boolean' ,
default : true ,
2022-08-01 13:47:55 -07:00
description :
'Whether to include files that are not downloadable, i.e. Google Docs. The default for this field is True.' ,
2021-03-11 00:16:05 -08:00
} ,
{
displayName : 'Recursive' ,
name : 'recursive' ,
type : 'boolean' ,
default : false ,
2022-08-01 13:47:55 -07:00
description :
'Whether the list folder operation will be applied recursively to all subfolders and the response will contain contents of all subfolders. The default for this field is False.' ,
2021-03-11 00:16:05 -08:00
} ,
] ,
} ,
2020-06-04 08:16:33 -07:00
] ,
2019-06-23 03:35:23 -07:00
} ;
async execute ( this : IExecuteFunctions ) : Promise < INodeExecutionData [ ] [ ] > {
const items = this . getInputData ( ) ;
2022-08-30 08:55:33 -07:00
const returnData : INodeExecutionData [ ] = [ ] ;
2019-06-23 03:35:23 -07:00
2022-12-02 03:53:59 -08:00
const resource = this . getNodeParameter ( 'resource' , 0 ) ;
const operation = this . getNodeParameter ( 'operation' , 0 ) ;
2019-06-23 03:35:23 -07:00
let endpoint = '' ;
2024-02-14 07:29:09 -08:00
let requestMethod : IHttpRequestMethods = 'GET' ;
2021-03-11 00:16:05 -08:00
let returnAll = false ;
let property = '' ;
2019-06-23 03:35:23 -07:00
let body : IDataObject | Buffer ;
2020-07-23 18:20:21 -07:00
let options ;
const query : IDataObject = { } ;
2019-06-23 03:35:23 -07:00
2021-03-11 00:16:05 -08:00
let headers : IDataObject = { } ;
let simple = false ;
2021-08-20 09:57:30 -07:00
const { accessType } = await getCredentials . call ( this ) ;
2021-03-25 15:25:05 -07:00
if ( accessType === 'full' ) {
// get the root directory to set it as the default for all operations
2022-08-01 13:47:55 -07:00
const {
root_info : { root_namespace_id } ,
} = await getRootDirectory . call ( this ) ;
2021-03-25 15:25:05 -07:00
headers = {
'dropbox-api-path-root' : JSON . stringify ( {
'.tag' : 'root' ,
2022-08-01 13:47:55 -07:00
root : root_namespace_id ,
2021-03-25 15:25:05 -07:00
} ) ,
} ;
}
2019-06-23 03:35:23 -07:00
for ( let i = 0 ; i < items . length ; i ++ ) {
2021-07-19 23:58:54 -07:00
try {
body = { } ;
2019-06-23 03:35:23 -07:00
2021-07-19 23:58:54 -07:00
if ( resource === 'file' ) {
if ( operation === 'download' ) {
// ----------------------------------
// download
// ----------------------------------
2019-06-23 03:35:23 -07:00
2021-07-19 23:58:54 -07:00
requestMethod = 'POST' ;
2020-07-15 00:52:28 -07:00
2021-07-19 23:58:54 -07:00
query . arg = JSON . stringify ( {
path : this.getNodeParameter ( 'path' , i ) as string ,
} ) ;
2019-06-23 03:35:23 -07:00
2021-07-19 23:58:54 -07:00
endpoint = 'https://content.dropboxapi.com/2/files/download' ;
} else if ( operation === 'upload' ) {
// ----------------------------------
// upload
// ----------------------------------
2019-06-23 03:35:23 -07:00
2021-07-19 23:58:54 -07:00
requestMethod = 'POST' ;
headers [ 'Content-Type' ] = 'application/octet-stream' ;
2020-07-15 00:52:28 -07:00
2021-07-19 23:58:54 -07:00
query . arg = JSON . stringify ( {
mode : 'overwrite' ,
path : this.getNodeParameter ( 'path' , i ) as string ,
} ) ;
2019-06-23 03:35:23 -07:00
2021-07-19 23:58:54 -07:00
endpoint = 'https://content.dropboxapi.com/2/files/upload' ;
2019-06-23 03:35:23 -07:00
2021-07-19 23:58:54 -07:00
options = { json : false } ;
2020-07-23 18:08:34 -07:00
2022-12-02 12:54:28 -08:00
if ( this . getNodeParameter ( 'binaryData' , i ) ) {
2023-03-06 08:33:32 -08:00
const binaryPropertyName = this . getNodeParameter ( 'binaryPropertyName' , i ) ;
this . helpers . assertBinaryData ( i , binaryPropertyName ) ;
body = await this . helpers . getBinaryDataBuffer ( i , binaryPropertyName ) ;
2021-07-19 23:58:54 -07:00
} else {
// Is text file
body = Buffer . from ( this . getNodeParameter ( 'fileContent' , i ) as string , 'utf8' ) ;
}
2019-07-14 05:26:09 -07:00
}
2021-07-19 23:58:54 -07:00
} else if ( resource === 'folder' ) {
if ( operation === 'create' ) {
// ----------------------------------
// create
// ----------------------------------
2020-06-04 08:16:33 -07:00
2021-07-19 23:58:54 -07:00
requestMethod = 'POST' ;
body = {
path : this.getNodeParameter ( 'path' , i ) as string ,
} ;
2019-06-23 03:35:23 -07:00
2021-07-19 23:58:54 -07:00
endpoint = 'https://api.dropboxapi.com/2/files/create_folder_v2' ;
} else if ( operation === 'list' ) {
// ----------------------------------
// list
// ----------------------------------
2019-06-23 03:35:23 -07:00
2022-11-18 05:31:38 -08:00
returnAll = this . getNodeParameter ( 'returnAll' , 0 ) ;
2020-06-04 08:16:33 -07:00
2022-11-18 07:29:44 -08:00
const filters = this . getNodeParameter ( 'filters' , i ) ;
2021-03-11 00:16:05 -08:00
2021-07-19 23:58:54 -07:00
property = 'entries' ;
2021-03-11 00:16:05 -08:00
2021-07-19 23:58:54 -07:00
requestMethod = 'POST' ;
body = {
path : this.getNodeParameter ( 'path' , i ) as string ,
limit : 1000 ,
} ;
2021-03-11 00:16:05 -08:00
2022-12-02 12:54:28 -08:00
if ( ! returnAll ) {
2022-11-18 06:26:22 -08:00
const limit = this . getNodeParameter ( 'limit' , 0 ) ;
2021-07-19 23:58:54 -07:00
body . limit = limit ;
}
2021-03-11 00:16:05 -08:00
2021-07-19 23:58:54 -07:00
Object . assign ( body , filters ) ;
2021-03-11 00:16:05 -08:00
2021-07-19 23:58:54 -07:00
endpoint = 'https://api.dropboxapi.com/2/files/list_folder' ;
2021-03-11 00:16:05 -08:00
}
2021-07-19 23:58:54 -07:00
} else if ( resource === 'search' ) {
if ( operation === 'query' ) {
// ----------------------------------
// query
// ----------------------------------
2021-03-11 00:16:05 -08:00
2022-11-18 05:31:38 -08:00
returnAll = this . getNodeParameter ( 'returnAll' , 0 ) ;
2021-03-11 00:16:05 -08:00
2021-07-19 23:58:54 -07:00
simple = this . getNodeParameter ( 'simple' , 0 ) as boolean ;
2019-07-14 05:26:09 -07:00
2022-11-18 07:29:44 -08:00
const filters = this . getNodeParameter ( 'filters' , i ) ;
2020-06-04 08:16:33 -07:00
2021-07-19 23:58:54 -07:00
property = 'matches' ;
2019-07-14 05:26:09 -07:00
2021-07-19 23:58:54 -07:00
requestMethod = 'POST' ;
body = {
query : this.getNodeParameter ( 'query' , i ) as string ,
options : {
filename_only : true ,
} ,
} ;
2019-07-14 05:26:09 -07:00
2021-07-19 23:58:54 -07:00
if ( filters . file_extensions ) {
filters . file_extensions = ( filters . file_extensions as string ) . split ( ',' ) ;
}
2020-06-04 08:16:33 -07:00
2022-08-30 08:55:33 -07:00
Object . assign ( body . options ! , filters ) ;
2019-07-14 05:26:09 -07:00
2022-12-02 12:54:28 -08:00
if ( ! returnAll ) {
2022-11-18 06:26:22 -08:00
const limit = this . getNodeParameter ( 'limit' , i ) ;
2022-08-30 08:55:33 -07:00
Object . assign ( body . options ! , { max_results : limit } ) ;
2021-07-19 23:58:54 -07:00
}
2019-07-14 05:26:09 -07:00
2021-07-19 23:58:54 -07:00
endpoint = 'https://api.dropboxapi.com/2/files/search_v2' ;
}
}
if ( [ 'file' , 'folder' , 'search' ] . includes ( resource ) ) {
if ( operation === 'copy' ) {
// ----------------------------------
// copy
// ----------------------------------
requestMethod = 'POST' ;
body = {
from_path : this.getNodeParameter ( 'path' , i ) as string ,
to_path : this.getNodeParameter ( 'toPath' , i ) as string ,
} ;
endpoint = 'https://api.dropboxapi.com/2/files/copy_v2' ;
} else if ( operation === 'delete' ) {
// ----------------------------------
// delete
// ----------------------------------
requestMethod = 'POST' ;
body = {
path : this.getNodeParameter ( 'path' , i ) as string ,
} ;
endpoint = 'https://api.dropboxapi.com/2/files/delete_v2' ;
} else if ( operation === 'move' ) {
// ----------------------------------
// move
// ----------------------------------
requestMethod = 'POST' ;
body = {
from_path : this.getNodeParameter ( 'path' , i ) as string ,
to_path : this.getNodeParameter ( 'toPath' , i ) as string ,
} ;
endpoint = 'https://api.dropboxapi.com/2/files/move_v2' ;
}
} else {
2022-08-01 13:47:55 -07:00
throw new NodeOperationError ( this . getNode ( ) , ` The resource " ${ resource } " is not known! ` , {
itemIndex : i ,
} ) ;
2019-06-23 03:35:23 -07:00
}
2021-07-19 23:58:54 -07:00
if ( resource === 'file' && operation === 'download' ) {
// Return the data as a buffer
options = { encoding : null } ;
}
2019-06-23 03:35:23 -07:00
2021-07-19 23:58:54 -07:00
let responseData ;
2021-03-11 00:16:05 -08:00
2022-12-02 12:54:28 -08:00
if ( returnAll ) {
2022-08-01 13:47:55 -07:00
responseData = await dropboxpiRequestAllItems . call (
this ,
property ,
requestMethod ,
endpoint ,
body ,
query ,
headers ,
) ;
2021-07-19 23:58:54 -07:00
} else {
2022-08-01 13:47:55 -07:00
responseData = await dropboxApiRequest . call (
this ,
requestMethod ,
endpoint ,
body ,
query ,
headers ,
options ,
) ;
2021-07-19 23:58:54 -07:00
}
2020-07-23 18:08:34 -07:00
2021-07-19 23:58:54 -07:00
if ( resource === 'file' && operation === 'upload' ) {
2023-02-27 19:39:43 -08:00
const data = JSON . parse ( responseData as string ) ;
2022-08-30 08:55:33 -07:00
const executionData = this . helpers . constructExecutionMetaData (
2023-02-27 19:39:43 -08:00
this . helpers . returnJsonArray ( data as IDataObject [ ] ) ,
2022-08-30 08:55:33 -07:00
{ itemData : { item : i } } ,
) ;
returnData . push ( . . . executionData ) ;
} else if ( resource === 'file' && operation === 'download' ) {
2021-07-19 23:58:54 -07:00
const newItem : INodeExecutionData = {
json : items [ i ] . json ,
binary : { } ,
2022-11-18 06:26:22 -08:00
pairedItem : { item : i } ,
2021-07-19 23:58:54 -07:00
} ;
2019-08-01 13:55:33 -07:00
2021-07-19 23:58:54 -07:00
if ( items [ i ] . binary !== undefined ) {
// Create a shallow copy of the binary data so that the old
// data references which do not get changed still stay behind
// but the incoming data does not get changed.
2022-08-30 08:55:33 -07:00
Object . assign ( newItem . binary ! , items [ i ] . binary ) ;
2021-07-19 23:58:54 -07:00
}
2019-08-01 13:55:33 -07:00
2021-07-19 23:58:54 -07:00
items [ i ] = newItem ;
2019-08-01 13:55:33 -07:00
2023-01-06 06:09:32 -08:00
const dataPropertyNameDownload = this . getNodeParameter ( 'binaryPropertyName' , i ) ;
2019-06-23 03:35:23 -07:00
2021-07-19 23:58:54 -07:00
const filePathDownload = this . getNodeParameter ( 'path' , i ) as string ;
2022-08-01 13:47:55 -07:00
items [ i ] . binary ! [ dataPropertyNameDownload ] = await this . helpers . prepareBinaryData (
2023-02-27 19:39:43 -08:00
Buffer . from ( responseData as string ) ,
2022-08-01 13:47:55 -07:00
filePathDownload ,
) ;
2021-07-19 23:58:54 -07:00
} else if ( resource === 'folder' && operation === 'list' ) {
const propNames : { [ key : string ] : string } = {
2022-08-01 13:47:55 -07:00
id : 'id' ,
name : 'name' ,
client_modified : 'lastModifiedClient' ,
server_modified : 'lastModifiedServer' ,
rev : 'rev' ,
size : 'contentSize' ,
2021-07-19 23:58:54 -07:00
'.tag' : 'type' ,
2022-08-01 13:47:55 -07:00
content_hash : 'contentHash' ,
path_lower : 'pathLower' ,
path_display : 'pathDisplay' ,
has_explicit_shared_members : 'hasExplicitSharedMembers' ,
is_downloadable : 'isDownloadable' ,
2021-07-19 23:58:54 -07:00
} ;
2019-06-23 03:35:23 -07:00
2022-12-02 12:54:28 -08:00
if ( ! returnAll ) {
2021-07-19 23:58:54 -07:00
responseData = responseData . entries ;
}
2021-03-11 00:16:05 -08:00
2021-07-19 23:58:54 -07:00
for ( const item of responseData ) {
const newItem : IDataObject = { } ;
2019-06-23 03:35:23 -07:00
2021-07-19 23:58:54 -07:00
// Get the props and save them under a proper name
for ( const propName of Object . keys ( propNames ) ) {
if ( item [ propName ] !== undefined ) {
newItem [ propNames [ propName ] ] = item [ propName ] ;
}
2019-06-23 03:35:23 -07:00
}
2022-08-30 08:55:33 -07:00
const executionData = this . helpers . constructExecutionMetaData (
this . helpers . returnJsonArray ( newItem ) ,
{ itemData : { item : i } } ,
) ;
returnData . push ( . . . executionData ) ;
2021-07-19 23:58:54 -07:00
}
} else if ( resource === 'search' && operation === 'query' ) {
2022-08-30 08:55:33 -07:00
let data = responseData ;
2022-12-02 12:54:28 -08:00
if ( returnAll ) {
2023-02-27 19:39:43 -08:00
data = simple ? simplify ( responseData as IDataObject [ ] ) : responseData ;
2021-07-19 23:58:54 -07:00
} else {
2023-02-27 19:39:43 -08:00
data = simple
? simplify ( responseData [ property ] as IDataObject [ ] )
: responseData [ property ] ;
2021-07-19 23:58:54 -07:00
}
2022-08-30 08:55:33 -07:00
const executionData = this . helpers . constructExecutionMetaData (
2023-02-27 19:39:43 -08:00
this . helpers . returnJsonArray ( data as IDataObject [ ] ) ,
2022-08-30 08:55:33 -07:00
{ itemData : { item : i } } ,
) ;
returnData . push ( . . . executionData ) ;
2021-03-11 00:16:05 -08:00
} else {
2022-08-30 08:55:33 -07:00
const executionData = this . helpers . constructExecutionMetaData (
2023-02-27 19:39:43 -08:00
this . helpers . returnJsonArray ( responseData as IDataObject [ ] ) ,
2022-08-30 08:55:33 -07:00
{ itemData : { item : i } } ,
) ;
returnData . push ( . . . executionData ) ;
2021-07-19 23:58:54 -07:00
}
} catch ( error ) {
2024-06-19 22:45:00 -07:00
if ( this . continueOnFail ( error ) ) {
2022-08-01 13:47:55 -07:00
if ( resource === 'file' && operation === 'download' ) {
2021-07-19 23:58:54 -07:00
items [ i ] . json = { error : error.message } ;
2022-08-01 13:47:55 -07:00
} else {
2022-11-18 06:26:22 -08:00
returnData . push ( { json : { error : error.message } } ) ;
2021-07-19 23:58:54 -07:00
}
continue ;
2021-03-11 00:16:05 -08:00
}
2021-07-19 23:58:54 -07:00
throw error ;
2019-06-23 03:35:23 -07:00
}
}
2019-07-14 05:26:09 -07:00
if ( resource === 'file' && operation === 'download' ) {
2019-06-23 03:35:23 -07:00
// For file downloads the files get attached to the existing items
2023-09-05 03:59:02 -07:00
return [ items ] ;
2019-06-23 03:35:23 -07:00
} else {
2019-07-14 05:26:09 -07:00
// For all other ones does the output items get replaced
2023-09-05 03:59:02 -07:00
return [ returnData ] ;
2019-06-23 03:35:23 -07:00
}
}
}