some cleanup

This commit is contained in:
atharva.dev 2025-10-27 20:46:44 +05:30
parent 14e7cbf9fc
commit 60b90f2f4d
4 changed files with 232 additions and 114 deletions

View File

@ -97,20 +97,6 @@ export class SecloreProtect implements INodeType {
}, },
}, },
}, },
{
displayName: 'Correlation ID',
name: 'correlationId',
type: 'string',
default: '',
placeholder: 'e.g., req-12345',
description: 'Optional correlation ID for request tracking and logging',
displayOptions: {
show: {
resource: ['protection'],
operation: ['protectWithHotFolder'],
},
},
},
{ {
displayName: 'Retry Count', displayName: 'Retry Count',
name: 'retryCount', name: 'retryCount',
@ -139,20 +125,6 @@ export class SecloreProtect implements INodeType {
}, },
}, },
}, },
{
displayName: 'Correlation ID',
name: 'correlationId',
type: 'string',
default: '',
placeholder: 'e.g., req-12345',
description: 'Optional correlation ID for request tracking and logging',
displayOptions: {
show: {
resource: ['protection'],
operation: ['unprotect'],
},
},
},
{ {
displayName: 'Retry Count', displayName: 'Retry Count',
name: 'retryCount', name: 'retryCount',

View File

@ -13,14 +13,10 @@ import {
import { IUnprotectRequest, IUnprotectResponse } from './Interfaces/UnprotectInterfaces'; import { IUnprotectRequest, IUnprotectResponse } from './Interfaces/UnprotectInterfaces';
export class SecloreDRMApiService { export class SecloreDRMApiService {
private baseUrl: string;
constructor( constructor(
private context: IExecuteFunctions, private context: IExecuteFunctions,
baseUrl: string, private baseUrl: string,
) { ) { }
this.baseUrl = baseUrl;
}
/** /**
* Common error handler for HTTP responses * Common error handler for HTTP responses

View File

@ -5,34 +5,182 @@ import {
NodeOutput, NodeOutput,
LoggerProxy as Logger, LoggerProxy as Logger,
} from 'n8n-workflow'; } from 'n8n-workflow';
import crypto from 'node:crypto';
import { SecloreDRMFileService } from '../Services/SecloreDRMFileService'; import { SecloreDRMFileService } from '../Services/SecloreDRMFileService';
/**
* 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 = "protectWithHotFolder::deleteFile:: ";
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'
});
}
}
/**
* Uploads a file, protects it with hot folder, and downloads the protected version
* @param fileService - The SecloreDRMFileService instance
* @param fileBuffer - The file buffer to upload
* @param fileName - The name of the file
* @param hotfolderId - The hot folder ID to use for protection
* @param correlationId - Optional correlation ID for tracking
* @param retryCount - Number of retries for operations
* @returns Promise containing the protected file data and metadata
*/
async function protectFileWithHotFolder(
fileService: SecloreDRMFileService,
fileBuffer: Buffer,
fileName: string,
hotfolderId: string,
correlationId?: string,
retryCount: number = 3,
): Promise<{
protectedFileData: Uint8Array;
originalFileStorageId: string;
protectedFileStorageId: string;
secloreFileId: string;
fileName: string;
fileSize: number;
}> {
const who = "protectWithHotFolder::protectFileWithHotFolder:: ";
var originalFileStorageId: string = '';
const protectedFileName = `${fileName}.html`;
try {
Logger.debug(who + 'Starting protect file with hot folder operation', { fileName, fileSize: fileBuffer.length, hotfolderId, correlationId, retryCount });
// Upload the file first
Logger.debug(who + 'Uploading 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;
// Protect the uploaded file with HotFolder
Logger.debug(who + 'Protecting file with hot folder', { fileStorageId: uploadResult.fileStorageId, hotfolderId, fileName, correlationId });
const protectResult = await fileService.protectWithHotFolder(
{
hotfolderId,
fileStorageId: uploadResult.fileStorageId,
},
correlationId,
retryCount,
);
Logger.debug(who + 'File protected successfully', {
originalFileStorageId: uploadResult.fileStorageId,
protectedFileStorageId: protectResult.fileStorageId,
secloreFileId: protectResult.secloreFileId,
hotfolderId,
fileName,
correlationId
});
// Download the protected file
Logger.debug(who + 'Downloading protected file', { fileStorageId: protectResult.fileStorageId, fileName, correlationId });
const protectedFileData = await fileService.downloadFile(
protectResult.fileStorageId,
correlationId,
retryCount,
);
Logger.debug(who + 'Protected file downloaded successfully', {
fileStorageId: protectResult.fileStorageId,
fileSize: protectedFileData.length,
fileName: protectedFileName,
correlationId
});
const result = {
protectedFileData,
originalFileStorageId: uploadResult.fileStorageId,
protectedFileStorageId: protectResult.fileStorageId,
secloreFileId: protectResult.secloreFileId,
fileName: protectedFileName,
fileSize: protectedFileData.length,
};
Logger.debug(who + 'Protect file with hot folder operation completed successfully', {
fileName: protectedFileName,
originalFileStorageId: result.originalFileStorageId,
protectedFileStorageId: result.protectedFileStorageId,
secloreFileId: result.secloreFileId,
fileSize: result.fileSize,
hotfolderId,
correlationId
});
return result;
} catch (error) {
Logger.error(who + 'Protect file with hot folder operation failed', { error, fileName, hotfolderId, correlationId });
throw error;
} finally {
if (originalFileStorageId !== '') {
await deleteFile(fileService, originalFileStorageId, correlationId, retryCount);
}
}
}
export async function protectWithHotFolder(this: IExecuteFunctions): Promise<NodeOutput> { export async function protectWithHotFolder(this: IExecuteFunctions): Promise<NodeOutput> {
const who = "protectWithHotFolder::protectWithHotFolder:: ";
const items = this.getInputData(); const items = this.getInputData();
const returnData: INodeExecutionData[] = []; const returnData: INodeExecutionData[] = [];
// Initialize logger with the current execution context // Initialize logger with the current execution context
Logger.init(this.logger); Logger.init(this.logger);
Logger.info('Seclore Protect with HotFolder operation started', { itemCount: items.length }); Logger.debug(who + 'Seclore Protect with HotFolder operation started', { itemCount: items.length });
// Get credentials // Get credentials
Logger.debug(who + 'Getting credentials', {});
const credentials = await this.getCredentials('secloreProtectApi'); const credentials = await this.getCredentials('secloreProtectApi');
const baseUrl = credentials.baseUrl as string; const baseUrl = credentials.baseUrl as string;
const tenantId = credentials.tenantId as string; const tenantId = credentials.tenantId as string;
const tenantSecret = credentials.tenantSecret as string; const tenantSecret = credentials.tenantSecret as string;
// Initialize the file service // Initialize the file service
Logger.debug(who + 'Initializing file service', { baseUrl, tenantId });
const fileService = new SecloreDRMFileService(this, baseUrl, tenantId, tenantSecret); const fileService = new SecloreDRMFileService(this, baseUrl, tenantId, tenantSecret);
for (let i = 0; i < items.length; i++) { for (let i = 0; i < items.length; i++) {
Logger.debug('Processing item', { itemIndex: i, itemData: items[i] }); Logger.debug(who + 'Processing item', { itemIndex: i });
try { try {
// Get parameters for this item // Get parameters for this item
Logger.debug(who + 'Getting node parameters', { itemIndex: i });
const hotfolderId = this.getNodeParameter('hotfolderId', i) as string; const hotfolderId = this.getNodeParameter('hotfolderId', i) as string;
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i) as string; const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i) as string;
const correlationId = this.getNodeParameter('correlationId', i) as string; const correlationId = crypto.randomUUID().toString();
const retryCount = this.getNodeParameter('retryCount', i) as number; const retryCount = this.getNodeParameter('retryCount', i) as number;
// Validate required parameters // Validate required parameters
@ -42,92 +190,95 @@ export async function protectWithHotFolder(this: IExecuteFunctions): Promise<Nod
}); });
} }
Logger.debug('Asserting binary data', { binaryPropertyName, itemIndex: i }); Logger.debug(who + 'Asserting binary data', { binaryPropertyName, itemIndex: i });
// Get input binary data // Get input binary data
const binaryData = this.helpers.assertBinaryData(i, binaryPropertyName); const binaryData = this.helpers.assertBinaryData(i, binaryPropertyName);
Logger.debug('Getting binary data buffer', { binaryPropertyName, itemIndex: i }); Logger.debug(who + 'Getting binary data buffer', { binaryPropertyName, itemIndex: i });
const fileBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName); const fileBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
Logger.debug('Binary data retrieved', { Logger.debug(who + 'Binary data retrieved', {
fileName: binaryData.fileName, fileName: binaryData.fileName,
fileSize: fileBuffer.length, fileSize: fileBuffer.length,
mimeType: binaryData.mimeType, mimeType: binaryData.mimeType,
itemIndex: i itemIndex: i,
correlationId,
retryCount
}); });
// Upload the file first // Use the combined upload, protect, and download function
const uploadResult = await fileService.uploadFile( try {
new Uint8Array(fileBuffer), Logger.debug(who + 'Starting protect file with hot folder operation', {
binaryData.fileName || 'file', fileName: binaryData.fileName,
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, hotfolderId,
fileStorageId: uploadResult.fileStorageId, correlationId,
}, retryCount,
correlationId || undefined, itemIndex: i
retryCount, });
);
Logger.info('File protected successfully', { const result = await protectFileWithHotFolder(
originalFileStorageId: uploadResult.fileStorageId, fileService,
protectedFileStorageId: protectResult.fileStorageId, fileBuffer,
secloreFileId: protectResult.secloreFileId, binaryData.fileName || 'file',
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, hotfolderId,
fileName: binaryData.fileName, correlationId,
fileSize: protectedFileData.length, retryCount,
correlationId: correlationId || null, );
},
binary: {
data: outputBinaryData,
},
};
returnData.push(returnItem); Logger.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
});
// Create output binary data
Logger.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,
);
// Create return item with binary data and metadata
const returnItem: INodeExecutionData = {
json: {
success: true,
originalFileStorageId: result.originalFileStorageId,
protectedFileStorageId: result.protectedFileStorageId,
secloreFileId: result.secloreFileId,
hotfolderId,
fileName: result.fileName,
fileSize: result.fileSize,
correlationId: correlationId,
},
binary: {
data: outputBinaryData,
},
};
Logger.debug(who + 'Adding result to return data', { itemIndex: i, success: true });
returnData.push(returnItem);
} catch (protectError) {
Logger.error(who + 'Protect file with hot folder operation failed', { protectError, itemIndex: i });
// Re-throw the error to be handled by the outer catch block
throw protectError;
}
} catch (error) { } catch (error) {
// Handle errors gracefully // Handle errors gracefully
Logger.error('Protect with HotFolder operation failed', { error, itemIndex: i }); Logger.error(who + 'Item processing failed', { error, itemIndex: i });
if (this.continueOnFail()) { if (this.continueOnFail()) {
Logger.debug(who + 'Continuing on fail, adding error item', { itemIndex: i, errorMessage: error.message });
const returnItem: INodeExecutionData = { const returnItem: INodeExecutionData = {
json: { json: {
success: false, success: false,
@ -137,6 +288,7 @@ export async function protectWithHotFolder(this: IExecuteFunctions): Promise<Nod
}; };
returnData.push(returnItem); returnData.push(returnItem);
} else { } else {
Logger.error(who + 'Throwing NodeOperationError', { error: error.message, itemIndex: i });
throw new NodeOperationError(this.getNode(), error.message, { throw new NodeOperationError(this.getNode(), error.message, {
itemIndex: i, itemIndex: i,
}); });
@ -144,7 +296,7 @@ export async function protectWithHotFolder(this: IExecuteFunctions): Promise<Nod
} }
} }
Logger.info('Seclore Protect with HotFolder operation completed', { Logger.debug(who + 'Seclore Protect with HotFolder operation completed', {
processedItems: returnData.length, processedItems: returnData.length,
successfulItems: returnData.filter(item => item.json.success).length successfulItems: returnData.filter(item => item.json.success).length
}); });

View File

@ -5,7 +5,7 @@ import {
NodeOutput, NodeOutput,
LoggerProxy as Logger, LoggerProxy as Logger,
} from 'n8n-workflow'; } from 'n8n-workflow';
import crypto from 'node:crypto';
import { SecloreDRMFileService } from '../Services/SecloreDRMFileService'; import { SecloreDRMFileService } from '../Services/SecloreDRMFileService';
/** /**
@ -53,7 +53,6 @@ async function deleteFile(
* @returns Promise containing the unprotected file data and metadata * @returns Promise containing the unprotected file data and metadata
*/ */
async function unprotectFile( async function unprotectFile(
context: IExecuteFunctions,
fileService: SecloreDRMFileService, fileService: SecloreDRMFileService,
fileBuffer: Buffer, fileBuffer: Buffer,
fileName: string, fileName: string,
@ -182,7 +181,7 @@ export async function unprotect(this: IExecuteFunctions): Promise<NodeOutput> {
// Get parameters for this item // Get parameters for this item
Logger.debug(who + 'Getting node parameters', { itemIndex: i }); Logger.debug(who + 'Getting node parameters', { itemIndex: i });
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i) as string; const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i) as string;
const correlationId = this.getNodeParameter('correlationId', i) as string; const correlationId = crypto.randomUUID().toString();
const retryCount = this.getNodeParameter('retryCount', i) as number; const retryCount = this.getNodeParameter('retryCount', i) as number;
Logger.debug(who + 'Asserting binary data', { binaryPropertyName, itemIndex: i }); Logger.debug(who + 'Asserting binary data', { binaryPropertyName, itemIndex: i });
@ -211,11 +210,10 @@ export async function unprotect(this: IExecuteFunctions): Promise<NodeOutput> {
}); });
const result = await unprotectFile( const result = await unprotectFile(
this,
fileService, fileService,
fileBuffer, fileBuffer,
binaryData.fileName || 'protected_file', binaryData.fileName || 'protected_file',
correlationId || undefined, correlationId,
retryCount, retryCount,
); );
@ -249,7 +247,7 @@ export async function unprotect(this: IExecuteFunctions): Promise<NodeOutput> {
unprotectedFileStorageId: result.unprotectedFileStorageId, unprotectedFileStorageId: result.unprotectedFileStorageId,
fileName: result.fileName, fileName: result.fileName,
fileSize: result.fileSize, fileSize: result.fileSize,
correlationId: correlationId || null, correlationId: correlationId,
}, },
binary: { binary: {
data: outputBinaryData, data: outputBinaryData,