@tuanltntu/n8n-nodes-bitrix24
Version:
Comprehensive n8n community node for Bitrix24 API integration with CRM, Tasks, Chat, Telephony, and more
481 lines • 19.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DiskResourceHandler = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const ResourceHandlerBase_1 = require("./ResourceHandlerBase");
/**
* Handles Disk operations in Bitrix24
*/
class DiskResourceHandler extends ResourceHandlerBase_1.ResourceHandlerBase {
constructor(executeFunctions, returnData, options = {}) {
super(executeFunctions, returnData, options);
this.resourceEndpoints = {
storage: {
list: "disk.storage.getlist",
get: "disk.storage.get",
},
folder: {
list: "disk.folder.getchildren",
get: "disk.folder.get",
add: "disk.folder.addsubfolder",
update: "disk.folder.update",
delete: "disk.folder.deletefolder",
copy: "disk.folder.copyto",
move: "disk.folder.moveto",
rename: "disk.folder.rename",
},
file: {
list: "disk.folder.getfiles",
get: "disk.file.get",
add: "disk.file.uploadfile",
download: "disk.file.download",
delete: "disk.file.delete",
copy: "disk.file.copyto",
move: "disk.file.moveto",
rename: "disk.file.rename",
},
sharing: {
share: "disk.sharing.add",
list: "disk.sharing.getlist",
get: "disk.sharing.get",
update: "disk.sharing.update",
},
};
}
/**
* Process all items with disk operations
*/
async process() {
for (let itemIndex = 0; itemIndex < this.items.length; itemIndex++) {
try {
const operation = this.getNodeParameter("operation", itemIndex);
switch (operation) {
// New operations matching DiskDescription
case "uploadFile":
await this.handleUploadFile(itemIndex);
break;
case "downloadFile":
await this.handleDownloadFile(itemIndex);
break;
case "deleteFile":
await this.handleDeleteFile(itemIndex);
break;
case "getFileInfo":
await this.handleGetFile(itemIndex);
break;
case "listFiles":
await this.handleGetFiles(itemIndex);
break;
case "createFolder":
await this.handleAddFolder(itemIndex);
break;
case "deleteFolder":
await this.handleDeleteFolder(itemIndex);
break;
case "getStorageInfo":
await this.handleGetStorage(itemIndex);
break;
// Legacy operations for backward compatibility
case "getStorages":
await this.handleGetStorages(itemIndex);
break;
case "getStorage":
await this.handleGetStorage(itemIndex);
break;
case "getFolders":
await this.handleGetFolders(itemIndex);
break;
case "getFolder":
await this.handleGetFolder(itemIndex);
break;
case "addFolder":
await this.handleAddFolder(itemIndex);
break;
case "updateFolder":
await this.handleUpdateFolder(itemIndex);
break;
case "copyFolder":
await this.handleCopyFolder(itemIndex);
break;
case "moveFolder":
await this.handleMoveFolder(itemIndex);
break;
case "renameFolder":
await this.handleRenameFolder(itemIndex);
break;
case "getFiles":
await this.handleGetFiles(itemIndex);
break;
case "getFile":
await this.handleGetFile(itemIndex);
break;
case "copyFile":
await this.handleCopyFile(itemIndex);
break;
case "moveFile":
await this.handleMoveFile(itemIndex);
break;
case "renameFile":
await this.handleRenameFile(itemIndex);
break;
case "shareItem":
await this.handleShareItem(itemIndex);
break;
case "getSharedItems":
await this.handleGetSharedItems(itemIndex);
break;
case "getSharingRights":
await this.handleGetSharingRights(itemIndex);
break;
case "updateSharingRights":
await this.handleUpdateSharingRights(itemIndex);
break;
default:
throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), `Operation ${operation} is not supported for resource disk`);
}
}
catch (error) {
if (this.continueOnFail()) {
this.addErrorToReturnData(error, itemIndex);
}
else {
throw error;
}
}
}
return this.returnData;
}
/**
* Handle 'getStorages' operation
*/
async handleGetStorages(itemIndex) {
const endpoint = this.resourceEndpoints.storage.list;
const responseData = await this.makeApiCall(endpoint, {}, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'getStorage' operation
*/
async handleGetStorage(itemIndex) {
const storageId = this.getNodeParameter("storageId", itemIndex);
const requestParams = {
id: storageId,
};
const endpoint = this.resourceEndpoints.storage.get;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'getFolders' operation
*/
async handleGetFolders(itemIndex) {
const folderId = this.getNodeParameter("folderId", itemIndex);
const requestParams = {
id: folderId,
filter: { TYPE: "folder" },
};
const endpoint = this.resourceEndpoints.folder.list;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'getFolder' operation
*/
async handleGetFolder(itemIndex) {
const folderId = this.getNodeParameter("folderId", itemIndex);
const requestParams = {
id: folderId,
};
const endpoint = this.resourceEndpoints.folder.get;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'addFolder' operation
*/
async handleAddFolder(itemIndex) {
const parentFolderId = this.getNodeParameter("parentFolderId", itemIndex);
const folderName = this.getNodeParameter("folderName", itemIndex);
const requestParams = {
id: parentFolderId,
data: {
NAME: folderName,
},
};
const endpoint = this.resourceEndpoints.folder.add;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'updateFolder' operation
*/
async handleUpdateFolder(itemIndex) {
const folderId = this.getNodeParameter("folderId", itemIndex);
const updateFieldsJson = this.getNodeParameter("updateFields", itemIndex);
if (!updateFieldsJson || updateFieldsJson.trim() === "") {
throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Update fields must not be empty", { itemIndex });
}
const fields = this.parseJsonParameter(updateFieldsJson, "updateFields", itemIndex);
const requestParams = {
id: folderId,
fields,
};
const endpoint = this.resourceEndpoints.folder.update;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'deleteFolder' operation
*/
async handleDeleteFolder(itemIndex) {
const folderId = this.getNodeParameter("folderId", itemIndex);
const requestParams = {
id: folderId,
};
const endpoint = this.resourceEndpoints.folder.delete;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'copyFolder' operation
*/
async handleCopyFolder(itemIndex) {
const folderId = this.getNodeParameter("folderId", itemIndex);
const targetFolderId = this.getNodeParameter("targetFolderId", itemIndex);
const requestParams = {
id: folderId,
targetFolderId,
};
const endpoint = this.resourceEndpoints.folder.copy;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'moveFolder' operation
*/
async handleMoveFolder(itemIndex) {
const folderId = this.getNodeParameter("folderId", itemIndex);
const targetFolderId = this.getNodeParameter("targetFolderId", itemIndex);
const requestParams = {
id: folderId,
targetFolderId,
};
const endpoint = this.resourceEndpoints.folder.move;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'renameFolder' operation
*/
async handleRenameFolder(itemIndex) {
const folderId = this.getNodeParameter("folderId", itemIndex);
const folderName = this.getNodeParameter("folderName", itemIndex);
const requestParams = {
id: folderId,
newName: folderName,
};
const endpoint = this.resourceEndpoints.folder.rename;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'getFiles' operation
*/
async handleGetFiles(itemIndex) {
const folderId = this.getNodeParameter("folderId", itemIndex);
const requestParams = {
id: folderId,
};
const endpoint = this.resourceEndpoints.file.list;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'getFile' operation
*/
async handleGetFile(itemIndex) {
const fileId = this.getNodeParameter("fileId", itemIndex);
const requestParams = {
id: fileId,
};
const endpoint = this.resourceEndpoints.file.get;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'uploadFile' operation
*/
async handleUploadFile(itemIndex) {
const folderId = this.getNodeParameter("folderId", itemIndex);
const binaryPropertyName = this.getNodeParameter("binaryPropertyName", itemIndex);
// Ensure binary data exists
if (!this.items[itemIndex].binary ||
!this.items[itemIndex].binary[binaryPropertyName]) {
throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), `No binary data property "${binaryPropertyName}" exists on item!`, { itemIndex });
}
const binaryData = this.items[itemIndex].binary[binaryPropertyName];
const fileName = binaryData.fileName || "file";
const requestParams = {
id: folderId,
fileContent: {
name: fileName,
data: binaryData.data,
contentType: binaryData.mimeType,
},
};
const endpoint = this.resourceEndpoints.file.add;
const responseData = await this.makeApiCall(endpoint, requestParams, { uploadFile: true }, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'downloadFile' operation
*/
async handleDownloadFile(itemIndex) {
const fileId = this.getNodeParameter("fileId", itemIndex);
const binaryPropertyName = this.getNodeParameter("binaryPropertyName", itemIndex);
const requestParams = {
id: fileId,
};
// Make API call to download the file
const endpoint = this.resourceEndpoints.file.download;
const response = await this.makeApiCall(endpoint, requestParams, { returnBinary: true }, itemIndex);
// Add the binary data to the return data
const newItem = {
json: { ...this.items[itemIndex].json },
binary: {
...(this.items[itemIndex].binary || {}),
},
};
if (response.fileName && response.fileContent) {
newItem.binary[binaryPropertyName] = {
data: response.fileContent,
mimeType: response.mimeType || "application/octet-stream",
fileName: response.fileName,
};
}
// Replace the item at the given index with the new item
this.returnData[itemIndex] = newItem;
}
/**
* Handle 'deleteFile' operation
*/
async handleDeleteFile(itemIndex) {
const fileId = this.getNodeParameter("fileId", itemIndex);
const requestParams = {
id: fileId,
};
const endpoint = this.resourceEndpoints.file.delete;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'copyFile' operation
*/
async handleCopyFile(itemIndex) {
const fileId = this.getNodeParameter("fileId", itemIndex);
const targetFolderId = this.getNodeParameter("targetFolderId", itemIndex);
const requestParams = {
id: fileId,
targetFolderId,
};
const endpoint = this.resourceEndpoints.file.copy;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'moveFile' operation
*/
async handleMoveFile(itemIndex) {
const fileId = this.getNodeParameter("fileId", itemIndex);
const targetFolderId = this.getNodeParameter("targetFolderId", itemIndex);
const requestParams = {
id: fileId,
targetFolderId,
};
const endpoint = this.resourceEndpoints.file.move;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'renameFile' operation
*/
async handleRenameFile(itemIndex) {
const fileId = this.getNodeParameter("fileId", itemIndex);
const newFileName = this.getNodeParameter("newFileName", itemIndex);
const requestParams = {
id: fileId,
newName: newFileName,
};
const endpoint = this.resourceEndpoints.file.rename;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'shareItem' operation
*/
async handleShareItem(itemIndex) {
const itemType = this.getNodeParameter("itemType", itemIndex);
const itemId = this.getNodeParameter("itemId", itemIndex);
const shareWithJson = this.getNodeParameter("shareWith", itemIndex);
const rights = this.getNodeParameter("rights", itemIndex);
if (!shareWithJson || shareWithJson.trim() === "") {
throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Share with must not be empty", { itemIndex });
}
const shareWith = this.parseJsonParameter(shareWithJson, "shareWith", itemIndex);
const requestParams = {
type: itemType,
id: itemId,
users: shareWith,
rights,
};
const endpoint = this.resourceEndpoints.sharing.share;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'getSharedItems' operation
*/
async handleGetSharedItems(itemIndex) {
const endpoint = this.resourceEndpoints.sharing.list;
const responseData = await this.makeApiCall(endpoint, {}, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'getSharingRights' operation
*/
async handleGetSharingRights(itemIndex) {
const itemType = this.getNodeParameter("itemType", itemIndex);
const itemId = this.getNodeParameter("itemId", itemIndex);
const requestParams = {
type: itemType,
id: itemId,
};
const endpoint = this.resourceEndpoints.sharing.get;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'updateSharingRights' operation
*/
async handleUpdateSharingRights(itemIndex) {
const itemType = this.getNodeParameter("itemType", itemIndex);
const itemId = this.getNodeParameter("itemId", itemIndex);
const shareWithJson = this.getNodeParameter("shareWith", itemIndex);
const rights = this.getNodeParameter("rights", itemIndex);
if (!shareWithJson || shareWithJson.trim() === "") {
throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Share with must not be empty", { itemIndex });
}
const shareWith = this.parseJsonParameter(shareWithJson, "shareWith", itemIndex);
const requestParams = {
type: itemType,
id: itemId,
users: shareWith,
rights,
};
const endpoint = this.resourceEndpoints.sharing.update;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
}
exports.DiskResourceHandler = DiskResourceHandler;
//# sourceMappingURL=DiskResourceHandler.js.map