UNPKG

n8n-nodes-putio

Version:
331 lines 15 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Putio = void 0; const n8n_workflow_1 = require("n8n-workflow"); const GenericFunctions_1 = require("./GenericFunctions"); class Putio { constructor() { this.description = { displayName: 'Put.io', name: 'putio', icon: 'file:icons/putio.svg', group: ['transform'], version: 1, subtitle: '={{$parameter["operation"]}}', description: 'Interact with Put.io API', defaults: { name: 'Put.io', }, inputs: ['main'], outputs: ['main'], credentials: [ { name: 'putioApi', required: true, }, ], properties: [ { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, options: [ { name: 'List Files', value: 'listFiles', description: 'List files and folders', action: 'List files and folders', }, { name: 'Get File', value: 'getFile', description: 'Get file details', action: 'Get file details', }, { name: 'Download File', value: 'downloadFile', description: 'Download a file', action: 'Download a file', }, { name: 'Create Folder', value: 'createFolder', description: 'Create a new folder', action: 'Create a new folder', }, { name: 'Upload File', value: 'uploadFile', description: 'Upload a file to Put.io', action: 'Upload a file', }, ], default: 'listFiles', }, { displayName: 'Folder ID', name: 'folderId', type: 'string', default: '0', description: 'The ID of the folder to list files from (use 0 for root directory)', displayOptions: { show: { operation: [ 'listFiles', ], }, }, }, { displayName: 'Selection Method', name: 'selectionMethod', type: 'options', options: [ { name: 'File ID', value: 'id', description: 'Select file using its ID', }, { name: 'File Path', value: 'path', description: 'Select file using its path', }, ], default: 'id', displayOptions: { show: { operation: [ 'getFile', 'downloadFile', ], }, }, }, { displayName: 'File ID', name: 'fileId', type: 'string', default: '', description: 'The ID of the file to get or download', displayOptions: { show: { operation: [ 'getFile', 'downloadFile', ], selectionMethod: [ 'id', ], }, }, }, { displayName: 'File Path', name: 'filePath', type: 'string', default: '', placeholder: '/folder/subfolder/file.txt', description: 'The full path to the file (e.g., /folder/subfolder/file.txt)', displayOptions: { show: { operation: [ 'getFile', 'downloadFile', ], selectionMethod: [ 'path', ], }, }, }, { displayName: 'Folder Name', name: 'folderName', type: 'string', default: '', description: 'The name of the folder to create', displayOptions: { show: { operation: [ 'createFolder', ], }, }, }, { displayName: 'Parent Folder ID', name: 'parentFolderId', type: 'string', default: '0', description: 'The ID of the parent folder where the new folder will be created', displayOptions: { show: { operation: [ 'createFolder', ], }, }, }, { displayName: 'Binary Property', name: 'binaryPropertyName', type: 'string', default: 'data', description: 'Name of the binary property which contains the data for the file to be uploaded', displayOptions: { show: { operation: [ 'uploadFile', ], }, }, }, { displayName: 'File Name', name: 'fileName', type: 'string', default: '', description: 'Name of the file to be uploaded', displayOptions: { show: { operation: [ 'uploadFile', ], }, }, }, { displayName: 'Parent Folder ID', name: 'uploadParentFolderId', type: 'string', default: '0', description: 'The ID of the parent folder where the file will be uploaded', displayOptions: { show: { operation: [ 'uploadFile', ], }, }, }, ], }; } execute() { return __awaiter(this, void 0, void 0, function* () { const items = this.getInputData(); const returnData = []; const length = items.length; let responseData; const operation = this.getNodeParameter('operation', 0); for (let i = 0; i < length; i++) { try { if (operation === 'listFiles') { const folderId = this.getNodeParameter('folderId', i); console.log('Put.io List Files - Folder ID:', folderId); const parentId = parseInt(folderId, 10); if (isNaN(parentId)) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Invalid folder ID: must be a number'); } const response = yield GenericFunctions_1.putioApiRequest.call(this, 'GET', '/files/list', {}, { parent_id: parentId }); const files = response.files || []; const folders = files.filter((item) => item.file_type === 'FOLDER'); const nonFolders = files.filter((item) => item.file_type !== 'FOLDER'); returnData.push({ files: nonFolders, folders: folders, }); } else if (operation === 'getFile') { const selectionMethod = this.getNodeParameter('selectionMethod', i); let fileId; if (selectionMethod === 'path') { const filePath = this.getNodeParameter('filePath', i); const response = yield GenericFunctions_1.putioApiRequest.call(this, 'GET', '/files/list', {}, { path: filePath }); if (!response.files || response.files.length === 0) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `File not found at path: ${filePath}`); } fileId = response.files[0].id; } else { fileId = this.getNodeParameter('fileId', i); } const response = yield GenericFunctions_1.putioApiRequest.call(this, 'GET', `/files/${fileId}`); returnData.push(response.file); } else if (operation === 'downloadFile') { const selectionMethod = this.getNodeParameter('selectionMethod', i); let fileId; if (selectionMethod === 'path') { const filePath = this.getNodeParameter('filePath', i); const response = yield GenericFunctions_1.putioApiRequest.call(this, 'GET', '/files/list', {}, { path: filePath }); if (!response.files || response.files.length === 0) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `File not found at path: ${filePath}`); } fileId = response.files[0].id; } else { fileId = this.getNodeParameter('fileId', i); } const downloadUrl = yield GenericFunctions_1.putioApiRequest.call(this, 'GET', `/files/${fileId}/download`); returnData.push({ downloadUrl }); } else if (operation === 'createFolder') { const folderName = this.getNodeParameter('folderName', i); const parentFolderId = this.getNodeParameter('parentFolderId', i); const response = yield GenericFunctions_1.putioApiRequest.call(this, 'POST', '/files/create-folder', { name: folderName, parent_id: parentFolderId, }); returnData.push(response); } else if (operation === 'uploadFile') { const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i); const fileName = this.getNodeParameter('fileName', i); const parentFolderId = this.getNodeParameter('uploadParentFolderId', i); if (items[i].binary === undefined) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No binary data exists on item!'); } if (items[i].binary[binaryPropertyName] === undefined) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `No binary data property "${binaryPropertyName}" does not exists on item!`); } const binaryData = items[i].binary[binaryPropertyName]; const formData = { file: { value: Buffer.from(binaryData.data, 'base64'), options: { filename: fileName, }, }, parent_id: parentFolderId, }; const response = yield GenericFunctions_1.putioApiRequest.call(this, 'POST', '/files/upload', {}, {}, formData); returnData.push(response); } } catch (error) { if (this.continueOnFail()) { returnData.push({ error: error.message }); continue; } throw error; } } return [this.helpers.returnJsonArray(returnData)]; }); } } exports.Putio = Putio; //# sourceMappingURL=Putio.node.js.map