2019-06-23 03:35:23 -07:00
import {
BINARY_ENCODING ,
IExecuteFunctions ,
} from 'n8n-core' ;
2020-07-22 14:52:40 -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-07-22 14:52:40 -07:00
import {
parseString ,
} from 'xml2js' ;
2019-06-23 03:35:23 -07:00
2020-07-22 14:52:40 -07:00
import {
nextCloudApiRequest ,
} from './GenericFunctions' ;
2019-06-23 03:35:23 -07:00
export class NextCloud implements INodeType {
description : INodeTypeDescription = {
2020-09-14 00:58:01 -07:00
displayName : 'Nextcloud' ,
2019-06-23 03:35:23 -07:00
name : 'nextCloud' ,
icon : 'file:nextcloud.png' ,
group : [ 'input' ] ,
version : 1 ,
2019-07-14 09:55:04 -07:00
subtitle : '={{$parameter["operation"] + ": " + $parameter["resource"]}}' ,
2020-09-14 00:58:01 -07:00
description : 'Access data on Nextcloud' ,
2019-06-23 03:35:23 -07:00
defaults : {
2020-09-14 00:58:01 -07:00
name : 'Nextcloud' ,
2020-07-22 14:52:40 -07:00
color : '#1cafff' ,
2019-06-23 03:35:23 -07:00
} ,
inputs : [ 'main' ] ,
outputs : [ 'main' ] ,
credentials : [
{
name : 'nextCloudApi' ,
required : true ,
2020-06-17 10:38:30 -07:00
displayOptions : {
show : {
authentication : [
'accessToken' ,
] ,
} ,
} ,
} ,
{
name : 'nextCloudOAuth2Api' ,
required : true ,
displayOptions : {
show : {
authentication : [
'oAuth2' ,
] ,
} ,
} ,
} ,
2019-06-23 03:35:23 -07:00
] ,
properties : [
2020-06-17 10:38:30 -07:00
{
displayName : 'Authentication' ,
name : 'authentication' ,
type : 'options' ,
options : [
{
name : 'Access Token' ,
value : 'accessToken' ,
} ,
{
name : 'OAuth2' ,
value : 'oAuth2' ,
} ,
] ,
default : 'accessToken' ,
description : 'The resource to operate on.' ,
} ,
2019-07-14 08:16:56 -07:00
{
displayName : 'Resource' ,
name : 'resource' ,
type : 'options' ,
options : [
{
name : 'File' ,
value : 'file' ,
} ,
{
name : 'Folder' ,
value : 'folder' ,
} ,
2021-01-13 14:49:37 -08:00
{
name : 'User' ,
value : 'user' ,
} ,
2019-07-14 08:16:56 -07:00
] ,
default : 'file' ,
description : 'The resource to operate on.' ,
} ,
// ----------------------------------
// operations
// ----------------------------------
2019-06-23 03:35:23 -07:00
{
displayName : 'Operation' ,
name : 'operation' ,
type : 'options' ,
2019-07-14 08:16:56 -07:00
displayOptions : {
show : {
resource : [
'file' ,
] ,
} ,
} ,
2019-06-23 03:35:23 -07:00
options : [
{
name : 'Copy' ,
value : 'copy' ,
2019-07-14 08:16:56 -07:00
description : 'Copy a file' ,
2019-06-23 03:35:23 -07:00
} ,
{
name : 'Delete' ,
value : 'delete' ,
2019-07-14 08:16:56 -07:00
description : 'Delete a file' ,
2019-06-23 03:35:23 -07:00
} ,
{
2019-07-14 08:16:56 -07:00
name : 'Download' ,
value : 'download' ,
description : 'Download a file' ,
2019-06-23 03:35:23 -07:00
} ,
{
name : 'Move' ,
value : 'move' ,
2019-07-14 08:16:56 -07:00
description : 'Move a file' ,
2019-06-23 03:35:23 -07:00
} ,
{
2019-07-14 08:16:56 -07:00
name : 'Upload' ,
value : 'upload' ,
description : 'Upload a file' ,
2019-06-23 03:35:23 -07:00
} ,
] ,
2019-07-14 08:16:56 -07:00
default : 'upload' ,
2019-06-23 03:35:23 -07:00
description : 'The operation to perform.' ,
} ,
2019-07-14 08:16:56 -07:00
{
displayName : 'Operation' ,
name : 'operation' ,
type : 'options' ,
displayOptions : {
show : {
resource : [
'folder' ,
] ,
} ,
} ,
options : [
{
name : 'Copy' ,
value : 'copy' ,
description : 'Copy a folder' ,
} ,
{
name : 'Create' ,
value : 'create' ,
description : 'Create a folder' ,
} ,
{
name : 'Delete' ,
value : 'delete' ,
description : 'Delete a folder' ,
} ,
{
name : 'List' ,
value : 'list' ,
2020-09-14 00:58:01 -07:00
description : 'Return the contents of a given folder' ,
2019-07-14 08:16:56 -07:00
} ,
{
name : 'Move' ,
value : 'move' ,
description : 'Move a folder' ,
} ,
] ,
default : 'create' ,
description : 'The operation to perform.' ,
2019-07-14 09:40:37 -07:00
} ,
2019-07-14 08:16:56 -07:00
2021-01-13 14:49:37 -08:00
{
displayName : 'Operation' ,
name : 'operation' ,
type : 'options' ,
displayOptions : {
show : {
resource : [
'user' ,
] ,
} ,
} ,
options : [
{
2021-01-18 13:55:07 -08:00
name : 'Create' ,
2021-01-22 11:58:06 -08:00
value : 'create' ,
2021-01-18 13:55:07 -08:00
description : 'Invite a user to a NextCloud organization' ,
2021-01-13 14:49:37 -08:00
} ,
] ,
2021-01-18 13:55:07 -08:00
default : 'create' ,
2021-01-13 14:49:37 -08:00
description : 'The operation to perform.' ,
} ,
2019-07-14 08:16:56 -07:00
// ----------------------------------
// file
2019-06-23 03:35:23 -07:00
// ----------------------------------
2019-07-14 08:16:56 -07:00
// ----------------------------------
// file/folder:copy
2019-06-23 03:35:23 -07:00
// ----------------------------------
{
displayName : 'From Path' ,
name : 'path' ,
type : 'string' ,
default : '' ,
required : true ,
displayOptions : {
show : {
operation : [
2020-10-22 06:46:03 -07:00
'copy' ,
2019-06-23 03:35:23 -07:00
] ,
2019-07-14 08:16:56 -07:00
resource : [
'file' ,
'folder' ,
] ,
2019-06-23 03:35:23 -07:00
} ,
} ,
placeholder : '/invoices/original.txt' ,
2021-04-03 07:59:07 -07:00
description : 'The path of file or folder to copy. The path should start with "/"' ,
2019-06-23 03:35:23 -07:00
} ,
{
displayName : 'To Path' ,
name : 'toPath' ,
type : 'string' ,
default : '' ,
required : true ,
displayOptions : {
show : {
operation : [
2020-10-22 06:46:03 -07:00
'copy' ,
2019-06-23 03:35:23 -07:00
] ,
2019-07-14 08:16:56 -07:00
resource : [
'file' ,
'folder' ,
] ,
2019-06-23 03:35:23 -07:00
} ,
} ,
placeholder : '/invoices/copy.txt' ,
2021-04-03 07:59:07 -07:00
description : 'The destination path of file or folder. The path should start with "/"' ,
2019-06-23 03:35:23 -07:00
} ,
// ----------------------------------
2019-07-14 08:16:56 -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 : {
operation : [
2020-10-22 06:46:03 -07:00
'delete' ,
2019-06-23 03:35:23 -07:00
] ,
2019-07-14 08:16:56 -07:00
resource : [
'file' ,
'folder' ,
] ,
2019-06-23 03:35:23 -07:00
} ,
} ,
2021-04-03 07:59:07 -07:00
placeholder : '/invoices/2019/invoice_1.pdf' ,
description : 'The path to delete. Can be a single file or a whole folder. The path should start with "/"' ,
2019-06-23 03:35:23 -07:00
} ,
// ----------------------------------
2019-07-14 08:16:56 -07:00
// file/folder:move
2019-06-23 03:35:23 -07:00
// ----------------------------------
{
2019-07-14 08:16:56 -07:00
displayName : 'From Path' ,
2019-06-23 03:35:23 -07:00
name : 'path' ,
type : 'string' ,
default : '' ,
required : true ,
displayOptions : {
show : {
operation : [
2020-10-22 06:46:03 -07:00
'move' ,
2019-06-23 03:35:23 -07:00
] ,
2019-07-14 08:16:56 -07:00
resource : [
'file' ,
'folder' ,
2019-06-23 03:35:23 -07:00
] ,
} ,
} ,
2019-07-14 08:16:56 -07:00
placeholder : '/invoices/old_name.txt' ,
2021-04-03 07:59:07 -07:00
description : 'The path of file or folder to move. The path should start with "/"' ,
2019-06-23 03:35:23 -07:00
} ,
{
2019-07-14 08:16:56 -07:00
displayName : 'To Path' ,
name : 'toPath' ,
2019-06-23 03:35:23 -07:00
type : 'string' ,
default : '' ,
required : true ,
displayOptions : {
show : {
operation : [
2020-10-22 06:46:03 -07:00
'move' ,
2019-07-14 08:16:56 -07:00
] ,
resource : [
'file' ,
'folder' ,
2019-06-23 03:35:23 -07:00
] ,
} ,
} ,
2019-07-14 08:16:56 -07:00
placeholder : '/invoices/new_name.txt' ,
2021-04-03 07:59:07 -07:00
description : 'The new path of file or folder. The path should start with "/"' ,
2019-06-23 03:35:23 -07:00
} ,
// ----------------------------------
2019-07-14 08:16:56 -07:00
// file:download
2019-06-23 03:35:23 -07:00
// ----------------------------------
{
2019-07-14 08:16:56 -07:00
displayName : 'File Path' ,
2019-06-23 03:35:23 -07:00
name : 'path' ,
type : 'string' ,
default : '' ,
required : true ,
displayOptions : {
show : {
operation : [
2020-10-22 06:46:03 -07:00
'download' ,
2019-07-14 08:16:56 -07:00
] ,
resource : [
'file' ,
2019-06-23 03:35:23 -07:00
] ,
} ,
} ,
2021-04-03 07:59:07 -07:00
placeholder : '/invoices/2019/invoice_1.pdf' ,
description : 'The file path of the file to download. Has to contain the full path. The path should start with "/"' ,
2019-06-23 03:35:23 -07:00
} ,
{
2019-07-14 08:16:56 -07:00
displayName : 'Binary Property' ,
name : 'binaryPropertyName' ,
2019-06-23 03:35:23 -07:00
type : 'string' ,
2019-07-14 08:16:56 -07:00
default : 'data' ,
2019-06-23 03:35:23 -07:00
required : true ,
displayOptions : {
show : {
operation : [
2020-10-22 06:46:03 -07:00
'download' ,
2019-07-14 08:16:56 -07:00
] ,
resource : [
'file' ,
2019-06-23 03:35:23 -07:00
] ,
} ,
} ,
2019-07-14 08:16:56 -07:00
description : 'Name of the binary property to which to<br />write the data of the read file.' ,
2019-06-23 03:35:23 -07:00
} ,
// ----------------------------------
2019-07-14 08:16:56 -07:00
// file:upload
2019-06-23 03:35:23 -07:00
// ----------------------------------
{
displayName : 'File Path' ,
name : 'path' ,
type : 'string' ,
default : '' ,
required : true ,
displayOptions : {
show : {
operation : [
2020-10-22 06:46:03 -07:00
'upload' ,
2019-07-14 08:16:56 -07:00
] ,
resource : [
'file' ,
2019-06-23 03:35:23 -07:00
] ,
} ,
} ,
2021-04-03 07:59:07 -07:00
placeholder : '/invoices/2019/invoice_1.pdf' ,
description : 'The absolute 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 : 'binaryDataUpload' ,
type : 'boolean' ,
default : false ,
required : true ,
displayOptions : {
show : {
operation : [
2020-10-22 06:46:03 -07:00
'upload' ,
2019-07-14 08:16:56 -07:00
] ,
resource : [
'file' ,
2019-06-23 03:35:23 -07:00
] ,
} ,
} ,
description : '' ,
} ,
{
displayName : 'File Content' ,
name : 'fileContent' ,
type : 'string' ,
default : '' ,
displayOptions : {
show : {
2019-07-14 08:16:56 -07:00
binaryDataUpload : [
2020-10-22 06:46:03 -07:00
false ,
2019-07-14 08:16:56 -07:00
] ,
2019-06-23 03:35:23 -07:00
operation : [
2020-10-22 06:46:03 -07:00
'upload' ,
2019-06-23 03:35:23 -07:00
] ,
2019-07-14 08:16:56 -07:00
resource : [
'file' ,
2019-06-23 03:35:23 -07:00
] ,
} ,
} ,
placeholder : '' ,
description : 'The text content of the file to upload.' ,
} ,
{
displayName : 'Binary Property' ,
name : 'binaryPropertyName' ,
type : 'string' ,
default : 'data' ,
required : true ,
displayOptions : {
show : {
binaryDataUpload : [
2020-10-22 06:46:03 -07:00
true ,
2019-06-23 03:35:23 -07:00
] ,
2019-07-14 08:16:56 -07:00
operation : [
2020-10-22 06:46:03 -07:00
'upload' ,
2019-07-14 08:16:56 -07:00
] ,
resource : [
'file' ,
] ,
2019-06-23 03:35:23 -07:00
} ,
} ,
placeholder : '' ,
description : 'Name of the binary property which contains<br />the data for the file to be uploaded.' ,
} ,
2019-07-14 08:16:56 -07:00
// ----------------------------------
// folder
// ----------------------------------
// ----------------------------------
// folder:create
// ----------------------------------
{
displayName : 'Folder' ,
name : 'path' ,
type : 'string' ,
default : '' ,
required : true ,
displayOptions : {
show : {
operation : [
2020-10-22 06:46:03 -07:00
'create' ,
2019-07-14 08:16:56 -07:00
] ,
resource : [
'folder' ,
] ,
} ,
} ,
2021-04-03 07:59:07 -07:00
placeholder : '/invoices/2019' ,
description : 'The folder to create. The parent folder has to exist. The path should start with "/"' ,
2019-07-14 08:16:56 -07:00
} ,
// ----------------------------------
// folder:list
// ----------------------------------
{
displayName : 'Folder Path' ,
name : 'path' ,
type : 'string' ,
default : '' ,
displayOptions : {
show : {
operation : [
2020-10-22 06:46:03 -07:00
'list' ,
2019-07-14 08:16:56 -07:00
] ,
resource : [
'folder' ,
] ,
} ,
} ,
2021-04-03 07:59:07 -07:00
placeholder : '/invoices/2019/' ,
description : 'The path of which to list the content. The path should start with "/"' ,
2019-07-14 08:16:56 -07:00
} ,
2021-01-13 14:49:37 -08:00
// ----------------------------------
// user
// ----------------------------------
// ----------------------------------
2021-01-18 13:55:07 -08:00
// user:create
2021-01-13 14:49:37 -08:00
// ----------------------------------
{
2021-01-22 11:58:06 -08:00
displayName : 'Username' ,
2021-01-13 14:49:37 -08:00
name : 'userId' ,
type : 'string' ,
default : '' ,
required : true ,
displayOptions : {
show : {
resource : [
'user' ,
] ,
operation : [
2021-01-18 13:55:07 -08:00
'create' ,
2021-01-13 14:49:37 -08:00
] ,
} ,
} ,
placeholder : 'john' ,
2021-01-22 11:58:06 -08:00
description : 'Username the user will have.' ,
2021-01-13 14:49:37 -08:00
} ,
{
displayName : 'Email' ,
name : 'email' ,
type : 'string' ,
default : '' ,
required : true ,
displayOptions : {
show : {
resource : [
'user' ,
] ,
operation : [
2021-01-18 13:55:07 -08:00
'create' ,
2021-01-13 14:49:37 -08:00
] ,
} ,
} ,
placeholder : 'john@email.com' ,
2021-01-18 13:55:07 -08:00
description : 'The email of the user to invite.' ,
2021-01-13 14:49:37 -08:00
} ,
{
displayName : 'Additional Fields' ,
name : 'additionalFields' ,
type : 'collection' ,
2021-01-22 11:58:06 -08:00
placeholder : 'Add Field' ,
2021-01-13 14:49:37 -08:00
default : { } ,
displayOptions : {
show : {
resource : [
'user' ,
] ,
operation : [
2021-01-18 13:55:07 -08:00
'create' ,
2021-01-13 14:49:37 -08:00
] ,
} ,
} ,
options : [
{
displayName : 'Display name' ,
name : 'displayName' ,
type : 'string' ,
default : '' ,
2021-01-18 13:55:07 -08:00
description : 'The display name of the user to invite.' ,
2021-01-13 14:49:37 -08:00
} ,
] ,
} ,
2019-06-23 03:35:23 -07:00
] ,
} ;
async execute ( this : IExecuteFunctions ) : Promise < INodeExecutionData [ ] [ ] > {
2019-08-01 13:55:33 -07:00
const items = this . getInputData ( ) . slice ( ) ;
2019-06-23 03:35:23 -07:00
const returnData : IDataObject [ ] = [ ] ;
2020-06-17 10:38:30 -07:00
const authenticationMethod = this . getNodeParameter ( 'authentication' , 0 ) ;
let credentials ;
if ( authenticationMethod === 'accessToken' ) {
credentials = this . getCredentials ( 'nextCloudApi' ) ;
} else {
credentials = this . getCredentials ( 'nextCloudOAuth2Api' ) ;
}
2019-06-23 03:35:23 -07:00
if ( credentials === undefined ) {
2021-04-16 09:33:36 -07:00
throw new NodeOperationError ( this . getNode ( ) , 'No credentials got returned!' ) ;
2019-06-23 03:35:23 -07:00
}
2019-07-14 08:16:56 -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 = '' ;
2020-03-17 05:18:04 -07:00
let responseData : any ; // tslint:disable-line:no-any
2019-06-23 03:35:23 -07:00
2021-01-13 14:52:15 -08:00
let body : string | Buffer = '' ;
2019-06-23 03:35:23 -07:00
const headers : IDataObject = { } ;
for ( let i = 0 ; i < items . length ; i ++ ) {
2019-07-14 08:16:56 -07:00
if ( resource === 'file' ) {
if ( operation === 'download' ) {
// ----------------------------------
// download
// ----------------------------------
2019-06-23 03:35:23 -07:00
2019-07-14 08:16:56 -07:00
requestMethod = 'GET' ;
endpoint = this . getNodeParameter ( 'path' , i ) as string ;
2019-06-23 03:35:23 -07:00
2019-07-14 08:16:56 -07:00
} else if ( operation === 'upload' ) {
// ----------------------------------
// upload
// ----------------------------------
2019-06-23 03:35:23 -07:00
2019-07-14 08:16:56 -07:00
requestMethod = 'PUT' ;
endpoint = this . getNodeParameter ( 'path' , i ) as string ;
2019-06-23 03:35:23 -07:00
2019-07-14 08:16:56 -07:00
if ( this . getNodeParameter ( 'binaryDataUpload' , i ) === true ) {
// Is binary file to upload
const item = items [ i ] ;
2019-06-23 03:35:23 -07:00
2019-07-14 08:16:56 -07:00
if ( item . binary === undefined ) {
2021-04-16 09:33:36 -07:00
throw new NodeOperationError ( this . getNode ( ) , 'No binary data exists on item!' ) ;
2019-07-14 08:16:56 -07:00
}
const propertyNameUpload = this . getNodeParameter ( 'binaryPropertyName' , i ) as string ;
2019-06-23 03:35:23 -07:00
2019-07-14 08:16:56 -07:00
if ( item . binary [ propertyNameUpload ] === undefined ) {
2021-04-16 09:33:36 -07:00
throw new NodeOperationError ( this . getNode ( ) , ` No binary data property " ${ propertyNameUpload } " does not exists on item! ` ) ;
2019-07-14 08:16:56 -07:00
}
2019-06-23 03:35:23 -07:00
2019-07-14 08:16:56 -07:00
body = Buffer . from ( item . binary [ propertyNameUpload ] . data , BINARY_ENCODING ) ;
} else {
// Is text file
body = this . getNodeParameter ( 'fileContent' , i ) as string ;
}
}
} else if ( resource === 'folder' ) {
if ( operation === 'create' ) {
// ----------------------------------
// create
// ----------------------------------
2019-06-23 03:35:23 -07:00
2019-07-14 08:16:56 -07:00
requestMethod = 'MKCOL' ;
endpoint = this . getNodeParameter ( 'path' , i ) as string ;
2019-06-23 03:35:23 -07:00
2019-07-14 08:16:56 -07:00
} else if ( operation === 'list' ) {
// ----------------------------------
// list
// ----------------------------------
2019-06-23 03:35:23 -07:00
2019-07-14 08:16:56 -07:00
requestMethod = 'PROPFIND' ;
endpoint = this . getNodeParameter ( 'path' , i ) as string ;
2019-06-23 03:35:23 -07:00
2019-07-14 08:16:56 -07:00
}
}
2019-06-23 03:35:23 -07:00
2019-07-14 08:16:56 -07:00
if ( [ 'file' , 'folder' ] . includes ( resource ) ) {
if ( operation === 'copy' ) {
// ----------------------------------
// copy
// ----------------------------------
2019-06-23 03:35:23 -07:00
2019-07-14 08:16:56 -07:00
requestMethod = 'COPY' ;
endpoint = this . getNodeParameter ( 'path' , i ) as string ;
const toPath = this . getNodeParameter ( 'toPath' , i ) as string ;
headers . Destination = ` ${ credentials . webDavUrl } / ${ encodeURI ( toPath ) } ` ;
2019-06-23 03:35:23 -07:00
2019-07-14 08:16:56 -07:00
} else if ( operation === 'delete' ) {
// ----------------------------------
// delete
// ----------------------------------
2019-06-23 03:35:23 -07:00
2019-07-14 08:16:56 -07:00
requestMethod = 'DELETE' ;
endpoint = this . getNodeParameter ( 'path' , i ) as string ;
2019-06-23 03:35:23 -07:00
2019-07-14 08:16:56 -07:00
} else if ( operation === 'move' ) {
// ----------------------------------
// move
// ----------------------------------
2019-06-23 03:35:23 -07:00
2019-07-14 08:16:56 -07:00
requestMethod = 'MOVE' ;
endpoint = this . getNodeParameter ( 'path' , i ) as string ;
const toPath = this . getNodeParameter ( 'toPath' , i ) as string ;
headers . Destination = ` ${ credentials . webDavUrl } / ${ encodeURI ( toPath ) } ` ;
2019-06-23 03:35:23 -07:00
}
2021-01-13 14:49:37 -08:00
} else if ( resource === 'user' ) {
2021-01-18 13:55:07 -08:00
if ( operation === 'create' ) {
2021-01-13 14:49:37 -08:00
// ----------------------------------
2021-01-18 13:55:07 -08:00
// user:create
2021-01-13 14:49:37 -08:00
// ----------------------------------
requestMethod = 'POST' ;
2021-01-15 03:58:51 -08:00
endpoint = 'ocs/v1.php/cloud/users' ;
2021-01-13 14:49:37 -08:00
headers [ 'OCS-APIRequest' ] = true ;
headers [ 'Content-Type' ] = 'application/x-www-form-urlencoded' ;
const userid = this . getNodeParameter ( 'userId' , i ) as string ;
const email = this . getNodeParameter ( 'email' , i ) as string ;
body = ` userid= ${ userid } &email= ${ email } ` ;
const additionalFields = this . getNodeParameter ( 'additionalFields' , i ) as IDataObject ;
if ( additionalFields . displayName ) {
body += ` &displayName= ${ additionalFields . displayName } ` ;
}
}
2019-06-23 03:35:23 -07:00
} else {
2021-04-16 09:33:36 -07:00
throw new NodeOperationError ( this . getNode ( ) , ` The resource " ${ resource } " is not known! ` ) ;
2019-06-23 03:35:23 -07:00
}
// Make sure that the webdav URL does never have a trailing slash because
// one gets added always automatically
let webDavUrl = credentials . webDavUrl as string ;
if ( webDavUrl . slice ( - 1 ) === '/' ) {
webDavUrl = webDavUrl . slice ( 0 , - 1 ) ;
}
2020-06-17 10:38:30 -07:00
let encoding = undefined ;
2019-07-14 08:16:56 -07:00
if ( resource === 'file' && operation === 'download' ) {
2019-06-23 03:35:23 -07:00
// Return the data as a buffer
2020-06-17 10:38:30 -07:00
encoding = null ;
2019-06-23 03:35:23 -07:00
}
2020-03-17 05:18:04 -07:00
try {
2020-06-17 10:38:30 -07:00
responseData = await nextCloudApiRequest . call ( this , requestMethod , endpoint , body , headers , encoding ) ;
2020-03-17 05:18:04 -07:00
} catch ( error ) {
if ( this . continueOnFail ( ) === true ) {
returnData . push ( { error } ) ;
continue ;
}
throw error ;
}
2019-06-23 03:35:23 -07:00
2019-07-14 08:16:56 -07:00
if ( resource === 'file' && operation === 'download' ) {
2019-08-01 13:55:33 -07:00
const newItem : INodeExecutionData = {
json : items [ i ] . json ,
binary : { } ,
} ;
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-06-23 03:35:23 -07:00
}
2019-08-01 13:55:33 -07:00
items [ i ] = newItem ;
2019-06-23 03:35:23 -07:00
const binaryPropertyName = this . getNodeParameter ( 'binaryPropertyName' , i ) as string ;
items [ i ] . binary ! [ binaryPropertyName ] = await this . helpers . prepareBinaryData ( responseData , endpoint ) ;
2021-01-18 13:42:54 -08:00
2021-01-18 13:55:07 -08:00
} else if ( resource === 'user' && operation === 'create' ) {
2021-01-18 13:42:54 -08:00
const jsonResponseData : IDataObject = await new Promise ( ( resolve , reject ) = > {
parseString ( responseData , { explicitArray : false } , ( err , data ) = > {
if ( err ) {
return reject ( err ) ;
}
2021-01-23 03:57:30 -08:00
if ( data . ocs . meta . status !== 'ok' ) {
return reject ( new Error ( data . ocs . meta . message ) ) ;
}
resolve ( data . ocs . data as IDataObject ) ;
2021-01-18 13:42:54 -08:00
} ) ;
} ) ;
returnData . push ( jsonResponseData as IDataObject ) ;
2019-07-14 08:16:56 -07:00
} else if ( resource === 'folder' && operation === 'list' ) {
2019-06-23 03:35:23 -07:00
const jsonResponseData : IDataObject = await new Promise ( ( resolve , reject ) = > {
parseString ( responseData , { explicitArray : false } , ( err , data ) = > {
if ( err ) {
return reject ( err ) ;
}
resolve ( data as IDataObject ) ;
} ) ;
} ) ;
const propNames : { [ key : string ] : string } = {
'd:getlastmodified' : 'lastModified' ,
'd:getcontentlength' : 'contentLength' ,
'd:getcontenttype' : 'contentType' ,
} ;
if ( jsonResponseData [ 'd:multistatus' ] !== undefined &&
jsonResponseData [ 'd:multistatus' ] !== null &&
( jsonResponseData [ 'd:multistatus' ] as IDataObject ) [ 'd:response' ] !== undefined &&
( jsonResponseData [ 'd:multistatus' ] as IDataObject ) [ 'd:response' ] !== null ) {
let skippedFirst = false ;
// @ts-ignore
for ( const item of jsonResponseData [ 'd:multistatus' ] [ 'd:response' ] ) {
if ( skippedFirst === false ) {
skippedFirst = true ;
continue ;
}
const newItem : IDataObject = { } ;
newItem . path = item [ 'd:href' ] . slice ( 19 ) ;
const props = item [ 'd:propstat' ] [ 0 ] [ 'd:prop' ] ;
// Get the props and save them under a proper name
for ( const propName of Object . keys ( propNames ) ) {
if ( props [ propName ] !== undefined ) {
newItem [ propNames [ propName ] ] = props [ propName ] ;
}
}
if ( props [ 'd:resourcetype' ] === '' ) {
newItem . type = 'file' ;
} else {
newItem . type = 'folder' ;
}
newItem . eTag = props [ 'd:getetag' ] . slice ( 1 , - 1 ) ;
returnData . push ( newItem as IDataObject ) ;
}
}
} else {
returnData . push ( responseData as IDataObject ) ;
}
2020-03-17 05:18:04 -07:00
2019-06-23 03:35:23 -07:00
}
2019-07-14 08:16:56 -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 ) ;
2021-01-23 03:57:30 -08:00
} else {
2019-06-23 03:35:23 -07:00
// For all other ones does the output get replaced
return [ this . helpers . returnJsonArray ( returnData ) ] ;
}
}
}