n8n-nodes-seclore/nodes/SecloreProtect/operations/unprotect.ts

299 lines
9.7 KiB
TypeScript
Raw Normal View History

2025-10-27 12:17:21 +00:00
import {
IExecuteFunctions,
INodeExecutionData,
NodeOperationError,
NodeOutput,
LoggerProxy as Logger,
} from 'n8n-workflow';
2025-10-27 15:16:44 +00:00
import crypto from 'node:crypto';
2025-10-27 12:17:21 +00:00
import { SecloreDRMFileService } from '../Services/SecloreDRMFileService';
2025-10-29 05:05:14 +00:00
import { getFileNameFromHeaders } from '../Services/Utils';
2025-10-27 12:17:21 +00:00
2025-10-27 12:33:58 +00:00
/**
* Deletes a file from storage with error handling (does not throw errors)
* @param fileService - The SecloreDRMFileService instance
* @param fileStorageId - The file storage ID to delete
* @param correlationId - Optional correlation ID for tracking
* @param retryCount - Number of retries for operations
*/
async function deleteFile(
fileService: SecloreDRMFileService,
fileStorageId: string,
correlationId?: string,
retryCount: number = 3,
): Promise<void> {
const who = "unprotect::deleteFileWithErrorHandling:: ";
try {
Logger.debug(who + 'Attempting to delete file', { fileStorageId, correlationId, retryCount });
await fileService.deleteFile(
fileStorageId,
correlationId,
retryCount,
);
Logger.debug(who + 'File deleted successfully', { fileStorageId, correlationId });
} catch (error) {
// Log error but don't throw - this is for cleanup operations
Logger.error(who + 'File deletion failed, continuing operation', {
error,
fileStorageId,
correlationId,
message: 'This is a cleanup operation, continuing despite deletion failure'
});
}
}
2025-10-27 12:17:21 +00:00
/**
* Uploads a file, unprotects it, and downloads the unprotected version
* @param fileService - The SecloreDRMFileService instance
* @param fileBuffer - The file buffer to upload
* @param fileName - The name of the file
* @param correlationId - Optional correlation ID for tracking
* @param retryCount - Number of retries for operations
* @returns Promise containing the unprotected file data and metadata
*/
async function unprotectFile(
fileService: SecloreDRMFileService,
fileBuffer: Buffer,
fileName: string,
correlationId?: string,
retryCount: number = 3,
): Promise<{
unprotectedFileData: Uint8Array;
originalFileStorageId: string;
unprotectedFileStorageId: string;
fileName: string;
fileSize: number;
}> {
2025-10-27 12:33:58 +00:00
const who = "unprotect::unprotectFile:: ";
2025-11-04 05:42:48 +00:00
let originalFileStorageId: string = '';
2025-10-27 12:33:58 +00:00
try {
Logger.debug(who + 'Starting unprotect file operation', { fileName, fileSize: fileBuffer.length, correlationId, retryCount });
// Upload the protected file
Logger.debug(who + 'Uploading protected file', { fileName, fileSize: fileBuffer.length, correlationId });
const uploadResult = await fileService.uploadFile(
new Uint8Array(fileBuffer),
fileName,
correlationId,
retryCount,
);
Logger.debug(who + 'File uploaded successfully', { fileStorageId: uploadResult.fileStorageId, fileName, correlationId });
originalFileStorageId = uploadResult.fileStorageId;
// check if the file is already unprotected
if (!uploadResult.protected) {
Logger.debug(who + 'File is already unprotected', { fileStorageId: uploadResult.fileStorageId, fileName, correlationId });
return {
unprotectedFileData: fileBuffer,
originalFileStorageId: uploadResult.fileStorageId,
unprotectedFileStorageId: uploadResult.fileStorageId,
fileName,
fileSize: fileBuffer.length,
};
}
// Unprotect the uploaded file
Logger.debug(who + 'Unprotecting file', { fileStorageId: uploadResult.fileStorageId, fileName, correlationId });
const unprotectResult = await fileService.unprotect(
{
fileStorageId: uploadResult.fileStorageId,
},
correlationId,
retryCount,
);
Logger.debug(who + 'File unprotected successfully', {
originalFileStorageId: uploadResult.fileStorageId,
unprotectedFileStorageId: unprotectResult.fileStorageId,
fileName,
correlationId
});
// Download the unprotected file
Logger.debug(who + 'Downloading unprotected file', { fileStorageId: unprotectResult.fileStorageId, fileName, correlationId });
const unprotectedFileData = await fileService.downloadFile(
unprotectResult.fileStorageId,
correlationId,
retryCount,
);
2025-10-29 05:05:14 +00:00
// Try to get the actual filename from response headers, fallback to original filename
const actualFileName = getFileNameFromHeaders(unprotectedFileData.headers) || fileName;
2025-10-27 12:33:58 +00:00
Logger.debug(who + 'Unprotected file downloaded successfully', {
fileStorageId: unprotectResult.fileStorageId,
2025-10-29 05:05:14 +00:00
fileSize: unprotectedFileData.data.length,
originalFileName: fileName,
actualFileName: actualFileName,
2025-10-27 12:33:58 +00:00
correlationId
});
const result = {
2025-10-29 05:05:14 +00:00
unprotectedFileData: unprotectedFileData.data,
2025-10-27 12:33:58 +00:00
originalFileStorageId: uploadResult.fileStorageId,
unprotectedFileStorageId: unprotectResult.fileStorageId,
2025-10-29 05:05:14 +00:00
fileName: actualFileName,
fileSize: unprotectedFileData.data.length,
2025-10-27 12:33:58 +00:00
};
Logger.debug(who + 'Unprotect file operation completed successfully', {
fileName: result.fileName,
originalFileStorageId: result.originalFileStorageId,
unprotectedFileStorageId: result.unprotectedFileStorageId,
fileSize: result.fileSize,
correlationId
});
return result;
} catch (error) {
Logger.error(who + 'Unprotect file operation failed', { error, fileName, correlationId });
throw error;
} finally {
if (originalFileStorageId !== '') {
await deleteFile(fileService, originalFileStorageId, correlationId, retryCount);
}
}
2025-10-27 12:17:21 +00:00
}
export async function unprotect(this: IExecuteFunctions): Promise<NodeOutput> {
2025-10-27 12:33:58 +00:00
const who = "unprotect::unprotect:: ";
2025-10-27 12:17:21 +00:00
const items = this.getInputData();
const returnData: INodeExecutionData[] = [];
// Initialize logger with the current execution context
Logger.init(this.logger);
2025-10-27 12:33:58 +00:00
Logger.debug(who + 'Seclore Unprotect operation started', { itemCount: items.length });
2025-10-27 12:17:21 +00:00
// Get credentials
2025-10-27 12:33:58 +00:00
Logger.debug(who + 'Getting credentials', {});
2025-10-27 12:17:21 +00:00
const credentials = await this.getCredentials('secloreProtectApi');
const baseUrl = credentials.baseUrl as string;
const tenantId = credentials.tenantId as string;
const tenantSecret = credentials.tenantSecret as string;
// Initialize the file service
2025-10-27 12:33:58 +00:00
Logger.debug(who + 'Initializing file service', { baseUrl, tenantId });
2025-10-27 12:17:21 +00:00
const fileService = new SecloreDRMFileService(this, baseUrl, tenantId, tenantSecret);
for (let i = 0; i < items.length; i++) {
2025-10-27 12:33:58 +00:00
Logger.debug(who + 'Processing item', { itemIndex: i });
2025-10-27 12:17:21 +00:00
try {
// Get parameters for this item
2025-10-27 12:33:58 +00:00
Logger.debug(who + 'Getting node parameters', { itemIndex: i });
2025-10-27 12:17:21 +00:00
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i) as string;
2025-11-11 11:02:05 +00:00
const correlationId = crypto.randomUUID();
2025-10-27 12:17:21 +00:00
const retryCount = this.getNodeParameter('retryCount', i) as number;
2025-10-27 12:33:58 +00:00
Logger.debug(who + 'Asserting binary data', { binaryPropertyName, itemIndex: i });
2025-10-27 12:17:21 +00:00
// Get input binary data
const binaryData = this.helpers.assertBinaryData(i, binaryPropertyName);
2025-10-27 12:33:58 +00:00
Logger.debug(who + 'Getting binary data buffer', { binaryPropertyName, itemIndex: i });
2025-10-27 12:17:21 +00:00
const fileBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
2025-10-27 12:33:58 +00:00
Logger.debug(who + 'Binary data retrieved', {
2025-10-27 12:17:21 +00:00
fileName: binaryData.fileName,
fileSize: fileBuffer.length,
mimeType: binaryData.mimeType,
2025-10-27 12:33:58 +00:00
itemIndex: i,
correlationId,
retryCount
2025-10-27 12:17:21 +00:00
});
// Use the combined upload, unprotect, and download function
try {
2025-10-27 12:33:58 +00:00
Logger.debug(who + 'Starting unprotect file operation', {
2025-10-27 12:17:21 +00:00
fileName: binaryData.fileName,
correlationId,
retryCount,
itemIndex: i
});
const result = await unprotectFile(
fileService,
fileBuffer,
binaryData.fileName || 'protected_file',
2025-10-27 15:16:44 +00:00
correlationId,
2025-10-27 12:17:21 +00:00
retryCount,
);
2025-10-27 12:33:58 +00:00
Logger.debug(who + 'Unprotect file operation completed successfully', {
2025-10-27 12:17:21 +00:00
fileName: result.fileName,
originalFileStorageId: result.originalFileStorageId,
unprotectedFileStorageId: result.unprotectedFileStorageId,
fileSize: result.fileSize,
2025-10-27 12:33:58 +00:00
itemIndex: i,
correlationId
2025-10-27 12:17:21 +00:00
});
// Create output binary data
2025-10-27 12:33:58 +00:00
Logger.debug(who + 'Preparing binary data for output', {
fileName: binaryData.fileName,
mimeType: binaryData.mimeType,
fileSize: result.fileSize,
itemIndex: i
});
2025-10-27 12:17:21 +00:00
const outputBinaryData = await this.helpers.prepareBinaryData(
Buffer.from(result.unprotectedFileData),
2025-10-29 05:05:14 +00:00
result.fileName,
2025-10-27 12:17:21 +00:00
binaryData.mimeType,
);
// Create return item with binary data and metadata
const returnItem: INodeExecutionData = {
json: {
success: true,
originalFileStorageId: result.originalFileStorageId,
unprotectedFileStorageId: result.unprotectedFileStorageId,
fileName: result.fileName,
fileSize: result.fileSize,
2025-10-27 15:16:44 +00:00
correlationId: correlationId,
2025-10-27 12:17:21 +00:00
},
binary: {
data: outputBinaryData,
},
};
2025-10-27 12:33:58 +00:00
Logger.debug(who + 'Adding result to return data', { itemIndex: i, success: true });
2025-10-27 12:17:21 +00:00
returnData.push(returnItem);
} catch (unprotectError) {
2025-10-27 12:33:58 +00:00
Logger.error(who + 'Unprotect file operation failed', { unprotectError, itemIndex: i });
2025-10-27 12:17:21 +00:00
// Re-throw the error to be handled by the outer catch block
throw unprotectError;
}
} catch (error) {
// Handle errors gracefully
2025-10-27 12:33:58 +00:00
Logger.error(who + 'Item processing failed', { error, itemIndex: i });
2025-10-27 12:17:21 +00:00
if (this.continueOnFail()) {
2025-10-27 12:33:58 +00:00
Logger.debug(who + 'Continuing on fail, adding error item', { itemIndex: i, errorMessage: error.message });
2025-10-27 12:17:21 +00:00
const returnItem: INodeExecutionData = {
json: {
success: false,
error: error.message,
itemIndex: i,
},
};
returnData.push(returnItem);
} else {
2025-10-27 12:33:58 +00:00
Logger.error(who + 'Throwing NodeOperationError', { error: error.message, itemIndex: i });
2025-10-27 12:17:21 +00:00
throw new NodeOperationError(this.getNode(), error.message, {
itemIndex: i,
});
}
}
}
2025-10-27 12:33:58 +00:00
Logger.debug(who + 'Seclore Unprotect operation completed', {
2025-10-27 12:17:21 +00:00
processedItems: returnData.length,
successfulItems: returnData.filter(item => item.json.success).length
});
return [returnData];
}