"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.unprotect = unprotect; 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 = "unprotect::deleteFileWithErrorHandling:: "; 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 unprotectFile(fileService, fileBuffer, fileName, correlationId, retryCount = 3) { const who = "unprotect::unprotectFile:: "; let originalFileStorageId = ''; try { n8n_workflow_1.LoggerProxy.debug(who + 'Starting unprotect file operation', { fileName, fileSize: fileBuffer.length, correlationId, retryCount }); n8n_workflow_1.LoggerProxy.debug(who + 'Uploading protected 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 }); originalFileStorageId = uploadResult.fileStorageId; if (!uploadResult.protected) { n8n_workflow_1.LoggerProxy.debug(who + 'File is already unprotected', { fileStorageId: uploadResult.fileStorageId, fileName, correlationId }); return { unprotectedFileData: fileBuffer, originalFileStorageId: uploadResult.fileStorageId, unprotectedFileStorageId: uploadResult.fileStorageId, fileName, fileSize: fileBuffer.length, }; } n8n_workflow_1.LoggerProxy.debug(who + 'Unprotecting file', { fileStorageId: uploadResult.fileStorageId, fileName, correlationId }); const unprotectResult = await fileService.unprotect({ fileStorageId: uploadResult.fileStorageId, }, correlationId, retryCount); n8n_workflow_1.LoggerProxy.debug(who + 'File unprotected successfully', { originalFileStorageId: uploadResult.fileStorageId, unprotectedFileStorageId: unprotectResult.fileStorageId, fileName, correlationId }); n8n_workflow_1.LoggerProxy.debug(who + 'Downloading unprotected file', { fileStorageId: unprotectResult.fileStorageId, fileName, correlationId }); const unprotectedFileData = await fileService.downloadFile(unprotectResult.fileStorageId, correlationId, retryCount); const actualFileName = (0, Utils_1.getFileNameFromHeaders)(unprotectedFileData.headers) || fileName; n8n_workflow_1.LoggerProxy.debug(who + 'Unprotected file downloaded successfully', { fileStorageId: unprotectResult.fileStorageId, fileSize: unprotectedFileData.data.length, originalFileName: fileName, actualFileName: actualFileName, correlationId }); const result = { unprotectedFileData: unprotectedFileData.data, originalFileStorageId: uploadResult.fileStorageId, unprotectedFileStorageId: unprotectResult.fileStorageId, fileName: actualFileName, fileSize: unprotectedFileData.data.length, }; n8n_workflow_1.LoggerProxy.debug(who + 'Unprotect file operation completed successfully', { fileName: result.fileName, originalFileStorageId: result.originalFileStorageId, unprotectedFileStorageId: result.unprotectedFileStorageId, fileSize: result.fileSize, correlationId }); return result; } catch (error) { n8n_workflow_1.LoggerProxy.error(who + 'Unprotect file operation failed', { error, fileName, correlationId }); throw error; } finally { if (originalFileStorageId !== '') { await deleteFile(fileService, originalFileStorageId, correlationId, retryCount); } } } async function unprotect() { const who = "unprotect::unprotect:: "; const items = this.getInputData(); const returnData = []; n8n_workflow_1.LoggerProxy.init(this.logger); n8n_workflow_1.LoggerProxy.debug(who + 'Seclore Unprotect 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 binaryPropertyName = this.getNodeParameter('binaryPropertyName', i); const correlationId = node_crypto_1.default.randomUUID(); const retryCount = this.getNodeParameter('retryCount', 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 unprotect file operation', { fileName: binaryData.fileName, correlationId, retryCount, itemIndex: i }); const result = await unprotectFile(fileService, fileBuffer, binaryData.fileName || 'protected_file', correlationId, retryCount); n8n_workflow_1.LoggerProxy.debug(who + 'Unprotect file operation completed successfully', { fileName: result.fileName, originalFileStorageId: result.originalFileStorageId, unprotectedFileStorageId: result.unprotectedFileStorageId, fileSize: result.fileSize, itemIndex: i, correlationId }); n8n_workflow_1.LoggerProxy.debug(who + 'Preparing binary data for output', { fileName: binaryData.fileName, mimeType: binaryData.mimeType, fileSize: result.fileSize, itemIndex: i }); const outputBinaryData = await this.helpers.prepareBinaryData(Buffer.from(result.unprotectedFileData), result.fileName, binaryData.mimeType); const returnItem = { json: { success: true, originalFileStorageId: result.originalFileStorageId, unprotectedFileStorageId: result.unprotectedFileStorageId, 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 (unprotectError) { n8n_workflow_1.LoggerProxy.error(who + 'Unprotect file operation failed', { unprotectError, itemIndex: i }); throw unprotectError; } } 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 Unprotect operation completed', { processedItems: returnData.length, successfulItems: returnData.filter(item => item.json.success).length }); return [returnData]; } //# sourceMappingURL=unprotect.js.map