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-04-23 15:08:08 -07:00
{
name : 'Delete' ,
value : 'delete' ,
description : 'Delete a user.' ,
} ,
{
name : 'Get' ,
value : 'get' ,
description : 'Retrieve information about a single user.' ,
} ,
{
name : 'Get All' ,
value : 'getAll' ,
description : 'Retrieve a list of users.' ,
} ,
{
name : 'Update' ,
value : 'update' ,
description : 'Edit attributes related to a user.' ,
} ,
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
} ,
] ,
} ,
2021-04-23 15:08:08 -07:00
// ----------------------------------
// user:get/delete/update
// ----------------------------------
{
displayName : 'Username' ,
name : 'userId' ,
type : 'string' ,
default : '' ,
required : true ,
displayOptions : {
show : {
resource : [
'user' ,
] ,
operation : [
'delete' ,
'get' ,
'update' ,
] ,
} ,
} ,
placeholder : 'john' ,
description : 'Username the user will have.' ,
} ,
// ----------------------------------
// user:getAll
// ----------------------------------
{
displayName : 'Return All' ,
name : 'returnAll' ,
type : 'boolean' ,
displayOptions : {
show : {
resource : [
'user' ,
] ,
operation : [
'getAll' ,
] ,
} ,
} ,
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 : [
'user' ,
] ,
operation : [
'getAll' ,
] ,
returnAll : [
false ,
] ,
} ,
} ,
typeOptions : {
minValue : 1 ,
maxValue : 100 ,
} ,
default : 50 ,
description : 'How many results to return.' ,
} ,
{
displayName : 'Options' ,
name : 'options' ,
type : 'collection' ,
placeholder : 'Add Option' ,
default : { } ,
displayOptions : {
show : {
resource : [
'user' ,
] ,
operation : [
'getAll' ,
] ,
} ,
} ,
options : [
{
displayName : 'Search' ,
name : 'search' ,
type : 'string' ,
default : '' ,
description : 'Optional search string.' ,
} ,
{
displayName : 'Offset' ,
name : 'offset' ,
type : 'number' ,
default : '' ,
description : 'Optional offset value.' ,
} ,
] ,
} ,
// ----------------------------------
// user:update
// ----------------------------------
{
2021-04-23 15:11:44 -07:00
displayName : 'Update Fields' ,
name : 'updateFields' ,
2021-04-23 15:08:08 -07:00
type : 'fixedCollection' ,
2021-04-23 15:11:44 -07:00
typeOptions : {
multipleValues : false ,
2021-04-23 15:08:08 -07:00
} ,
placeholder : 'Add Option' ,
default : { } ,
displayOptions : {
show : {
resource : [
'user' ,
] ,
operation : [
'update' ,
] ,
} ,
} ,
options : [
{
2021-04-23 15:11:44 -07:00
displayName : 'Fields' ,
name : 'field' ,
2021-04-23 15:08:08 -07:00
values : [
{
displayName : 'Key' ,
name : 'key' ,
type : 'options' ,
default : 'email' ,
options :
2021-04-23 15:11:44 -07:00
[
{
name : 'Address' ,
value : 'address' ,
description : 'The new address for the user.' ,
} ,
{
name : 'Display Name' ,
value : 'displayname' ,
description : 'The new display name for the user.' ,
} ,
{
name : 'Email' ,
value : 'email' ,
description : 'The new email for the user.' ,
} ,
{
name : 'Password' ,
value : 'password' ,
description : 'The new password for the user.' ,
} ,
{
name : 'Twitter' ,
value : 'twitter' ,
description : 'The new twitter handle for the user.' ,
} ,
{
name : 'Website' ,
value : 'website' ,
description : 'The new website for the user.' ,
} ,
] ,
description : 'Key of the updated attribute.' ,
2021-04-23 15:08:08 -07:00
} ,
{
displayName : 'Value' ,
name : 'value' ,
type : 'string' ,
default : '' ,
2021-04-23 15:11:44 -07:00
description : 'Value of the updated attribute.' ,
2021-04-23 15:08:08 -07: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' ) {
2021-08-20 09:57:30 -07:00
credentials = await this . getCredentials ( 'nextCloudApi' ) ;
2020-06-17 10:38:30 -07:00
} else {
2021-08-20 09:57:30 -07:00
credentials = await this . getCredentials ( 'nextCloudOAuth2Api' ) ;
2020-06-17 10:38:30 -07:00
}
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-04-23 15:08:08 -07:00
let body : string | Buffer | IDataObject = '' ;
2019-06-23 03:35:23 -07:00
const headers : IDataObject = { } ;
2021-04-23 15:08:08 -07:00
let qs ;
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 {
if ( resource === 'file' ) {
if ( operation === 'download' ) {
// ----------------------------------
// download
// ----------------------------------
2019-06-23 03:35:23 -07:00
2021-07-19 23:58:54 -07:00
requestMethod = 'GET' ;
endpoint = this . getNodeParameter ( 'path' , i ) as string ;
2019-06-23 03:35:23 -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 = 'PUT' ;
endpoint = this . getNodeParameter ( 'path' , i ) as string ;
2019-06-23 03:35:23 -07:00
2021-07-19 23:58:54 -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
2021-07-19 23:58:54 -07:00
if ( item . binary === undefined ) {
throw new NodeOperationError ( this . getNode ( ) , 'No binary data exists on item!' ) ;
}
2019-07-14 08:16:56 -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 = this . getNodeParameter ( 'fileContent' , i ) as string ;
}
2019-07-14 08:16:56 -07:00
}
2021-07-19 23:58:54 -07:00
} else if ( resource === 'folder' ) {
if ( operation === 'create' ) {
// ----------------------------------
// create
// ----------------------------------
2019-06-23 03:35:23 -07:00
2021-07-19 23:58:54 -07:00
requestMethod = 'MKCOL' ;
endpoint = this . getNodeParameter ( 'path' , i ) as string ;
2019-06-23 03:35:23 -07: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
requestMethod = 'PROPFIND' ;
endpoint = this . getNodeParameter ( 'path' , i ) as string ;
2019-06-23 03:35:23 -07:00
2021-07-19 23:58:54 -07:00
}
2019-07-14 08:16:56 -07:00
}
2019-06-23 03:35:23 -07:00
2021-07-19 23:58:54 -07:00
if ( [ 'file' , 'folder' ] . includes ( resource ) ) {
if ( operation === 'copy' ) {
// ----------------------------------
// copy
// ----------------------------------
2019-06-23 03:35:23 -07:00
2021-07-19 23:58:54 -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
2021-07-19 23:58:54 -07:00
} else if ( operation === 'delete' ) {
// ----------------------------------
// delete
// ----------------------------------
2019-06-23 03:35:23 -07:00
2021-07-19 23:58:54 -07:00
requestMethod = 'DELETE' ;
endpoint = this . getNodeParameter ( 'path' , i ) as string ;
2019-06-23 03:35:23 -07:00
2021-07-19 23:58:54 -07:00
} else if ( operation === 'move' ) {
// ----------------------------------
// move
// ----------------------------------
2019-06-23 03:35:23 -07:00
2021-07-19 23:58:54 -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-07-19 23:58:54 -07:00
}
2021-01-13 14:49:37 -08:00
2021-07-19 23:58:54 -07:00
} else if ( resource === 'user' ) {
if ( operation === 'create' ) {
// ----------------------------------
// user:create
// ----------------------------------
2021-04-23 15:11:44 -07:00
2021-07-19 23:58:54 -07:00
requestMethod = 'POST' ;
2021-01-13 14:49:37 -08:00
2021-07-19 23:58:54 -07:00
endpoint = 'ocs/v1.php/cloud/users' ;
2021-01-13 14:49:37 -08:00
2021-07-19 23:58:54 -07:00
headers [ 'OCS-APIRequest' ] = true ;
headers [ 'Content-Type' ] = 'application/x-www-form-urlencoded' ;
2021-01-13 14:49:37 -08:00
2021-07-19 23:58:54 -07:00
const userid = this . getNodeParameter ( 'userId' , i ) as string ;
const email = this . getNodeParameter ( 'email' , i ) as string ;
2021-01-13 14:49:37 -08:00
2021-07-19 23:58:54 -07:00
body = ` userid= ${ userid } &email= ${ email } ` ;
2021-01-13 14:49:37 -08:00
2021-07-19 23:58:54 -07:00
const additionalFields = this . getNodeParameter ( 'additionalFields' , i ) as IDataObject ;
2021-01-13 14:49:37 -08:00
2021-07-19 23:58:54 -07:00
if ( additionalFields . displayName ) {
body += ` &displayName= ${ additionalFields . displayName } ` ;
}
2021-01-13 14:49:37 -08:00
}
2021-07-19 23:58:54 -07:00
if ( operation === 'delete' ) {
// ----------------------------------
// user:delete
// ----------------------------------
2021-04-23 15:11:44 -07:00
2021-07-19 23:58:54 -07:00
requestMethod = 'DELETE' ;
2021-04-23 15:08:08 -07:00
2021-07-19 23:58:54 -07:00
const userid = this . getNodeParameter ( 'userId' , i ) as string ;
endpoint = ` ocs/v1.php/cloud/users/ ${ userid } ` ;
2021-04-23 15:08:08 -07:00
2021-07-19 23:58:54 -07:00
headers [ 'OCS-APIRequest' ] = true ;
headers [ 'Content-Type' ] = 'application/x-www-form-urlencoded' ;
}
if ( operation === 'get' ) {
// ----------------------------------
// user:get
// ----------------------------------
2021-04-23 15:11:44 -07:00
2021-07-19 23:58:54 -07:00
requestMethod = 'GET' ;
2021-04-23 15:08:08 -07:00
2021-07-19 23:58:54 -07:00
const userid = this . getNodeParameter ( 'userId' , i ) as string ;
endpoint = ` ocs/v1.php/cloud/users/ ${ userid } ` ;
2021-04-23 15:08:08 -07:00
2021-07-19 23:58:54 -07:00
headers [ 'OCS-APIRequest' ] = true ;
headers [ 'Content-Type' ] = 'application/x-www-form-urlencoded' ;
2021-04-23 15:08:08 -07:00
}
2021-07-19 23:58:54 -07:00
if ( operation === 'getAll' ) {
// ----------------------------------
// user:getAll
// ----------------------------------
requestMethod = 'GET' ;
const returnAll = this . getNodeParameter ( 'returnAll' , i ) as boolean ;
qs = this . getNodeParameter ( 'options' , i ) as IDataObject ;
if ( ! returnAll ) {
qs . limit = this . getNodeParameter ( 'limit' , i ) as number ;
}
endpoint = ` ocs/v1.php/cloud/users ` ;
2021-04-23 15:08:08 -07:00
2021-07-19 23:58:54 -07:00
headers [ 'OCS-APIRequest' ] = true ;
headers [ 'Content-Type' ] = 'application/x-www-form-urlencoded' ;
}
if ( operation === 'update' ) {
// ----------------------------------
// user:update
// ----------------------------------
2021-04-23 15:11:44 -07:00
2021-07-19 23:58:54 -07:00
requestMethod = 'PUT' ;
2021-01-13 14:49:37 -08:00
2021-07-19 23:58:54 -07:00
const userid = this . getNodeParameter ( 'userId' , i ) as string ;
endpoint = ` ocs/v1.php/cloud/users/ ${ userid } ` ;
2021-04-23 15:08:08 -07:00
2021-07-19 23:58:54 -07:00
body = Object . entries ( ( this . getNodeParameter ( 'updateFields' , i ) as IDataObject ) . field as IDataObject ) . map ( entry = > {
const [ key , value ] = entry ;
return ` ${ key } = ${ value } ` ;
} ) . join ( '&' ) ;
2021-04-23 15:08:08 -07:00
2021-07-19 23:58:54 -07:00
headers [ 'OCS-APIRequest' ] = true ;
headers [ 'Content-Type' ] = 'application/x-www-form-urlencoded' ;
}
} else {
throw new NodeOperationError ( this . getNode ( ) , ` The resource " ${ resource } " is not known! ` ) ;
2021-04-23 15:08:08 -07:00
}
2019-06-23 03:35:23 -07:00
2021-07-19 23:58:54 -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 ) ;
}
2019-06-23 03:35:23 -07:00
2021-07-19 23:58:54 -07:00
let encoding = undefined ;
if ( resource === 'file' && operation === 'download' ) {
// Return the data as a buffer
encoding = null ;
}
2019-06-23 03:35:23 -07:00
2021-07-19 23:58:54 -07:00
try {
responseData = await nextCloudApiRequest . call ( this , requestMethod , endpoint , body , headers , encoding , qs ) ;
} catch ( error ) {
if ( this . continueOnFail ( ) ) {
if ( resource === 'file' && operation === 'download' ) {
items [ i ] . json = { error : error.message } ;
} else {
returnData . push ( { error : error.message } ) ;
}
continue ;
}
throw error ;
2020-03-17 05:18:04 -07:00
}
2021-07-19 23:58:54 -07:00
if ( resource === 'file' && operation === 'download' ) {
2019-06-23 03:35:23 -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 binaryPropertyName = this . getNodeParameter ( 'binaryPropertyName' , i ) as string ;
2019-08-01 13:55:33 -07:00
2021-07-19 23:58:54 -07:00
items [ i ] . binary ! [ binaryPropertyName ] = await this . helpers . prepareBinaryData ( responseData , endpoint ) ;
2019-06-23 03:35:23 -07:00
2021-07-19 23:58:54 -07:00
} else if ( resource === 'user' ) {
2021-01-18 13:42:54 -08:00
2021-07-19 23:58:54 -07:00
if ( operation !== 'getAll' ) {
2021-01-18 13:42:54 -08:00
2021-07-19 23:58:54 -07: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
2021-07-19 23:58:54 -07:00
if ( data . ocs . meta . status !== 'ok' ) {
return reject ( new Error ( data . ocs . meta . message || data . ocs . meta . status ) ) ;
}
2021-01-23 03:57:30 -08:00
2021-07-19 23:58:54 -07:00
if ( operation === 'delete' || operation === 'update' ) {
resolve ( data . ocs . meta as IDataObject ) ;
} else {
resolve ( data . ocs . data as IDataObject ) ;
}
} ) ;
} ) ;
2021-04-23 15:08:08 -07:00
2021-07-19 23:58:54 -07:00
returnData . push ( jsonResponseData as IDataObject ) ;
} else {
const jsonResponseData : IDataObject [ ] = await new Promise ( ( resolve , reject ) = > {
parseString ( responseData , { explicitArray : false } , ( err , data ) = > {
if ( err ) {
return reject ( err ) ;
}
if ( data . ocs . meta . status !== 'ok' ) {
return reject ( new Error ( data . ocs . meta . message ) ) ;
}
if ( typeof ( data . ocs . data . users . element ) === 'string' ) {
resolve ( [ data . ocs . data . users . element ] as IDataObject [ ] ) ;
} else {
resolve ( data . ocs . data . users . element as IDataObject [ ] ) ;
}
} ) ;
2021-04-23 15:08:08 -07:00
} ) ;
2021-01-18 13:42:54 -08:00
2021-07-19 23:58:54 -07:00
jsonResponseData . forEach ( value = > {
returnData . push ( { id : value } as IDataObject ) ;
} ) ;
}
} else if ( resource === 'folder' && operation === 'list' ) {
2021-04-23 15:08:08 -07:00
2021-07-19 23:58:54 -07:00
const jsonResponseData : IDataObject = await new Promise ( ( resolve , reject ) = > {
2021-04-23 15:08:08 -07:00
parseString ( responseData , { explicitArray : false } , ( err , data ) = > {
if ( err ) {
return reject ( err ) ;
}
2021-07-19 23:58:54 -07:00
resolve ( data as IDataObject ) ;
2021-04-23 15:08:08 -07:00
} ) ;
} ) ;
2021-07-19 23:58:54 -07:00
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
2021-08-08 02:47:23 -07:00
if ( Array . isArray ( jsonResponseData [ 'd:multistatus' ] [ 'd:response' ] ) ) {
// @ts-ignore
for ( const item of jsonResponseData [ 'd:multistatus' ] [ 'd:response' ] ) {
if ( skippedFirst === false ) {
skippedFirst = true ;
continue ;
}
const newItem : IDataObject = { } ;
2019-06-23 03:35:23 -07:00
2021-08-08 02:47:23 -07:00
newItem . path = item [ 'd:href' ] . slice ( 19 ) ;
2019-06-23 03:35:23 -07:00
2021-08-08 02:47:23 -07:00
const props = item [ 'd:propstat' ] [ 0 ] [ 'd:prop' ] ;
2019-06-23 03:35:23 -07:00
2021-08-08 02:47:23 -07:00
// 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 ] ;
}
2021-07-19 23:58:54 -07:00
}
2019-06-23 03:35:23 -07:00
2021-08-08 02:47:23 -07:00
if ( props [ 'd:resourcetype' ] === '' ) {
newItem . type = 'file' ;
} else {
newItem . type = 'folder' ;
}
newItem . eTag = props [ 'd:getetag' ] . slice ( 1 , - 1 ) ;
2019-06-23 03:35:23 -07:00
2021-08-08 02:47:23 -07:00
returnData . push ( newItem as IDataObject ) ;
}
2019-06-23 03:35:23 -07:00
}
}
2021-08-08 02:47:23 -07:00
2021-07-19 23:58:54 -07:00
} else {
returnData . push ( responseData as IDataObject ) ;
}
} catch ( error ) {
if ( this . continueOnFail ( ) ) {
if ( resource === 'file' && operation === 'download' ) {
items [ i ] . json = { error : error.message } ;
} else {
returnData . push ( { error : error.message } ) ;
}
continue ;
2019-06-23 03:35:23 -07:00
}
2021-07-19 23:58:54 -07:00
throw error ;
2019-06-23 03:35:23 -07:00
}
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 ) ] ;
}
}
}