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

198 lines
6.1 KiB
TypeScript

import {
IExecuteFunctions,
INodeExecutionData,
NodeOperationError,
NodeOutput,
LoggerProxy as Logger,
} from 'n8n-workflow';
import { SecloreDRMFileService } from '../Services/SecloreDRMFileService';
/**
* 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(
context: IExecuteFunctions,
fileService: SecloreDRMFileService,
fileBuffer: Buffer,
fileName: string,
correlationId?: string,
retryCount: number = 3,
): Promise<{
unprotectedFileData: Uint8Array;
originalFileStorageId: string;
unprotectedFileStorageId: string;
fileName: string;
fileSize: number;
}> {
// Upload the protected file
const uploadResult = await fileService.uploadFile(
new Uint8Array(fileBuffer),
fileName,
correlationId,
retryCount,
);
Logger.info('File uploaded successfully', { fileStorageId: uploadResult.fileStorageId, fileName });
Logger.debug('File upload response', {uploadResult});
// Unprotect the uploaded file
Logger.debug('Unprotecting file', { fileStorageId: uploadResult.fileStorageId, fileName });
const unprotectResult = await fileService.unprotect(
{
fileStorageId: uploadResult.fileStorageId,
},
correlationId,
retryCount,
);
Logger.info('File unprotected successfully', { originalFileStorageId: uploadResult.fileStorageId, unprotectedFileStorageId: unprotectResult.fileStorageId, fileName });
Logger.info('Downloading unprotected file', { fileStorageId: unprotectResult.fileStorageId, fileName });
// Download the unprotected file
const unprotectedFileData = await fileService.downloadFile(
unprotectResult.fileStorageId,
correlationId,
retryCount,
);
Logger.info('Unprotected file downloaded successfully', { fileStorageId: unprotectResult.fileStorageId, fileSize: unprotectedFileData.length, fileName });
return {
unprotectedFileData,
originalFileStorageId: uploadResult.fileStorageId,
unprotectedFileStorageId: unprotectResult.fileStorageId,
fileName,
fileSize: unprotectedFileData.length,
};
}
export async function unprotect(this: IExecuteFunctions): Promise<NodeOutput> {
const items = this.getInputData();
const returnData: INodeExecutionData[] = [];
// Initialize logger with the current execution context
Logger.init(this.logger);
Logger.info('Seclore Unprotect operation started', { itemCount: items.length });
// Get credentials
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
const fileService = new SecloreDRMFileService(this, baseUrl, tenantId, tenantSecret);
for (let i = 0; i < items.length; i++) {
Logger.debug('Processing item', { itemIndex: i, itemData: items[i] });
try {
// Get parameters for this item
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i) as string;
const correlationId = this.getNodeParameter('correlationId', i) as string;
const retryCount = this.getNodeParameter('retryCount', i) as number;
Logger.debug('Asserting binary data', { binaryPropertyName, itemIndex: i });
// Get input binary data
const binaryData = this.helpers.assertBinaryData(i, binaryPropertyName);
Logger.debug('Getting binary data buffer', { binaryPropertyName, itemIndex: i });
const fileBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
Logger.debug('Binary data retrieved', {
fileName: binaryData.fileName,
fileSize: fileBuffer.length,
mimeType: binaryData.mimeType,
itemIndex: i
});
// Use the combined upload, unprotect, and download function
try {
Logger.debug('Starting unprotect file operation', {
fileName: binaryData.fileName,
correlationId,
retryCount,
itemIndex: i
});
const result = await unprotectFile(
this,
fileService,
fileBuffer,
binaryData.fileName || 'protected_file',
correlationId || undefined,
retryCount,
);
Logger.info('Unprotect file operation completed successfully', {
fileName: result.fileName,
originalFileStorageId: result.originalFileStorageId,
unprotectedFileStorageId: result.unprotectedFileStorageId,
fileSize: result.fileSize,
itemIndex: i
});
// Create output binary data
const outputBinaryData = await this.helpers.prepareBinaryData(
Buffer.from(result.unprotectedFileData),
binaryData.fileName || 'unprotected_file',
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,
correlationId: correlationId || null,
},
binary: {
data: outputBinaryData,
},
};
returnData.push(returnItem);
} catch (unprotectError) {
Logger.error('Unprotect file operation failed with error', { unprotectError });
// Re-throw the error to be handled by the outer catch block
throw unprotectError;
}
} catch (error) {
// Handle errors gracefully
Logger.error('Unprotect file operation failed with error', { error });
if (this.continueOnFail()) {
const returnItem: INodeExecutionData = {
json: {
success: false,
error: error.message,
itemIndex: i,
},
};
returnData.push(returnItem);
} else {
throw new NodeOperationError(this.getNode(), error.message, {
itemIndex: i,
});
}
}
}
Logger.info('Seclore Unprotect operation completed', {
processedItems: returnData.length,
successfulItems: returnData.filter(item => item.json.success).length
});
return [returnData];
}