new filename to be read from headers

This commit is contained in:
atharva.dev 2025-10-29 10:35:14 +05:30
parent bbee58f204
commit 9f46e5e8ff
9 changed files with 182 additions and 44 deletions

View File

@ -6,4 +6,14 @@ export interface IFileUploadResponse {
fileSize: number; fileSize: number;
secloreFileId: string; secloreFileId: string;
protected: boolean; protected: boolean;
headers?: { [key: string]: string };
}
export interface IFileDownloadResponse {
data: Uint8Array;
headers?: { [key: string]: string };
}
export interface IFileDeleteResponse {
headers?: { [key: string]: string };
} }

View File

@ -6,6 +6,7 @@ export interface ILoginRequest {
export interface ILoginResponse { export interface ILoginResponse {
accessToken: string; accessToken: string;
refreshToken: string; refreshToken: string;
headers?: { [key: string]: string };
} }
export interface IRefreshTokenRequest { export interface IRefreshTokenRequest {

View File

@ -14,6 +14,7 @@ export interface IProtectWithExternalRefIdRequest {
export interface IProtectWithExternalRefIdResponse { export interface IProtectWithExternalRefIdResponse {
fileStorageId: string; fileStorageId: string;
secloreFileId: string; secloreFileId: string;
headers?: { [key: string]: string };
} }
export interface IProtectWithFileIdRequest { export interface IProtectWithFileIdRequest {
@ -24,6 +25,7 @@ export interface IProtectWithFileIdRequest {
export interface IProtectWithFileIdResponse { export interface IProtectWithFileIdResponse {
fileStorageId: string; fileStorageId: string;
secloreFileId: string; secloreFileId: string;
headers?: { [key: string]: string };
} }
export interface IProtectWithHotFolderRequest { export interface IProtectWithHotFolderRequest {
@ -34,4 +36,5 @@ export interface IProtectWithHotFolderRequest {
export interface IProtectWithHotFolderResponse { export interface IProtectWithHotFolderResponse {
fileStorageId: string; fileStorageId: string;
secloreFileId: string; secloreFileId: string;
headers?: { [key: string]: string };
} }

View File

@ -4,4 +4,5 @@ export interface IUnprotectRequest {
export interface IUnprotectResponse { export interface IUnprotectResponse {
fileStorageId: string; fileStorageId: string;
headers?: { [key: string]: string };
} }

View File

@ -1,6 +1,6 @@
import { IExecuteFunctions, IHttpRequestOptions, NodeApiError, LoggerProxy as Logger } from 'n8n-workflow'; import { IExecuteFunctions, IHttpRequestOptions, NodeApiError, LoggerProxy as Logger } from 'n8n-workflow';
import { IErrorResponse } from './Interfaces/ErrorInterfaces'; import { IErrorResponse } from './Interfaces/ErrorInterfaces';
import { IFileUploadResponse } from './Interfaces/FileStorageInterfaces'; import { IFileUploadResponse, IFileDownloadResponse, IFileDeleteResponse } from './Interfaces/FileStorageInterfaces';
import { ILoginRequest, ILoginResponse, IRefreshTokenRequest } from './Interfaces/LoginInterfaces'; import { ILoginRequest, ILoginResponse, IRefreshTokenRequest } from './Interfaces/LoginInterfaces';
import { import {
IProtectWithExternalRefIdRequest, IProtectWithExternalRefIdRequest,
@ -95,9 +95,15 @@ export class SecloreDRMApiService {
}; };
Logger.debug(who + 'Making HTTP request', { url: options.url, method: options.method, correlationId }); Logger.debug(who + 'Making HTTP request', { url: options.url, method: options.method, correlationId });
const response = await this.context.helpers.httpRequest(options); const response = await this.context.helpers.httpRequest({ ...options, returnFullResponse: true });
Logger.debug(who + 'Login successful', { tenantId, correlationId }); Logger.debug(who + 'Login successful', { tenantId, correlationId });
return response as ILoginResponse;
const loginResponse: ILoginResponse = {
...(response.body as ILoginResponse),
headers: response.headers as { [key: string]: string }
};
return loginResponse;
} catch (error: unknown) { } catch (error: unknown) {
Logger.error(who + 'Login failed', { error, tenantId, correlationId }); Logger.error(who + 'Login failed', { error, tenantId, correlationId });
this.handleHttpError(error as NodeApiError); this.handleHttpError(error as NodeApiError);
@ -140,9 +146,15 @@ export class SecloreDRMApiService {
}; };
Logger.debug(who + 'Making HTTP request', { url: options.url, method: options.method, correlationId }); Logger.debug(who + 'Making HTTP request', { url: options.url, method: options.method, correlationId });
const response = await this.context.helpers.httpRequest(options); const response = await this.context.helpers.httpRequest({ ...options, returnFullResponse: true });
Logger.debug(who + 'Token refresh successful', { correlationId }); Logger.debug(who + 'Token refresh successful', { correlationId });
return response as ILoginResponse;
const refreshResponse: ILoginResponse = {
...(response.body as ILoginResponse),
headers: response.headers as { [key: string]: string }
};
return refreshResponse;
} catch (error: unknown) { } catch (error: unknown) {
Logger.error(who + 'Token refresh failed', { error, correlationId }); Logger.error(who + 'Token refresh failed', { error, correlationId });
this.handleHttpError(error as NodeApiError, { 401: 'Unauthorized' }); this.handleHttpError(error as NodeApiError, { 401: 'Unauthorized' });
@ -190,13 +202,19 @@ export class SecloreDRMApiService {
}; };
Logger.debug(who + 'Making HTTP request', { url: options.url, method: options.method, correlationId }); Logger.debug(who + 'Making HTTP request', { url: options.url, method: options.method, correlationId });
const response = await this.context.helpers.httpRequest(options); const response = await this.context.helpers.httpRequest({ ...options, returnFullResponse: true });
Logger.debug(who + 'Protection with external ref ID successful', { Logger.debug(who + 'Protection with external ref ID successful', {
fileStorageId: protectRequest.fileStorageId, fileStorageId: protectRequest.fileStorageId,
secloreFileId: (response as IProtectWithExternalRefIdResponse).secloreFileId, secloreFileId: (response.body as IProtectWithExternalRefIdResponse).secloreFileId,
correlationId correlationId
}); });
return response as IProtectWithExternalRefIdResponse;
const protectResponse: IProtectWithExternalRefIdResponse = {
...(response.body as IProtectWithExternalRefIdResponse),
headers: response.headers as { [key: string]: string }
};
return protectResponse;
} catch (error: unknown) { } catch (error: unknown) {
Logger.error(who + 'Protection with external ref ID failed', { Logger.error(who + 'Protection with external ref ID failed', {
error, error,
@ -248,13 +266,19 @@ export class SecloreDRMApiService {
}; };
Logger.debug(who + 'Making HTTP request', { url: options.url, method: options.method, correlationId }); Logger.debug(who + 'Making HTTP request', { url: options.url, method: options.method, correlationId });
const response = await this.context.helpers.httpRequest(options); const response = await this.context.helpers.httpRequest({ ...options, returnFullResponse: true });
Logger.debug(who + 'Protection with file ID successful', { Logger.debug(who + 'Protection with file ID successful', {
existingProtectedFileId: protectRequest.existingProtectedFileId, existingProtectedFileId: protectRequest.existingProtectedFileId,
secloreFileId: (response as IProtectWithFileIdResponse).secloreFileId, secloreFileId: (response.body as IProtectWithFileIdResponse).secloreFileId,
correlationId correlationId
}); });
return response as IProtectWithFileIdResponse;
const protectResponse: IProtectWithFileIdResponse = {
...(response.body as IProtectWithFileIdResponse),
headers: response.headers as { [key: string]: string }
};
return protectResponse;
} catch (error: unknown) { } catch (error: unknown) {
Logger.error(who + 'Protection with file ID failed', { Logger.error(who + 'Protection with file ID failed', {
error, error,
@ -306,13 +330,19 @@ export class SecloreDRMApiService {
}; };
Logger.debug(who + 'Making HTTP request', { url: options.url, method: options.method, correlationId }); Logger.debug(who + 'Making HTTP request', { url: options.url, method: options.method, correlationId });
const response = await this.context.helpers.httpRequest(options); const response = await this.context.helpers.httpRequest({ ...options, returnFullResponse: true });
Logger.debug(who + 'Protection with hot folder successful', { Logger.debug(who + 'Protection with hot folder successful', {
hotfolderId: protectRequest.hotfolderId, hotfolderId: protectRequest.hotfolderId,
secloreFileId: (response as IProtectWithHotFolderResponse).secloreFileId, secloreFileId: (response.body as IProtectWithHotFolderResponse).secloreFileId,
correlationId correlationId
}); });
return response as IProtectWithHotFolderResponse;
const protectResponse: IProtectWithHotFolderResponse = {
...(response.body as IProtectWithHotFolderResponse),
headers: response.headers as { [key: string]: string }
};
return protectResponse;
} catch (error: unknown) { } catch (error: unknown) {
Logger.error(who + 'Protection with hot folder failed', { Logger.error(who + 'Protection with hot folder failed', {
error, error,
@ -364,13 +394,19 @@ export class SecloreDRMApiService {
}; };
Logger.debug(who + 'Making HTTP request', { url: options.url, method: options.method, correlationId }); Logger.debug(who + 'Making HTTP request', { url: options.url, method: options.method, correlationId });
const response = await this.context.helpers.httpRequest(options); const response = await this.context.helpers.httpRequest({ ...options, returnFullResponse: true });
Logger.debug(who + 'Unprotection successful', { Logger.debug(who + 'Unprotection successful', {
originalFileStorageId: unprotectRequest.fileStorageId, originalFileStorageId: unprotectRequest.fileStorageId,
unprotectedFileStorageId: (response as IUnprotectResponse).fileStorageId, unprotectedFileStorageId: (response.body as IUnprotectResponse).fileStorageId,
correlationId correlationId
}); });
return response as IUnprotectResponse;
const unprotectResponse: IUnprotectResponse = {
...(response.body as IUnprotectResponse),
headers: response.headers as { [key: string]: string }
};
return unprotectResponse;
} catch (error: unknown) { } catch (error: unknown) {
Logger.error(who + 'Unprotection failed', { Logger.error(who + 'Unprotection failed', {
error, error,
@ -427,13 +463,19 @@ export class SecloreDRMApiService {
}; };
Logger.debug(who + 'Making HTTP request', { url: options.url, method: options.method, fileName, correlationId }); Logger.debug(who + 'Making HTTP request', { url: options.url, method: options.method, fileName, correlationId });
const response = await this.context.helpers.httpRequest(options); const response = await this.context.helpers.httpRequest({ ...options, returnFullResponse: true });
Logger.debug(who + 'File upload successful', { Logger.debug(who + 'File upload successful', {
fileName, fileName,
fileStorageId: (response as IFileUploadResponse).fileStorageId, fileStorageId: (response.body as IFileUploadResponse).fileStorageId,
correlationId correlationId
}); });
return response as IFileUploadResponse;
const uploadResponse: IFileUploadResponse = {
...(response.body as IFileUploadResponse),
headers: response.headers as { [key: string]: string }
};
return uploadResponse;
} catch (error: unknown) { } catch (error: unknown) {
Logger.error(who + 'File upload failed', { Logger.error(who + 'File upload failed', {
error, error,
@ -452,14 +494,14 @@ export class SecloreDRMApiService {
* @param fileStorageId - Storage ID of the file to be retrieved * @param fileStorageId - Storage ID of the file to be retrieved
* @param accessToken - JWT access token for authorization * @param accessToken - JWT access token for authorization
* @param correlationId - Optional request ID for logging purpose * @param correlationId - Optional request ID for logging purpose
* @returns Promise<Uint8Array> - The downloaded file data * @returns Promise<IFileDownloadResponse> - The downloaded file data with headers
* @throws Error on authentication failure or server error * @throws Error on authentication failure or server error
*/ */
async downloadFile( async downloadFile(
fileStorageId: string, fileStorageId: string,
accessToken: string, accessToken: string,
correlationId?: string, correlationId?: string,
): Promise<Uint8Array> { ): Promise<IFileDownloadResponse> {
const who = "SecloreDRMApiService::downloadFile:: "; const who = "SecloreDRMApiService::downloadFile:: ";
try { try {
Logger.debug(who + 'Downloading file', { Logger.debug(who + 'Downloading file', {
@ -484,14 +526,20 @@ export class SecloreDRMApiService {
}; };
Logger.debug(who + 'Making HTTP request', { url: options.url, method: options.method, fileStorageId, correlationId }); Logger.debug(who + 'Making HTTP request', { url: options.url, method: options.method, fileStorageId, correlationId });
const response = await this.context.helpers.httpRequest(options); const response = await this.context.helpers.httpRequest({ ...options, returnFullResponse: true });
const fileData = new Uint8Array(response as ArrayBuffer); const fileData = new Uint8Array(response.body as ArrayBuffer);
Logger.debug(who + 'File download successful', { Logger.debug(who + 'File download successful', {
fileStorageId, fileStorageId,
fileSize: fileData.length, fileSize: fileData.length,
correlationId correlationId
}); });
return fileData;
const downloadResponse: IFileDownloadResponse = {
data: fileData,
headers: response.headers as { [key: string]: string }
};
return downloadResponse;
} catch (error: unknown) { } catch (error: unknown) {
Logger.error(who + 'File download failed', { Logger.error(who + 'File download failed', {
error, error,
@ -508,14 +556,14 @@ export class SecloreDRMApiService {
* @param fileStorageId - Storage ID of the file to be deleted * @param fileStorageId - Storage ID of the file to be deleted
* @param accessToken - JWT access token for authorization * @param accessToken - JWT access token for authorization
* @param correlationId - Optional request ID for logging purpose * @param correlationId - Optional request ID for logging purpose
* @returns Promise<void> - No response body on successful deletion * @returns Promise<IFileDeleteResponse> - Response headers on successful deletion
* @throws Error on authentication failure or server error * @throws Error on authentication failure or server error
*/ */
async deleteFile( async deleteFile(
fileStorageId: string, fileStorageId: string,
accessToken: string, accessToken: string,
correlationId?: string, correlationId?: string,
): Promise<void> { ): Promise<IFileDeleteResponse> {
const who = "SecloreDRMApiService::deleteFile:: "; const who = "SecloreDRMApiService::deleteFile:: ";
try { try {
Logger.debug(who + 'Deleting file', { Logger.debug(who + 'Deleting file', {
@ -539,11 +587,17 @@ export class SecloreDRMApiService {
}; };
Logger.debug(who + 'Making HTTP request', { url: options.url, method: options.method, fileStorageId, correlationId }); Logger.debug(who + 'Making HTTP request', { url: options.url, method: options.method, fileStorageId, correlationId });
await this.context.helpers.httpRequest(options); const response = await this.context.helpers.httpRequest({ ...options, returnFullResponse: true });
Logger.debug(who + 'File deletion successful', { Logger.debug(who + 'File deletion successful', {
fileStorageId, fileStorageId,
correlationId correlationId
}); });
const deleteResponse: IFileDeleteResponse = {
headers: response.headers as { [key: string]: string }
};
return deleteResponse;
} catch (error: unknown) { } catch (error: unknown) {
Logger.error(who + 'File deletion failed', { Logger.error(who + 'File deletion failed', {
error, error,

View File

@ -1,5 +1,5 @@
import { IExecuteFunctions, LoggerProxy as Logger } from 'n8n-workflow'; import { IExecuteFunctions, LoggerProxy as Logger } from 'n8n-workflow';
import { IFileUploadResponse } from './Interfaces/FileStorageInterfaces'; import { IFileDownloadResponse, IFileUploadResponse } from './Interfaces/FileStorageInterfaces';
import { import {
IProtectWithExternalRefIdRequest, IProtectWithExternalRefIdRequest,
IProtectWithExternalRefIdResponse, IProtectWithExternalRefIdResponse,
@ -332,7 +332,7 @@ export class SecloreDRMFileService {
fileStorageId: string, fileStorageId: string,
correlationId?: string, correlationId?: string,
retryCount?: number, retryCount?: number,
): Promise<Uint8Array> { ): Promise<IFileDownloadResponse> {
const who = "SecloreDRMFileService::downloadFile:: "; const who = "SecloreDRMFileService::downloadFile:: ";
try { try {
Logger.debug(who + 'Downloading file', { fileStorageId, correlationId }); Logger.debug(who + 'Downloading file', { fileStorageId, correlationId });
@ -341,7 +341,7 @@ export class SecloreDRMFileService {
retryCount, retryCount,
correlationId, correlationId,
); );
Logger.info(who + 'File downloaded successfully', { fileStorageId, fileSize: result.length, correlationId }); Logger.info(who + 'File downloaded successfully', { fileStorageId, fileSize: result.data.length, correlationId });
return result; return result;
} catch (error) { } catch (error) {
Logger.error(who + 'Download file failed', { error, fileStorageId, correlationId }); Logger.error(who + 'Download file failed', { error, fileStorageId, correlationId });

View File

@ -0,0 +1,60 @@
import { LoggerProxy as Logger } from 'n8n-workflow';
/**
* Extracts filename from Content-Disposition header
* @param headers - Response headers object
* @returns The extracted filename or null if not found
*/
export function getFileNameFromHeaders(headers?: { [key: string]: string }): string | null {
const who = "Utils::getFileNameFromHeaders:: ";
if (!headers) {
Logger.debug(who + 'No headers provided');
return null;
}
// Look for content-disposition header (case-insensitive)
const contentDisposition = Object.keys(headers).find(key =>
key.toLowerCase() === 'content-disposition'
);
if (!contentDisposition || !headers[contentDisposition]) {
Logger.debug(who + 'Content-Disposition header not found');
return null;
}
const headerValue = headers[contentDisposition];
Logger.debug(who + 'Found Content-Disposition header', { headerValue });
// Handle different filename formats in Content-Disposition header
// Format 1: filename*=UTF-8''encoded-filename
const utf8Match = headerValue.match(/filename\*=UTF-8''([^;]+)/i);
if (utf8Match) {
try {
const decodedFilename = decodeURIComponent(utf8Match[1]);
Logger.debug(who + 'Extracted filename from UTF-8 format', { filename: decodedFilename });
return decodedFilename;
} catch (error) {
Logger.error(who + 'Failed to decode UTF-8 filename', { error, encodedFilename: utf8Match[1] });
}
}
// Format 2: filename="quoted-filename"
const quotedMatch = headerValue.match(/filename="([^"]+)"/i);
if (quotedMatch) {
Logger.debug(who + 'Extracted filename from quoted format', { filename: quotedMatch[1] });
return quotedMatch[1];
}
// Format 3: filename=unquoted-filename
const unquotedMatch = headerValue.match(/filename=([^;]+)/i);
if (unquotedMatch) {
const filename = unquotedMatch[1].trim();
Logger.debug(who + 'Extracted filename from unquoted format', { filename });
return filename;
}
Logger.debug(who + 'Could not extract filename from Content-Disposition header', { headerValue });
return null;
}

View File

@ -7,6 +7,7 @@ import {
} from 'n8n-workflow'; } from 'n8n-workflow';
import crypto from 'node:crypto'; import crypto from 'node:crypto';
import { SecloreDRMFileService } from '../Services/SecloreDRMFileService'; import { SecloreDRMFileService } from '../Services/SecloreDRMFileService';
import { getFileNameFromHeaders } from '../Services/Utils';
/** /**
* Deletes a file from storage with error handling (does not throw errors) * Deletes a file from storage with error handling (does not throw errors)
@ -70,7 +71,6 @@ async function protectFileWithHotFolder(
}> { }> {
const who = "protectWithHotFolder::protectFileWithHotFolder:: "; const who = "protectWithHotFolder::protectFileWithHotFolder:: ";
var originalFileStorageId: string = ''; var originalFileStorageId: string = '';
const protectedFileName = `${fileName}.html`;
try { try {
Logger.debug(who + 'Starting protect file with hot folder operation', { fileName, fileSize: fileBuffer.length, hotfolderId, correlationId, retryCount }); Logger.debug(who + 'Starting protect file with hot folder operation', { fileName, fileSize: fileBuffer.length, hotfolderId, correlationId, retryCount });
@ -120,24 +120,28 @@ async function protectFileWithHotFolder(
retryCount, retryCount,
); );
// Try to get the actual filename from response headers, fallback to constructed name
const actualFileName = getFileNameFromHeaders(protectedFileData.headers) || fileName;
Logger.debug(who + 'Protected file downloaded successfully', { Logger.debug(who + 'Protected file downloaded successfully', {
fileStorageId: protectResult.fileStorageId, fileStorageId: protectResult.fileStorageId,
fileSize: protectedFileData.length, fileSize: protectedFileData.data.length,
fileName: protectedFileName, originalFileName: fileName,
actualFileName,
correlationId correlationId
}); });
const result = { const result = {
protectedFileData, protectedFileData: protectedFileData.data,
originalFileStorageId: uploadResult.fileStorageId, originalFileStorageId: uploadResult.fileStorageId,
protectedFileStorageId: protectResult.fileStorageId, protectedFileStorageId: protectResult.fileStorageId,
secloreFileId: protectResult.secloreFileId, secloreFileId: protectResult.secloreFileId,
fileName: protectedFileName, fileName: actualFileName,
fileSize: protectedFileData.length, fileSize: protectedFileData.data.length,
}; };
Logger.debug(who + 'Protect file with hot folder operation completed successfully', { Logger.debug(who + 'Protect file with hot folder operation completed successfully', {
fileName: protectedFileName, fileName: result.fileName,
originalFileStorageId: result.originalFileStorageId, originalFileStorageId: result.originalFileStorageId,
protectedFileStorageId: result.protectedFileStorageId, protectedFileStorageId: result.protectedFileStorageId,
secloreFileId: result.secloreFileId, secloreFileId: result.secloreFileId,

View File

@ -7,6 +7,7 @@ import {
} from 'n8n-workflow'; } from 'n8n-workflow';
import crypto from 'node:crypto'; import crypto from 'node:crypto';
import { SecloreDRMFileService } from '../Services/SecloreDRMFileService'; import { SecloreDRMFileService } from '../Services/SecloreDRMFileService';
import { getFileNameFromHeaders } from '../Services/Utils';
/** /**
* Deletes a file from storage with error handling (does not throw errors) * Deletes a file from storage with error handling (does not throw errors)
@ -120,19 +121,23 @@ async function unprotectFile(
retryCount, retryCount,
); );
// Try to get the actual filename from response headers, fallback to original filename
const actualFileName = getFileNameFromHeaders(unprotectedFileData.headers) || fileName;
Logger.debug(who + 'Unprotected file downloaded successfully', { Logger.debug(who + 'Unprotected file downloaded successfully', {
fileStorageId: unprotectResult.fileStorageId, fileStorageId: unprotectResult.fileStorageId,
fileSize: unprotectedFileData.length, fileSize: unprotectedFileData.data.length,
fileName, originalFileName: fileName,
actualFileName: actualFileName,
correlationId correlationId
}); });
const result = { const result = {
unprotectedFileData, unprotectedFileData: unprotectedFileData.data,
originalFileStorageId: uploadResult.fileStorageId, originalFileStorageId: uploadResult.fileStorageId,
unprotectedFileStorageId: unprotectResult.fileStorageId, unprotectedFileStorageId: unprotectResult.fileStorageId,
fileName, fileName: actualFileName,
fileSize: unprotectedFileData.length, fileSize: unprotectedFileData.data.length,
}; };
Logger.debug(who + 'Unprotect file operation completed successfully', { Logger.debug(who + 'Unprotect file operation completed successfully', {
@ -235,7 +240,7 @@ export async function unprotect(this: IExecuteFunctions): Promise<NodeOutput> {
}); });
const outputBinaryData = await this.helpers.prepareBinaryData( const outputBinaryData = await this.helpers.prepareBinaryData(
Buffer.from(result.unprotectedFileData), Buffer.from(result.unprotectedFileData),
binaryData.fileName || 'unprotected_file', result.fileName,
binaryData.mimeType, binaryData.mimeType,
); );