2020-06-04 08:16:33 -07:00
import {
BINARY_ENCODING ,
IExecuteFunctions ,
} from 'n8n-core' ;
2020-07-23 18:08:34 -07:00
2019-06-23 03:35:23 -07:00
import {
IDataObject ,
INodeExecutionData ,
INodeType ,
2020-10-01 05:01:39 -07:00
INodeTypeDescription ,
2021-04-16 09:33:36 -07:00
NodeOperationError ,
2019-06-23 03:35:23 -07:00
} 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' ,
2021-03-11 00:16:05 -08:00
color : '#007ee5' ,
2019-06-23 03:35:23 -07:00
} ,
inputs : [ 'main' ] ,
outputs : [ 'main' ] ,
credentials : [
{
name : 'dropboxApi' ,
required : true ,
2020-06-03 07:24:17 -07:00
displayOptions : {
show : {
2020-06-04 08:16:33 -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 : {
2020-06-04 08:16:33 -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' ,
2020-07-23 18:08:34 -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' ,
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' ,
2020-06-04 08:16:33 -07:00
description : 'The resource to operate on.' ,
2019-07-14 05:26:09 -07:00
} ,
// ----------------------------------
// operations
// ----------------------------------
2019-06-23 03:35:23 -07:00
{
displayName : 'Operation' ,
name : 'operation' ,
type : 'options' ,
2019-07-14 05:26:09 -07:00
displayOptions : {
show : {
2020-06-04 08:16:33 -07:00
resource : [
'file' ,
] ,
} ,
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' ,
2019-06-23 03:35:23 -07:00
} ,
{
name : 'Delete' ,
value : 'delete' ,
2020-06-04 08:16:33 -07:00
description : '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' ,
2019-06-23 03:35:23 -07:00
} ,
{
name : 'Move' ,
value : 'move' ,
2020-06-04 08:16:33 -07:00
description : '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' ,
} ,
2019-06-23 03:35:23 -07:00
] ,
2019-07-14 05:26:09 -07:00
default : 'upload' ,
2020-06-04 08:16:33 -07:00
description : 'The operation to perform.' ,
2019-06-23 03:35:23 -07:00
} ,
2019-07-14 05:26:09 -07:00
{
displayName : 'Operation' ,
name : 'operation' ,
type : 'options' ,
displayOptions : {
show : {
2020-06-04 08:16:33 -07:00
resource : [
'folder' ,
] ,
} ,
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' ,
2019-07-14 05:26:09 -07:00
} ,
{
name : 'Create' ,
value : 'create' ,
2020-06-04 08:16:33 -07:00
description : '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' ,
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' ,
2019-07-14 05:26:09 -07:00
} ,
{
name : 'Move' ,
value : 'move' ,
2020-06-04 08:16:33 -07:00
description : 'Move a folder' ,
} ,
2019-07-14 05:26:09 -07:00
] ,
default : 'create' ,
2020-06-04 08:16:33 -07:00
description : 'The operation to perform.' ,
2019-07-14 05:26:09 -07:00
} ,
2019-06-23 03:35:23 -07:00
2021-03-11 00:16:05 -08:00
{
displayName : 'Operation' ,
name : 'operation' ,
type : 'options' ,
displayOptions : {
show : {
resource : [
'search' ,
] ,
} ,
} ,
options : [
{
name : 'Query' ,
value : 'query' ,
} ,
] ,
default : 'query' ,
description : 'The operation to perform.' ,
} ,
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 : {
2020-06-04 08:16:33 -07:00
operation : [
2020-10-22 06:46:03 -07:00
'copy' ,
2020-06-04 08:16:33 -07:00
] ,
resource : [
'file' ,
'folder' ,
] ,
} ,
2019-06-23 03:35:23 -07:00
} ,
placeholder : '/invoices/original.txt' ,
2020-06-04 08:16:33 -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 : {
2020-06-04 08:16:33 -07:00
operation : [
2020-10-22 06:46:03 -07:00
'copy' ,
2020-06-04 08:16:33 -07:00
] ,
resource : [
'file' ,
'folder' ,
] ,
} ,
2019-06-23 03:35:23 -07:00
} ,
placeholder : '/invoices/copy.txt' ,
2020-06-04 08:16:33 -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 : {
2020-06-04 08:16:33 -07:00
operation : [
2020-10-22 06:46:03 -07:00
'delete' ,
2020-06-04 08:16:33 -07:00
] ,
resource : [
'file' ,
'folder' ,
] ,
} ,
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
} ,
2020-06-04 08:16:33 -07:00
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 : {
2020-06-04 08:16:33 -07:00
operation : [
2020-10-22 06:46:03 -07:00
'move' ,
2020-06-04 08:16:33 -07:00
] ,
resource : [
'file' ,
'folder' ,
] ,
} ,
2019-06-23 03:35:23 -07:00
} ,
2019-07-14 05:26:09 -07:00
placeholder : '/invoices/old_name.txt' ,
2020-06-04 08:16:33 -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 : {
2020-06-04 08:16:33 -07:00
operation : [
2020-10-22 06:46:03 -07:00
'move' ,
2020-06-04 08:16:33 -07:00
] ,
resource : [
'file' ,
'folder' ,
] ,
} ,
2019-06-23 03:35:23 -07:00
} ,
2019-07-14 05:26:09 -07:00
placeholder : '/invoices/new_name.txt' ,
2020-06-04 08:16:33 -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 : {
2020-06-04 08:16:33 -07:00
operation : [
2020-10-22 06:46:03 -07:00
'download' ,
2020-06-04 08:16:33 -07:00
] ,
resource : [
'file' ,
] ,
} ,
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
} ,
{
2019-07-14 05:26:09 -07:00
displayName : 'Binary Property' ,
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 : {
2020-06-04 08:16:33 -07:00
operation : [
2020-10-22 06:46:03 -07:00
'download' ,
2020-06-04 08:16:33 -07:00
] ,
resource : [
'file' ,
] ,
} ,
2019-06-23 03:35:23 -07:00
} ,
2021-10-27 13:00:13 -07:00
description : 'Name of the binary property to which to write the data of the read file.' ,
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 : {
2020-06-04 08:16:33 -07:00
operation : [
2020-10-22 06:46:03 -07:00
'upload' ,
2020-06-04 08:16:33 -07:00
] ,
resource : [
'file' ,
] ,
} ,
2019-06-23 03:35:23 -07:00
} ,
placeholder : '/invoices/2019/invoice_1.pdf' ,
2020-06-04 08:16:33 -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
} ,
{
displayName : 'Binary Data' ,
name : 'binaryData' ,
type : 'boolean' ,
default : false ,
displayOptions : {
show : {
2020-06-04 08:16:33 -07:00
operation : [
2020-10-22 06:46:03 -07:00
'upload' ,
2020-06-04 08:16:33 -07:00
] ,
resource : [
'file' ,
] ,
} ,
2019-06-23 03:35:23 -07:00
} ,
2020-06-04 08:16:33 -07:00
description : 'If 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 : {
2020-06-04 08:16:33 -07:00
operation : [
2020-10-22 06:46:03 -07:00
'upload' ,
2020-06-04 08:16:33 -07:00
] ,
resource : [
'file' ,
] ,
binaryData : [
2020-10-22 06:46:03 -07:00
false ,
2020-06-04 08:16:33 -07:00
] ,
} ,
2019-06-23 03:35:23 -07:00
} ,
placeholder : '' ,
2020-06-04 08:16:33 -07:00
description : 'The text content of the file to upload.' ,
2019-06-23 03:35:23 -07:00
} ,
{
displayName : 'Binary Property' ,
name : 'binaryPropertyName' ,
type : 'string' ,
default : 'data' ,
required : true ,
displayOptions : {
show : {
2020-06-04 08:16:33 -07:00
operation : [
2020-10-22 06:46:03 -07:00
'upload' ,
2020-06-04 08:16:33 -07:00
] ,
resource : [
'file' ,
] ,
binaryData : [
2020-10-22 06:46:03 -07:00
true ,
2020-06-04 08:16:33 -07:00
] ,
} ,
2019-06-23 03:35:23 -07:00
} ,
placeholder : '' ,
2021-10-27 13:00:13 -07:00
description : 'Name of the binary property which contains the data for 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 : {
operation : [
'query' ,
] ,
resource : [
'search' ,
] ,
} ,
} ,
description : ' The string to search for. May match across multiple fields based on the request arguments.' ,
} ,
{
displayName : 'File Status' ,
name : 'fileStatus' ,
type : 'options' ,
options : [
{
name : 'Active' ,
value : 'active' ,
} ,
{
name : 'Deleted' ,
value : 'deleted' ,
} ,
] ,
default : 'active' ,
displayOptions : {
show : {
operation : [
'query' ,
] ,
resource : [
'search' ,
] ,
} ,
} ,
description : ' The string to search for. May match across multiple fields based on the request arguments.' ,
} ,
{
displayName : 'Return All' ,
name : 'returnAll' ,
type : 'boolean' ,
displayOptions : {
show : {
operation : [
'query' ,
] ,
resource : [
'search' ,
] ,
} ,
} ,
default : false ,
description : 'If all results should be returned or only up to a given limit.' ,
} ,
{
displayName : 'Limit' ,
name : 'limit' ,
type : 'number' ,
displayOptions : {
show : {
resource : [
'search' ,
] ,
operation : [
'query' ,
] ,
returnAll : [
false ,
] ,
} ,
} ,
default : 100 ,
description : 'How many results to return.' ,
} ,
{
displayName : 'Simple' ,
name : 'simple' ,
type : 'boolean' ,
displayOptions : {
show : {
operation : [
'query' ,
] ,
resource : [
'search' ,
] ,
} ,
} ,
default : true ,
description : 'When set to true a simplify version of the response will be used else the raw data.' ,
} ,
{
displayName : 'Filters' ,
name : 'filters' ,
type : 'collection' ,
placeholder : 'Add Filter' ,
default : { } ,
displayOptions : {
show : {
resource : [
'search' ,
] ,
operation : [
'query' ,
] ,
} ,
} ,
options : [
{
displayName : 'File Categories' ,
name : 'file_categories' ,
type : 'multiOptions' ,
options : [
{
name : 'Audio (mp3, wav, mid, etc.)' ,
value : 'audio' ,
} ,
{
name : 'Document (doc, docx, txt, etc.)' ,
value : 'document' ,
} ,
{
name : 'Folder' ,
value : 'folder' ,
} ,
{
name : 'Image (jpg, png, gif, etc.)' ,
value : 'image' ,
} ,
{
name : 'Other' ,
value : 'other' ,
} ,
{
name : 'Dropbox Paper' ,
value : 'paper' ,
} ,
{
name : 'PDF' ,
value : 'pdf' ,
} ,
{
name : 'Presentation (ppt, pptx, key, etc.)' ,
value : 'presentation' ,
} ,
{
name : 'Spreadsheet (xlsx, xls, csv, etc.)' ,
value : 'spreadsheet' ,
} ,
{
name : 'Video (avi, wmv, mp4, etc.)' ,
value : 'video' ,
} ,
] ,
default : [ ] ,
} ,
{
displayName : 'File Extensions' ,
name : 'file_extensions' ,
type : 'string' ,
default : '' ,
description : 'Multiple can be set separated by comma. Example: jpg,pdf' ,
} ,
{
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 : {
2020-06-04 08:16:33 -07:00
operation : [
2020-10-22 06:46:03 -07:00
'create' ,
2020-06-04 08:16:33 -07:00
] ,
resource : [
'folder' ,
] ,
} ,
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 : {
2020-06-04 08:16:33 -07:00
operation : [
2020-10-22 06:46:03 -07:00
'list' ,
2020-06-04 08:16:33 -07:00
] ,
resource : [
'folder' ,
] ,
} ,
2019-07-14 05:26:09 -07:00
} ,
placeholder : '/invoices/2019/' ,
2020-06-04 08:16:33 -07:00
description : 'The path of which to list the content.' ,
} ,
2021-03-11 00:16:05 -08:00
{
displayName : 'Return All' ,
name : 'returnAll' ,
type : 'boolean' ,
displayOptions : {
show : {
operation : [
'list' ,
] ,
resource : [
'folder' ,
] ,
} ,
} ,
default : false ,
description : 'If all results should be returned or only up to a given limit.' ,
} ,
{
displayName : 'Limit' ,
name : 'limit' ,
type : 'number' ,
displayOptions : {
show : {
resource : [
'folder' ,
] ,
operation : [
'list' ,
] ,
returnAll : [
false ,
] ,
} ,
} ,
default : 100 ,
description : 'How many results to return.' ,
} ,
{
displayName : 'Filters' ,
name : 'filters' ,
type : 'collection' ,
placeholder : 'Add Filter' ,
default : { } ,
displayOptions : {
show : {
resource : [
'folder' ,
] ,
operation : [
'list' ,
] ,
} ,
} ,
options : [
{
displayName : 'Include Deleted' ,
name : 'include_deleted' ,
type : 'boolean' ,
default : false ,
description : 'If true, the results will include entries for files and folders that used to exist but were deleted. The default for this field is False.' ,
} ,
{
displayName : 'Include Shared Members ' ,
name : 'include_has_explicit_shared_members' ,
type : 'boolean' ,
default : false ,
description : 'If true, 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.' ,
} ,
{
displayName : 'Include Mounted Folders ' ,
name : 'include_mounted_folders' ,
type : 'boolean' ,
default : true ,
description : 'If true, the results will include entries under mounted folders which includes app folder, shared folder and team folder. The default for this field is True.' ,
} ,
{
displayName : 'Include Non Downloadable Files ' ,
name : 'include_non_downloadable_files' ,
type : 'boolean' ,
default : true ,
description : 'If true, include files that are not downloadable, i.e. Google Docs. The default for this field is True.' ,
} ,
{
displayName : 'Recursive' ,
name : 'recursive' ,
type : 'boolean' ,
default : false ,
description : 'If true, 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.' ,
} ,
] ,
} ,
2020-06-04 08:16:33 -07:00
] ,
2019-06-23 03:35:23 -07: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 ( ) ;
const returnData : IDataObject [ ] = [ ] ;
2020-06-04 08:16:33 -07:00
2019-07-14 05:26:09 -07:00
const resource = this . getNodeParameter ( 'resource' , 0 ) as string ;
2019-06-23 03:35:23 -07:00
const operation = this . getNodeParameter ( 'operation' , 0 ) as string ;
let endpoint = '' ;
let requestMethod = '' ;
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
const { root_info : { root_namespace_id } } = await getRootDirectory . call ( this ) ;
headers = {
'dropbox-api-path-root' : JSON . stringify ( {
'.tag' : 'root' ,
'root' : root_namespace_id ,
} ) ,
} ;
}
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' ;
2020-06-04 08:16:33 -07:00
2021-07-19 23:58:54 -07:00
} 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
2021-07-19 23:58:54 -07:00
if ( this . getNodeParameter ( 'binaryData' , i ) === true ) {
2020-07-23 18:08:34 -07:00
2021-07-19 23:58:54 -07:00
// Is binary file to upload
const item = items [ i ] ;
2019-06-23 03:35:23 -07:00
2021-07-19 23:58:54 -07:00
if ( item . binary === undefined ) {
throw new NodeOperationError ( this . getNode ( ) , 'No binary data exists on item!' ) ;
}
2019-06-23 03:35:23 -07:00
2021-07-19 23:58:54 -07:00
const propertyNameUpload = this . getNodeParameter ( 'binaryPropertyName' , i ) as string ;
2019-06-23 03:35:23 -07:00
2021-07-19 23:58:54 -07:00
if ( item . binary [ propertyNameUpload ] === undefined ) {
throw new NodeOperationError ( this . getNode ( ) , ` No binary data property " ${ propertyNameUpload } " does not exists on item! ` ) ;
}
2019-06-23 03:35:23 -07:00
2021-07-19 23:58:54 -07:00
body = Buffer . from ( item . binary [ propertyNameUpload ] . data , BINARY_ENCODING ) ;
} 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' ;
2021-03-11 00:16:05 -08:00
2021-07-19 23:58:54 -07:00
} else if ( operation === 'list' ) {
// ----------------------------------
// list
// ----------------------------------
2019-06-23 03:35:23 -07:00
2021-07-19 23:58:54 -07:00
returnAll = this . getNodeParameter ( 'returnAll' , 0 ) as boolean ;
2020-06-04 08:16:33 -07:00
2021-07-19 23:58:54 -07:00
const filters = this . getNodeParameter ( 'filters' , i ) as IDataObject ;
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
2021-07-19 23:58:54 -07:00
if ( returnAll === false ) {
const limit = this . getNodeParameter ( 'limit' , 0 ) as number ;
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
2021-07-19 23:58:54 -07:00
returnAll = this . getNodeParameter ( 'returnAll' , 0 ) as boolean ;
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
2021-07-19 23:58:54 -07:00
const filters = this . getNodeParameter ( 'filters' , i ) as IDataObject ;
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
2021-07-19 23:58:54 -07:00
Object . assign ( body . options , filters ) ;
2019-07-14 05:26:09 -07:00
2021-07-19 23:58:54 -07:00
if ( returnAll === false ) {
const limit = this . getNodeParameter ( 'limit' , i ) as number ;
Object . assign ( body . options , { max_results : limit } ) ;
}
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 {
throw new NodeOperationError ( this . getNode ( ) , ` The resource " ${ resource } " is not known! ` ) ;
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
2021-07-19 23:58:54 -07:00
if ( returnAll === true ) {
responseData = await dropboxpiRequestAllItems . call ( this , property , requestMethod , endpoint , body , query , headers ) ;
} else {
responseData = await dropboxApiRequest . call ( this , requestMethod , endpoint , body , query , headers , options ) ;
}
2020-07-23 18:08:34 -07:00
2021-07-19 23:58:54 -07:00
if ( resource === 'file' && operation === 'upload' ) {
responseData = JSON . parse ( responseData ) ;
}
2020-06-04 08:17:15 -07:00
2021-07-19 23:58:54 -07:00
if ( resource === 'file' && operation === 'download' ) {
2020-06-04 08:16:33 -07:00
2021-07-19 23:58:54 -07:00
const newItem : INodeExecutionData = {
json : items [ i ] . json ,
binary : { } ,
} ;
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.
Object . assign ( newItem . binary , items [ i ] . binary ) ;
}
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
2021-07-19 23:58:54 -07:00
const dataPropertyNameDownload = this . getNodeParameter ( 'binaryPropertyName' , i ) as string ;
2019-06-23 03:35:23 -07:00
2021-07-19 23:58:54 -07:00
const filePathDownload = this . getNodeParameter ( 'path' , i ) as string ;
items [ i ] . binary ! [ dataPropertyNameDownload ] = await this . helpers . prepareBinaryData ( Buffer . from ( responseData ) , filePathDownload ) ;
2020-06-04 08:16:33 -07:00
2021-07-19 23:58:54 -07:00
} else if ( resource === 'folder' && operation === 'list' ) {
2020-06-04 08:16:33 -07:00
2021-07-19 23:58:54 -07:00
const propNames : { [ key : string ] : string } = {
'id' : 'id' ,
'name' : 'name' ,
'client_modified' : 'lastModifiedClient' ,
'server_modified' : 'lastModifiedServer' ,
'rev' : 'rev' ,
'size' : 'contentSize' ,
'.tag' : 'type' ,
'content_hash' : 'contentHash' ,
'path_lower' : 'pathLower' ,
'path_display' : 'pathDisplay' ,
'has_explicit_shared_members' : 'hasExplicitSharedMembers' ,
'is_downloadable' : 'isDownloadable' ,
} ;
2019-06-23 03:35:23 -07:00
2021-07-19 23:58:54 -07:00
if ( returnAll === false ) {
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
}
2021-07-19 23:58:54 -07:00
returnData . push ( newItem as IDataObject ) ;
}
} else if ( resource === 'search' && operation === 'query' ) {
if ( returnAll === true ) {
returnData . push . apply ( returnData , ( simple === true ) ? simplify ( responseData ) : responseData ) ;
} else {
returnData . push . apply ( returnData , ( simple === true ) ? simplify ( responseData [ property ] ) : responseData [ property ] ) ;
}
2021-03-11 00:16:05 -08:00
} else {
2021-07-19 23:58:54 -07:00
returnData . push ( responseData ) ;
}
} catch ( error ) {
if ( this . continueOnFail ( ) ) {
if ( resource === 'file' && operation === 'download' ) {
items [ i ] . json = { error : error.message } ;
} else {
returnData . push ( { error : error.message } ) ;
}
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
return this . prepareOutputData ( items ) ;
} else {
2019-07-14 05:26:09 -07:00
// For all other ones does the output items get replaced
2019-06-23 03:35:23 -07:00
return [ this . helpers . returnJsonArray ( returnData ) ] ;
}
}
}