205 lines
11 KiB
JavaScript
205 lines
11 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.protectWithHotFolder = protectWithHotFolder;
|
|
const n8n_workflow_1 = require("n8n-workflow");
|
|
const node_crypto_1 = __importDefault(require("node:crypto"));
|
|
const SecloreDRMFileService_1 = require("../Services/SecloreDRMFileService");
|
|
const Utils_1 = require("../Services/Utils");
|
|
async function deleteFile(fileService, fileStorageId, correlationId, retryCount = 3) {
|
|
const who = "protectWithHotFolder::deleteFile:: ";
|
|
try {
|
|
n8n_workflow_1.LoggerProxy.debug(who + 'Attempting to delete file', { fileStorageId, correlationId, retryCount });
|
|
await fileService.deleteFile(fileStorageId, correlationId, retryCount);
|
|
n8n_workflow_1.LoggerProxy.debug(who + 'File deleted successfully', { fileStorageId, correlationId });
|
|
}
|
|
catch (error) {
|
|
n8n_workflow_1.LoggerProxy.error(who + 'File deletion failed, continuing operation', {
|
|
error,
|
|
fileStorageId,
|
|
correlationId,
|
|
message: 'This is a cleanup operation, continuing despite deletion failure'
|
|
});
|
|
}
|
|
}
|
|
async function protectFileWithHotFolder(fileService, fileBuffer, fileName, hotfolderId, correlationId, retryCount = 3) {
|
|
const who = "protectWithHotFolder::protectFileWithHotFolder:: ";
|
|
let originalFileStorageId = '';
|
|
try {
|
|
n8n_workflow_1.LoggerProxy.debug(who + 'Starting protect file with hot folder operation', { fileName, fileSize: fileBuffer.length, hotfolderId, correlationId, retryCount });
|
|
n8n_workflow_1.LoggerProxy.debug(who + 'Uploading file', { fileName, fileSize: fileBuffer.length, correlationId });
|
|
const uploadResult = await fileService.uploadFile(new Uint8Array(fileBuffer), fileName, correlationId, retryCount);
|
|
n8n_workflow_1.LoggerProxy.debug(who + 'File uploaded successfully', { fileStorageId: uploadResult.fileStorageId, fileName, correlationId });
|
|
if (uploadResult.protected) {
|
|
throw new Error('File is already protected');
|
|
}
|
|
originalFileStorageId = uploadResult.fileStorageId;
|
|
n8n_workflow_1.LoggerProxy.debug(who + 'Protecting file with hot folder', { fileStorageId: uploadResult.fileStorageId, hotfolderId, fileName, correlationId });
|
|
const protectResult = await fileService.protectWithHotFolder({
|
|
hotfolderId,
|
|
fileStorageId: uploadResult.fileStorageId,
|
|
}, correlationId, retryCount);
|
|
n8n_workflow_1.LoggerProxy.debug(who + 'File protected successfully', {
|
|
originalFileStorageId: uploadResult.fileStorageId,
|
|
protectedFileStorageId: protectResult.fileStorageId,
|
|
secloreFileId: protectResult.secloreFileId,
|
|
hotfolderId,
|
|
fileName,
|
|
correlationId
|
|
});
|
|
n8n_workflow_1.LoggerProxy.debug(who + 'Downloading protected file', { fileStorageId: protectResult.fileStorageId, fileName, correlationId });
|
|
const protectedFileData = await fileService.downloadFile(protectResult.fileStorageId, correlationId, retryCount);
|
|
const actualFileName = (0, Utils_1.getFileNameFromHeaders)(protectedFileData.headers) || fileName;
|
|
n8n_workflow_1.LoggerProxy.debug(who + 'Protected file downloaded successfully', {
|
|
fileStorageId: protectResult.fileStorageId,
|
|
fileSize: protectedFileData.data.length,
|
|
originalFileName: fileName,
|
|
actualFileName,
|
|
correlationId
|
|
});
|
|
const result = {
|
|
protectedFileData: protectedFileData.data,
|
|
originalFileStorageId: uploadResult.fileStorageId,
|
|
protectedFileStorageId: protectResult.fileStorageId,
|
|
secloreFileId: protectResult.secloreFileId,
|
|
fileName: actualFileName,
|
|
fileSize: protectedFileData.data.length,
|
|
};
|
|
n8n_workflow_1.LoggerProxy.debug(who + 'Protect file with hot folder operation completed successfully', {
|
|
fileName: result.fileName,
|
|
originalFileStorageId: result.originalFileStorageId,
|
|
protectedFileStorageId: result.protectedFileStorageId,
|
|
secloreFileId: result.secloreFileId,
|
|
fileSize: result.fileSize,
|
|
hotfolderId,
|
|
correlationId
|
|
});
|
|
return result;
|
|
}
|
|
catch (error) {
|
|
n8n_workflow_1.LoggerProxy.error(who + 'Protect file with hot folder operation failed', { error, fileName, hotfolderId, correlationId });
|
|
throw error;
|
|
}
|
|
finally {
|
|
if (originalFileStorageId !== '') {
|
|
await deleteFile(fileService, originalFileStorageId, correlationId, retryCount);
|
|
}
|
|
}
|
|
}
|
|
async function protectWithHotFolder() {
|
|
const who = "protectWithHotFolder::protectWithHotFolder:: ";
|
|
const items = this.getInputData();
|
|
const returnData = [];
|
|
n8n_workflow_1.LoggerProxy.init(this.logger);
|
|
n8n_workflow_1.LoggerProxy.debug(who + 'Seclore Protect with HotFolder operation started', { itemCount: items.length });
|
|
n8n_workflow_1.LoggerProxy.debug(who + 'Getting credentials', {});
|
|
const credentials = await this.getCredentials('secloreProtectApi');
|
|
const baseUrl = credentials.baseUrl;
|
|
const tenantId = credentials.tenantId;
|
|
const tenantSecret = credentials.tenantSecret;
|
|
n8n_workflow_1.LoggerProxy.debug(who + 'Initializing file service', { baseUrl, tenantId });
|
|
const fileService = new SecloreDRMFileService_1.SecloreDRMFileService(this, baseUrl, tenantId, tenantSecret);
|
|
for (let i = 0; i < items.length; i++) {
|
|
n8n_workflow_1.LoggerProxy.debug(who + 'Processing item', { itemIndex: i });
|
|
try {
|
|
n8n_workflow_1.LoggerProxy.debug(who + 'Getting node parameters', { itemIndex: i });
|
|
const hotfolderId = this.getNodeParameter('hotfolderId', i);
|
|
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i);
|
|
const correlationId = node_crypto_1.default.randomUUID();
|
|
const retryCount = this.getNodeParameter('retryCount', i);
|
|
if (!hotfolderId) {
|
|
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'HotFolder ID is required', {
|
|
itemIndex: i,
|
|
});
|
|
}
|
|
n8n_workflow_1.LoggerProxy.debug(who + 'Asserting binary data', { binaryPropertyName, itemIndex: i });
|
|
const binaryData = this.helpers.assertBinaryData(i, binaryPropertyName);
|
|
n8n_workflow_1.LoggerProxy.debug(who + 'Getting binary data buffer', { binaryPropertyName, itemIndex: i });
|
|
const fileBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
|
|
n8n_workflow_1.LoggerProxy.debug(who + 'Binary data retrieved', {
|
|
fileName: binaryData.fileName,
|
|
fileSize: fileBuffer.length,
|
|
mimeType: binaryData.mimeType,
|
|
itemIndex: i,
|
|
correlationId,
|
|
retryCount
|
|
});
|
|
try {
|
|
n8n_workflow_1.LoggerProxy.debug(who + 'Starting protect file with hot folder operation', {
|
|
fileName: binaryData.fileName,
|
|
hotfolderId,
|
|
correlationId,
|
|
retryCount,
|
|
itemIndex: i
|
|
});
|
|
const result = await protectFileWithHotFolder(fileService, fileBuffer, binaryData.fileName || 'file', hotfolderId, correlationId, retryCount);
|
|
n8n_workflow_1.LoggerProxy.debug(who + 'Protect file with hot folder operation completed successfully', {
|
|
fileName: result.fileName,
|
|
originalFileStorageId: result.originalFileStorageId,
|
|
protectedFileStorageId: result.protectedFileStorageId,
|
|
secloreFileId: result.secloreFileId,
|
|
fileSize: result.fileSize,
|
|
hotfolderId,
|
|
itemIndex: i,
|
|
correlationId
|
|
});
|
|
n8n_workflow_1.LoggerProxy.debug(who + 'Preparing binary data for output', {
|
|
fileName: result.fileName,
|
|
mimeType: binaryData.mimeType,
|
|
fileSize: result.fileSize,
|
|
itemIndex: i
|
|
});
|
|
const outputBinaryData = await this.helpers.prepareBinaryData(Buffer.from(result.protectedFileData), result.fileName, binaryData.mimeType);
|
|
const returnItem = {
|
|
json: {
|
|
success: true,
|
|
originalFileStorageId: result.originalFileStorageId,
|
|
protectedFileStorageId: result.protectedFileStorageId,
|
|
secloreFileId: result.secloreFileId,
|
|
hotfolderId,
|
|
fileName: result.fileName,
|
|
fileSize: result.fileSize,
|
|
correlationId: correlationId,
|
|
},
|
|
binary: {
|
|
data: outputBinaryData,
|
|
},
|
|
};
|
|
n8n_workflow_1.LoggerProxy.debug(who + 'Adding result to return data', { itemIndex: i, success: true });
|
|
returnData.push(returnItem);
|
|
}
|
|
catch (protectError) {
|
|
n8n_workflow_1.LoggerProxy.error(who + 'Protect file with hot folder operation failed', { protectError, itemIndex: i });
|
|
throw protectError;
|
|
}
|
|
}
|
|
catch (error) {
|
|
n8n_workflow_1.LoggerProxy.error(who + 'Item processing failed', { error, itemIndex: i });
|
|
if (this.continueOnFail()) {
|
|
n8n_workflow_1.LoggerProxy.debug(who + 'Continuing on fail, adding error item', { itemIndex: i, errorMessage: error.message });
|
|
const returnItem = {
|
|
json: {
|
|
success: false,
|
|
error: error.message,
|
|
itemIndex: i,
|
|
},
|
|
};
|
|
returnData.push(returnItem);
|
|
}
|
|
else {
|
|
n8n_workflow_1.LoggerProxy.error(who + 'Throwing NodeOperationError', { error: error.message, itemIndex: i });
|
|
throw new n8n_workflow_1.NodeOperationError(this.getNode(), error.message, {
|
|
itemIndex: i,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
n8n_workflow_1.LoggerProxy.debug(who + 'Seclore Protect with HotFolder operation completed', {
|
|
processedItems: returnData.length,
|
|
successfulItems: returnData.filter(item => item.json.success).length
|
|
});
|
|
return [returnData];
|
|
}
|
|
//# sourceMappingURL=protectWithHotFolder.js.map
|