delete original file once unprotected
This commit is contained in:
parent
b784b8d811
commit
14e7cbf9fc
|
|
@ -505,4 +505,56 @@ export class SecloreDRMApiService {
|
||||||
this.handleHttpError(error as NodeApiError);
|
this.handleHttpError(error as NodeApiError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes a file with fileStorageId from file storage of currently logged in Tenant.
|
||||||
|
*
|
||||||
|
* @param fileStorageId - Storage ID of the file to be deleted
|
||||||
|
* @param accessToken - JWT access token for authorization
|
||||||
|
* @param correlationId - Optional request ID for logging purpose
|
||||||
|
* @returns Promise<void> - No response body on successful deletion
|
||||||
|
* @throws Error on authentication failure or server error
|
||||||
|
*/
|
||||||
|
async deleteFile(
|
||||||
|
fileStorageId: string,
|
||||||
|
accessToken: string,
|
||||||
|
correlationId?: string,
|
||||||
|
): Promise<void> {
|
||||||
|
const who = "SecloreDRMApiService::deleteFile:: ";
|
||||||
|
try {
|
||||||
|
Logger.debug(who + 'Deleting file', {
|
||||||
|
fileStorageId,
|
||||||
|
correlationId
|
||||||
|
});
|
||||||
|
|
||||||
|
const headers: { [key: string]: string } = {
|
||||||
|
Authorization: `Bearer ${accessToken}`,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Add correlation ID if provided
|
||||||
|
if (correlationId) {
|
||||||
|
headers['X-SECLORE-CORRELATION-ID'] = correlationId;
|
||||||
|
}
|
||||||
|
|
||||||
|
const options: IHttpRequestOptions = {
|
||||||
|
method: 'DELETE',
|
||||||
|
url: `${this.baseUrl}/seclore/drm/filestorage/1.0/${fileStorageId}`,
|
||||||
|
headers,
|
||||||
|
};
|
||||||
|
|
||||||
|
Logger.debug(who + 'Making HTTP request', { url: options.url, method: options.method, fileStorageId, correlationId });
|
||||||
|
await this.context.helpers.httpRequest(options);
|
||||||
|
Logger.debug(who + 'File deletion successful', {
|
||||||
|
fileStorageId,
|
||||||
|
correlationId
|
||||||
|
});
|
||||||
|
} catch (error: unknown) {
|
||||||
|
Logger.error(who + 'File deletion failed', {
|
||||||
|
error,
|
||||||
|
fileStorageId,
|
||||||
|
correlationId
|
||||||
|
});
|
||||||
|
this.handleHttpError(error as NodeApiError);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -349,6 +349,29 @@ export class SecloreDRMFileService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete file with automatic authentication and retry
|
||||||
|
*/
|
||||||
|
async deleteFile(
|
||||||
|
fileStorageId: string,
|
||||||
|
correlationId?: string,
|
||||||
|
retryCount?: number,
|
||||||
|
): Promise<void> {
|
||||||
|
const who = "SecloreDRMFileService::deleteFile:: ";
|
||||||
|
try {
|
||||||
|
Logger.debug(who + 'Deleting file', { fileStorageId, correlationId });
|
||||||
|
await this.executeWithRetry(
|
||||||
|
(accessToken) => this.apiService.deleteFile(fileStorageId, accessToken, correlationId),
|
||||||
|
retryCount,
|
||||||
|
correlationId,
|
||||||
|
);
|
||||||
|
Logger.info(who + 'File deleted successfully', { fileStorageId, correlationId });
|
||||||
|
} catch (error) {
|
||||||
|
Logger.error(who + 'Delete file failed', { error, fileStorageId, correlationId });
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get current access token (for debugging/monitoring)
|
* Get current access token (for debugging/monitoring)
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,41 @@ import {
|
||||||
|
|
||||||
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 = "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'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Uploads a file, unprotects it, and downloads the unprotected version
|
* Uploads a file, unprotects it, and downloads the unprotected version
|
||||||
* @param fileService - The SecloreDRMFileService instance
|
* @param fileService - The SecloreDRMFileService instance
|
||||||
|
|
@ -31,91 +66,144 @@ async function unprotectFile(
|
||||||
fileName: string;
|
fileName: string;
|
||||||
fileSize: number;
|
fileSize: number;
|
||||||
}> {
|
}> {
|
||||||
// Upload the protected file
|
const who = "unprotect::unprotectFile:: ";
|
||||||
const uploadResult = await fileService.uploadFile(
|
var originalFileStorageId: string = '';
|
||||||
new Uint8Array(fileBuffer),
|
try {
|
||||||
fileName,
|
Logger.debug(who + 'Starting unprotect file operation', { fileName, fileSize: fileBuffer.length, correlationId, retryCount });
|
||||||
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.info('File uploaded successfully', { fileStorageId: uploadResult.fileStorageId, fileName });
|
Logger.debug(who + 'File uploaded successfully', { fileStorageId: uploadResult.fileStorageId, fileName, correlationId });
|
||||||
Logger.debug('File upload response', {uploadResult});
|
|
||||||
|
|
||||||
// Unprotect the uploaded file
|
originalFileStorageId = uploadResult.fileStorageId;
|
||||||
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 });
|
// 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,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
Logger.info('Downloading unprotected file', { fileStorageId: unprotectResult.fileStorageId, fileName });
|
// Unprotect the uploaded file
|
||||||
// Download the unprotected file
|
Logger.debug(who + 'Unprotecting file', { fileStorageId: uploadResult.fileStorageId, fileName, correlationId });
|
||||||
const unprotectedFileData = await fileService.downloadFile(
|
const unprotectResult = await fileService.unprotect(
|
||||||
unprotectResult.fileStorageId,
|
{
|
||||||
correlationId,
|
fileStorageId: uploadResult.fileStorageId,
|
||||||
retryCount,
|
},
|
||||||
);
|
correlationId,
|
||||||
|
retryCount,
|
||||||
|
);
|
||||||
|
|
||||||
Logger.info('Unprotected file downloaded successfully', { fileStorageId: unprotectResult.fileStorageId, fileSize: unprotectedFileData.length, fileName });
|
Logger.debug(who + 'File unprotected successfully', {
|
||||||
|
originalFileStorageId: uploadResult.fileStorageId,
|
||||||
|
unprotectedFileStorageId: unprotectResult.fileStorageId,
|
||||||
|
fileName,
|
||||||
|
correlationId
|
||||||
|
});
|
||||||
|
|
||||||
return {
|
// Download the unprotected file
|
||||||
unprotectedFileData,
|
Logger.debug(who + 'Downloading unprotected file', { fileStorageId: unprotectResult.fileStorageId, fileName, correlationId });
|
||||||
originalFileStorageId: uploadResult.fileStorageId,
|
const unprotectedFileData = await fileService.downloadFile(
|
||||||
unprotectedFileStorageId: unprotectResult.fileStorageId,
|
unprotectResult.fileStorageId,
|
||||||
fileName,
|
correlationId,
|
||||||
fileSize: unprotectedFileData.length,
|
retryCount,
|
||||||
};
|
);
|
||||||
|
|
||||||
|
Logger.debug(who + 'Unprotected file downloaded successfully', {
|
||||||
|
fileStorageId: unprotectResult.fileStorageId,
|
||||||
|
fileSize: unprotectedFileData.length,
|
||||||
|
fileName,
|
||||||
|
correlationId
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = {
|
||||||
|
unprotectedFileData,
|
||||||
|
originalFileStorageId: uploadResult.fileStorageId,
|
||||||
|
unprotectedFileStorageId: unprotectResult.fileStorageId,
|
||||||
|
fileName,
|
||||||
|
fileSize: unprotectedFileData.length,
|
||||||
|
};
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function unprotect(this: IExecuteFunctions): Promise<NodeOutput> {
|
export async function unprotect(this: IExecuteFunctions): Promise<NodeOutput> {
|
||||||
|
const who = "unprotect::unprotect:: ";
|
||||||
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 Unprotect operation started', { itemCount: items.length });
|
Logger.debug(who + 'Seclore Unprotect 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 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 = this.getNodeParameter('correlationId', i) as string;
|
||||||
const retryCount = this.getNodeParameter('retryCount', i) as number;
|
const retryCount = this.getNodeParameter('retryCount', i) as number;
|
||||||
|
|
||||||
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
|
||||||
});
|
});
|
||||||
|
|
||||||
// Use the combined upload, unprotect, and download function
|
// Use the combined upload, unprotect, and download function
|
||||||
try {
|
try {
|
||||||
Logger.debug('Starting unprotect file operation', {
|
Logger.debug(who + 'Starting unprotect file operation', {
|
||||||
fileName: binaryData.fileName,
|
fileName: binaryData.fileName,
|
||||||
correlationId,
|
correlationId,
|
||||||
retryCount,
|
retryCount,
|
||||||
|
|
@ -131,15 +219,22 @@ export async function unprotect(this: IExecuteFunctions): Promise<NodeOutput> {
|
||||||
retryCount,
|
retryCount,
|
||||||
);
|
);
|
||||||
|
|
||||||
Logger.info('Unprotect file operation completed successfully', {
|
Logger.debug(who + 'Unprotect file operation completed successfully', {
|
||||||
fileName: result.fileName,
|
fileName: result.fileName,
|
||||||
originalFileStorageId: result.originalFileStorageId,
|
originalFileStorageId: result.originalFileStorageId,
|
||||||
unprotectedFileStorageId: result.unprotectedFileStorageId,
|
unprotectedFileStorageId: result.unprotectedFileStorageId,
|
||||||
fileSize: result.fileSize,
|
fileSize: result.fileSize,
|
||||||
itemIndex: i
|
itemIndex: i,
|
||||||
|
correlationId
|
||||||
});
|
});
|
||||||
|
|
||||||
// Create output binary data
|
// Create output binary data
|
||||||
|
Logger.debug(who + 'Preparing binary data for output', {
|
||||||
|
fileName: binaryData.fileName,
|
||||||
|
mimeType: binaryData.mimeType,
|
||||||
|
fileSize: result.fileSize,
|
||||||
|
itemIndex: i
|
||||||
|
});
|
||||||
const outputBinaryData = await this.helpers.prepareBinaryData(
|
const outputBinaryData = await this.helpers.prepareBinaryData(
|
||||||
Buffer.from(result.unprotectedFileData),
|
Buffer.from(result.unprotectedFileData),
|
||||||
binaryData.fileName || 'unprotected_file',
|
binaryData.fileName || 'unprotected_file',
|
||||||
|
|
@ -161,17 +256,19 @@ export async function unprotect(this: IExecuteFunctions): Promise<NodeOutput> {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Logger.debug(who + 'Adding result to return data', { itemIndex: i, success: true });
|
||||||
returnData.push(returnItem);
|
returnData.push(returnItem);
|
||||||
} catch (unprotectError) {
|
} catch (unprotectError) {
|
||||||
Logger.error('Unprotect file operation failed with error', { unprotectError });
|
Logger.error(who + 'Unprotect file operation failed', { unprotectError, itemIndex: i });
|
||||||
|
|
||||||
// Re-throw the error to be handled by the outer catch block
|
// Re-throw the error to be handled by the outer catch block
|
||||||
throw unprotectError;
|
throw unprotectError;
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Handle errors gracefully
|
// Handle errors gracefully
|
||||||
Logger.error('Unprotect file operation failed with error', { error });
|
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,
|
||||||
|
|
@ -181,6 +278,7 @@ export async function unprotect(this: IExecuteFunctions): Promise<NodeOutput> {
|
||||||
};
|
};
|
||||||
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,
|
||||||
});
|
});
|
||||||
|
|
@ -188,7 +286,7 @@ export async function unprotect(this: IExecuteFunctions): Promise<NodeOutput> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger.info('Seclore Unprotect operation completed', {
|
Logger.debug(who + 'Seclore Unprotect operation completed', {
|
||||||
processedItems: returnData.length,
|
processedItems: returnData.length,
|
||||||
successfulItems: returnData.filter(item => item.json.success).length
|
successfulItems: returnData.filter(item => item.json.success).length
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue