154 lines
4.7 KiB
TypeScript
154 lines
4.7 KiB
TypeScript
import {
|
|
IExecuteFunctions,
|
|
INodeExecutionData,
|
|
NodeOperationError,
|
|
NodeOutput,
|
|
LoggerProxy as Logger,
|
|
} from 'n8n-workflow';
|
|
|
|
import { SecloreDRMFileService } from '../Services/SecloreDRMFileService';
|
|
|
|
export async function protectWithHotFolder(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 Protect with HotFolder 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 hotfolderId = this.getNodeParameter('hotfolderId', i) as string;
|
|
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i) as string;
|
|
const correlationId = this.getNodeParameter('correlationId', i) as string;
|
|
const retryCount = this.getNodeParameter('retryCount', i) as number;
|
|
|
|
// Validate required parameters
|
|
if (!hotfolderId) {
|
|
throw new NodeOperationError(this.getNode(), 'HotFolder ID is required', {
|
|
itemIndex: i,
|
|
});
|
|
}
|
|
|
|
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
|
|
});
|
|
|
|
// Upload the file first
|
|
const uploadResult = await fileService.uploadFile(
|
|
new Uint8Array(fileBuffer),
|
|
binaryData.fileName || 'file',
|
|
correlationId || undefined,
|
|
retryCount,
|
|
);
|
|
|
|
Logger.info('File uploaded successfully', { fileStorageId: uploadResult.fileStorageId, fileName: binaryData.fileName });
|
|
Logger.debug('File upload response', { uploadResult });
|
|
|
|
// Protect the uploaded file with HotFolder
|
|
const protectResult = await fileService.protectWithHotFolder(
|
|
{
|
|
hotfolderId,
|
|
fileStorageId: uploadResult.fileStorageId,
|
|
},
|
|
correlationId || undefined,
|
|
retryCount,
|
|
);
|
|
|
|
Logger.info('File protected successfully', {
|
|
originalFileStorageId: uploadResult.fileStorageId,
|
|
protectedFileStorageId: protectResult.fileStorageId,
|
|
secloreFileId: protectResult.secloreFileId,
|
|
hotfolderId,
|
|
fileName: binaryData.fileName
|
|
});
|
|
Logger.debug('File protect response', { protectResult });
|
|
|
|
// Download the protected file
|
|
const protectedFileData = await fileService.downloadFile(
|
|
protectResult.fileStorageId,
|
|
correlationId || undefined,
|
|
retryCount,
|
|
);
|
|
|
|
Logger.info('Protected file downloaded successfully', {
|
|
fileStorageId: protectResult.fileStorageId,
|
|
fileSize: protectedFileData.length,
|
|
fileName: binaryData.fileName
|
|
});
|
|
|
|
// Create output binary data
|
|
const outputBinaryData = await this.helpers.prepareBinaryData(
|
|
Buffer.from(protectedFileData),
|
|
binaryData.fileName || 'protected_file',
|
|
binaryData.mimeType,
|
|
);
|
|
|
|
// Create return item with binary data and metadata
|
|
const returnItem: INodeExecutionData = {
|
|
json: {
|
|
success: true,
|
|
originalFileStorageId: uploadResult.fileStorageId,
|
|
protectedFileStorageId: protectResult.fileStorageId,
|
|
secloreFileId: protectResult.secloreFileId,
|
|
hotfolderId,
|
|
fileName: binaryData.fileName,
|
|
fileSize: protectedFileData.length,
|
|
correlationId: correlationId || null,
|
|
},
|
|
binary: {
|
|
data: outputBinaryData,
|
|
},
|
|
};
|
|
|
|
returnData.push(returnItem);
|
|
} catch (error) {
|
|
// Handle errors gracefully
|
|
Logger.error('Protect with HotFolder operation failed', { error, itemIndex: i });
|
|
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 Protect with HotFolder operation completed', {
|
|
processedItems: returnData.length,
|
|
successfulItems: returnData.filter(item => item.json.success).length
|
|
});
|
|
|
|
return [returnData];
|
|
}
|