n8n-nodes-seclore/dist/nodes/SecloreProtect/Services/SecloreDRMFileService.js

217 lines
11 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SecloreDRMFileService = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const SecloreDRMApiService_1 = require("./SecloreDRMApiService");
class SecloreDRMFileService {
constructor(context, baseUrl, tenantId, tenantSecret, defaultRetryCount = 3) {
this.defaultRetryCount = defaultRetryCount;
this.apiService = new SecloreDRMApiService_1.SecloreDRMApiService(context, baseUrl);
this.tenantId = tenantId;
this.tenantSecret = tenantSecret;
}
async ensureAuthenticated(correlationId) {
if (!this.accessToken || (this.tokenExpiry && new Date() >= this.tokenExpiry)) {
if (this.loginPromise) {
await this.loginPromise;
return;
}
this.loginPromise = this.login(correlationId);
try {
await this.loginPromise;
}
finally {
this.loginPromise = undefined;
}
}
}
async login(correlationId) {
const who = "SecloreDRMFileService::login:: ";
try {
n8n_workflow_1.LoggerProxy.debug(who + 'Attempting login', { tenantId: this.tenantId, correlationId });
const loginResponse = await this.apiService.login(this.tenantId, this.tenantSecret, correlationId);
this.accessToken = loginResponse.accessToken;
this.refreshToken = loginResponse.refreshToken;
this.tokenExpiry = new Date(Date.now() + 50 * 60 * 1000);
n8n_workflow_1.LoggerProxy.info(who + 'Login successful', { tenantId: this.tenantId, correlationId });
}
catch (error) {
n8n_workflow_1.LoggerProxy.error(who + 'Login failed', { error, tenantId: this.tenantId, correlationId });
this.clearTokens();
throw error;
}
}
async refreshAccessToken(correlationId) {
if (this.refreshPromise) {
await this.refreshPromise;
return;
}
if (!this.refreshToken) {
throw new Error('No refresh token available');
}
this.refreshPromise = this.performTokenRefresh(correlationId);
try {
await this.refreshPromise;
}
finally {
this.refreshPromise = undefined;
}
}
async performTokenRefresh(correlationId) {
const who = "SecloreDRMFileService::performTokenRefresh:: ";
try {
n8n_workflow_1.LoggerProxy.debug(who + 'Attempting token refresh', { correlationId });
const refreshResponse = await this.apiService.refreshToken(this.refreshToken, correlationId);
this.accessToken = refreshResponse.accessToken;
this.refreshToken = refreshResponse.refreshToken;
this.tokenExpiry = new Date(Date.now() + 50 * 60 * 1000);
n8n_workflow_1.LoggerProxy.info(who + 'Token refresh successful', { correlationId });
}
catch (error) {
n8n_workflow_1.LoggerProxy.error(who + 'Token refresh failed', { error, correlationId });
this.clearTokens();
throw error;
}
}
clearTokens() {
this.accessToken = undefined;
this.refreshToken = undefined;
this.tokenExpiry = undefined;
this.refreshPromise = undefined;
this.loginPromise = undefined;
}
async executeWithRetry(apiCall, retryCount = this.defaultRetryCount, correlationId) {
let lastError;
for (let attempt = 0; attempt <= retryCount; attempt++) {
try {
await this.ensureAuthenticated(correlationId);
if (!this.accessToken) {
throw new Error('Failed to obtain access token');
}
return await apiCall(this.accessToken);
}
catch (error) {
lastError = error;
if (error.message.includes('Unauthorized') && attempt < retryCount) {
try {
await this.refreshAccessToken(correlationId);
continue;
}
catch (refreshError) {
n8n_workflow_1.LoggerProxy.error('SecloreDRMFileService::executeWithRetry:: Token refresh failed', { refreshError, correlationId });
this.clearTokens();
}
}
if (attempt === retryCount) {
throw lastError;
}
await new Promise((resolve) => {
const delay = Math.pow(2, attempt) * 1000;
const start = Date.now();
while (Date.now() - start < delay) {
}
resolve(undefined);
});
}
}
throw lastError;
}
async protectWithExternalRefId(protectRequest, correlationId, retryCount) {
const who = "SecloreDRMFileService::protectWithExternalRefId:: ";
try {
n8n_workflow_1.LoggerProxy.debug(who + 'Protecting file with external ref ID', { fileStorageId: protectRequest.fileStorageId, hotfolderExternalReferenceId: protectRequest.hotfolderExternalReference.externalReferenceId, correlationId });
const result = await this.executeWithRetry((accessToken) => this.apiService.protectWithExternalRefId(protectRequest, accessToken, correlationId), retryCount, correlationId);
n8n_workflow_1.LoggerProxy.info(who + 'File protected with external ref ID successfully', { fileStorageId: protectRequest.fileStorageId, secloreFileId: result.secloreFileId, correlationId });
return result;
}
catch (error) {
n8n_workflow_1.LoggerProxy.error(who + 'Protect with external ref ID failed', { error, fileStorageId: protectRequest.fileStorageId, correlationId });
throw error;
}
}
async protectWithFileId(protectRequest, correlationId, retryCount) {
const who = "SecloreDRMFileService::protectWithFileId:: ";
try {
n8n_workflow_1.LoggerProxy.debug(who + 'Protecting file with file ID', { existingProtectedFileId: protectRequest.existingProtectedFileId, fileStorageId: protectRequest.fileStorageId, correlationId });
const result = await this.executeWithRetry((accessToken) => this.apiService.protectWithFileId(protectRequest, accessToken, correlationId), retryCount, correlationId);
n8n_workflow_1.LoggerProxy.info(who + 'File protected with file ID successfully', { existingProtectedFileId: protectRequest.existingProtectedFileId, secloreFileId: result.secloreFileId, correlationId });
return result;
}
catch (error) {
n8n_workflow_1.LoggerProxy.error(who + 'Protect with file ID failed', { error, existingProtectedFileId: protectRequest.existingProtectedFileId, correlationId });
throw error;
}
}
async protectWithHotFolder(protectRequest, correlationId, retryCount) {
const who = "SecloreDRMFileService::protectWithHotFolder:: ";
try {
n8n_workflow_1.LoggerProxy.debug(who + 'Protecting file with hot folder', { hotfolderId: protectRequest.hotfolderId, fileStorageId: protectRequest.fileStorageId, correlationId });
const result = await this.executeWithRetry((accessToken) => this.apiService.protectWithHotFolder(protectRequest, accessToken, correlationId), retryCount, correlationId);
n8n_workflow_1.LoggerProxy.info(who + 'File protected with hot folder successfully', { hotfolderId: protectRequest.hotfolderId, fileStorageId: protectRequest.fileStorageId, secloreFileId: result.secloreFileId, correlationId });
return result;
}
catch (error) {
n8n_workflow_1.LoggerProxy.error(who + 'Protect with hot folder failed', { error, hotfolderId: protectRequest.hotfolderId, fileStorageId: protectRequest.fileStorageId, correlationId });
throw error;
}
}
async unprotect(unprotectRequest, correlationId, retryCount) {
const who = "SecloreDRMFileService::unprotect:: ";
try {
n8n_workflow_1.LoggerProxy.debug(who + 'Unprotecting file', { fileStorageId: unprotectRequest.fileStorageId, correlationId });
const result = await this.executeWithRetry((accessToken) => this.apiService.unprotect(unprotectRequest, accessToken, correlationId), retryCount, correlationId);
n8n_workflow_1.LoggerProxy.info(who + 'File unprotected successfully', { originalFileStorageId: unprotectRequest.fileStorageId, unprotectedFileStorageId: result.fileStorageId, correlationId });
return result;
}
catch (error) {
n8n_workflow_1.LoggerProxy.error(who + 'Unprotect file failed', { error, fileStorageId: unprotectRequest.fileStorageId, correlationId });
throw error;
}
}
async uploadFile(fileBuffer, fileName, correlationId, retryCount) {
const who = "SecloreDRMFileService::uploadFile:: ";
try {
n8n_workflow_1.LoggerProxy.debug(who + 'Uploading file', { fileName, correlationId });
const result = await this.executeWithRetry((accessToken) => this.apiService.uploadFile(fileBuffer, fileName, accessToken, correlationId), retryCount, correlationId);
n8n_workflow_1.LoggerProxy.info(who + 'File uploaded successfully', { fileName, correlationId });
return result;
}
catch (error) {
n8n_workflow_1.LoggerProxy.error(who + 'Upload file failed', { error, fileName, correlationId });
throw error;
}
}
async downloadFile(fileStorageId, correlationId, retryCount) {
const who = "SecloreDRMFileService::downloadFile:: ";
try {
n8n_workflow_1.LoggerProxy.debug(who + 'Downloading file', { fileStorageId, correlationId });
const result = await this.executeWithRetry((accessToken) => this.apiService.downloadFile(fileStorageId, accessToken, correlationId), retryCount, correlationId);
n8n_workflow_1.LoggerProxy.info(who + 'File downloaded successfully', { fileStorageId, fileSize: result.data.length, correlationId });
return result;
}
catch (error) {
n8n_workflow_1.LoggerProxy.error(who + 'Download file failed', { error, fileStorageId, correlationId });
throw error;
}
}
async deleteFile(fileStorageId, correlationId, retryCount) {
const who = "SecloreDRMFileService::deleteFile:: ";
try {
n8n_workflow_1.LoggerProxy.debug(who + 'Deleting file', { fileStorageId, correlationId });
await this.executeWithRetry((accessToken) => this.apiService.deleteFile(fileStorageId, accessToken, correlationId), retryCount, correlationId);
n8n_workflow_1.LoggerProxy.info(who + 'File deleted successfully', { fileStorageId, correlationId });
}
catch (error) {
n8n_workflow_1.LoggerProxy.error(who + 'Delete file failed', { error, fileStorageId, correlationId });
throw error;
}
}
getAccessToken() {
return this.accessToken;
}
isAuthenticated() {
return !!this.accessToken && (!this.tokenExpiry || new Date() < this.tokenExpiry);
}
}
exports.SecloreDRMFileService = SecloreDRMFileService;
//# sourceMappingURL=SecloreDRMFileService.js.map