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

403 lines
17 KiB
JavaScript
Raw Normal View History

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SecloreDRMApiService = void 0;
const n8n_workflow_1 = require("n8n-workflow");
class SecloreDRMApiService {
constructor(context, baseUrl) {
this.context = context;
this.baseUrl = baseUrl;
}
handleHttpError(error, customMessages) {
var _a;
const statusCode = parseInt((_a = error.httpCode) !== null && _a !== void 0 ? _a : '0');
const errorResponse = error.errorResponse;
if (customMessages && customMessages[statusCode]) {
throw new Error(`${customMessages[statusCode]}: ${(errorResponse === null || errorResponse === void 0 ? void 0 : errorResponse.errorMessage) || 'Unknown error'}`);
}
switch (statusCode) {
case 400:
throw new Error(`Bad Request: ${(errorResponse === null || errorResponse === void 0 ? void 0 : errorResponse.errorMessage) || 'Invalid request data'}`);
case 401:
throw new Error(`Unauthorized: ${(errorResponse === null || errorResponse === void 0 ? void 0 : errorResponse.errorMessage) || 'Invalid credentials'}`);
case 413:
throw new Error(`Payload Too Large: ${(errorResponse === null || errorResponse === void 0 ? void 0 : errorResponse.errorMessage) || 'File size exceeds limit'}`);
case 500:
throw new Error(`Server Error: ${(errorResponse === null || errorResponse === void 0 ? void 0 : errorResponse.errorMessage) || 'Internal server error'}`);
default:
throw error;
}
}
async login(tenantId, tenantSecret, correlationId) {
const who = "SecloreDRMApiService::login:: ";
try {
n8n_workflow_1.LoggerProxy.debug(who + 'Attempting login', { tenantId, correlationId });
const requestBody = {
tenantId,
tenantSecret,
};
const headers = {
'Content-Type': 'application/json',
};
if (correlationId) {
headers['X-SECLORE-CORRELATION-ID'] = correlationId;
}
const options = {
method: 'POST',
url: `${this.baseUrl}/seclore/drm/1.0/auth/login`,
headers,
body: requestBody,
json: true,
};
n8n_workflow_1.LoggerProxy.debug(who + 'Making HTTP request', { url: options.url, method: options.method, correlationId });
const response = await this.context.helpers.httpRequest({ ...options, returnFullResponse: true });
n8n_workflow_1.LoggerProxy.debug(who + 'Login successful', { tenantId, correlationId });
const loginResponse = {
...response.body,
headers: response.headers
};
return loginResponse;
}
catch (error) {
n8n_workflow_1.LoggerProxy.error(who + 'Login failed', { error, tenantId, correlationId });
this.handleHttpError(error);
}
}
async refreshToken(refreshToken, correlationId) {
const who = "SecloreDRMApiService::refreshToken:: ";
try {
n8n_workflow_1.LoggerProxy.debug(who + 'Attempting token refresh', { correlationId });
const requestBody = {
refreshToken,
};
const headers = {
'Content-Type': 'application/json',
};
if (correlationId) {
headers['X-SECLORE-CORRELATION-ID'] = correlationId;
}
const options = {
method: 'POST',
url: `${this.baseUrl}/seclore/drm/1.0/auth/refresh`,
headers,
body: requestBody,
json: true,
};
n8n_workflow_1.LoggerProxy.debug(who + 'Making HTTP request', { url: options.url, method: options.method, correlationId });
const response = await this.context.helpers.httpRequest({ ...options, returnFullResponse: true });
n8n_workflow_1.LoggerProxy.debug(who + 'Token refresh successful', { correlationId });
const refreshResponse = {
...response.body,
headers: response.headers
};
return refreshResponse;
}
catch (error) {
n8n_workflow_1.LoggerProxy.error(who + 'Token refresh failed', { error, correlationId });
this.handleHttpError(error, { 401: 'Unauthorized' });
}
}
async protectWithExternalRefId(protectRequest, accessToken, correlationId) {
const who = "SecloreDRMApiService::protectWithExternalRefId:: ";
try {
n8n_workflow_1.LoggerProxy.debug(who + 'Protecting file with external ref ID', {
fileStorageId: protectRequest.fileStorageId,
hotfolderExternalReferenceId: protectRequest.hotfolderExternalReference.externalReferenceId,
correlationId
});
const headers = {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
};
if (correlationId) {
headers['X-SECLORE-CORRELATION-ID'] = correlationId;
}
const options = {
method: 'POST',
url: `${this.baseUrl}/seclore/drm/1.0/protect/externalref`,
headers,
body: protectRequest,
json: true,
};
n8n_workflow_1.LoggerProxy.debug(who + 'Making HTTP request', { url: options.url, method: options.method, correlationId });
const response = await this.context.helpers.httpRequest({ ...options, returnFullResponse: true });
n8n_workflow_1.LoggerProxy.debug(who + 'Protection with external ref ID successful', {
fileStorageId: protectRequest.fileStorageId,
secloreFileId: response.body.secloreFileId,
correlationId
});
const protectResponse = {
...response.body,
headers: response.headers
};
return protectResponse;
}
catch (error) {
n8n_workflow_1.LoggerProxy.error(who + 'Protection with external ref ID failed', {
error,
fileStorageId: protectRequest.fileStorageId,
correlationId
});
this.handleHttpError(error);
}
}
async protectWithFileId(protectRequest, accessToken, correlationId) {
const who = "SecloreDRMApiService::protectWithFileId:: ";
try {
n8n_workflow_1.LoggerProxy.debug(who + 'Protecting file with file ID', {
existingProtectedFileId: protectRequest.existingProtectedFileId,
fileStorageId: protectRequest.fileStorageId,
correlationId
});
const headers = {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
};
if (correlationId) {
headers['X-SECLORE-CORRELATION-ID'] = correlationId;
}
const options = {
method: 'POST',
url: `${this.baseUrl}/seclore/drm/1.0/protect/fileid`,
headers,
body: protectRequest,
json: true,
};
n8n_workflow_1.LoggerProxy.debug(who + 'Making HTTP request', { url: options.url, method: options.method, correlationId });
const response = await this.context.helpers.httpRequest({ ...options, returnFullResponse: true });
n8n_workflow_1.LoggerProxy.debug(who + 'Protection with file ID successful', {
existingProtectedFileId: protectRequest.existingProtectedFileId,
secloreFileId: response.body.secloreFileId,
correlationId
});
const protectResponse = {
...response.body,
headers: response.headers
};
return protectResponse;
}
catch (error) {
n8n_workflow_1.LoggerProxy.error(who + 'Protection with file ID failed', {
error,
existingProtectedFileId: protectRequest.existingProtectedFileId,
correlationId
});
this.handleHttpError(error);
}
}
async protectWithHotFolder(protectRequest, accessToken, correlationId) {
const who = "SecloreDRMApiService::protectWithHotFolder:: ";
try {
n8n_workflow_1.LoggerProxy.debug(who + 'Protecting file with hot folder', {
hotfolderId: protectRequest.hotfolderId,
fileStorageId: protectRequest.fileStorageId,
correlationId
});
const headers = {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
};
if (correlationId) {
headers['X-SECLORE-CORRELATION-ID'] = correlationId;
}
const options = {
method: 'POST',
url: `${this.baseUrl}/seclore/drm/1.0/protect/hf`,
headers,
body: protectRequest,
json: true,
};
n8n_workflow_1.LoggerProxy.debug(who + 'Making HTTP request', { url: options.url, method: options.method, correlationId });
const response = await this.context.helpers.httpRequest({ ...options, returnFullResponse: true });
n8n_workflow_1.LoggerProxy.debug(who + 'Protection with hot folder successful', {
hotfolderId: protectRequest.hotfolderId,
secloreFileId: response.body.secloreFileId,
correlationId
});
const protectResponse = {
...response.body,
headers: response.headers
};
return protectResponse;
}
catch (error) {
n8n_workflow_1.LoggerProxy.error(who + 'Protection with hot folder failed', {
error,
hotfolderId: protectRequest.hotfolderId,
fileStorageId: protectRequest.fileStorageId,
correlationId
});
this.handleHttpError(error);
}
}
async unprotect(unprotectRequest, accessToken, correlationId) {
const who = "SecloreDRMApiService::unprotect:: ";
try {
n8n_workflow_1.LoggerProxy.debug(who + 'Unprotecting file', {
fileStorageId: unprotectRequest.fileStorageId,
correlationId
});
const headers = {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
};
if (correlationId) {
headers['X-SECLORE-CORRELATION-ID'] = correlationId;
}
const options = {
method: 'POST',
url: `${this.baseUrl}/seclore/drm/1.0/unprotect`,
headers,
body: unprotectRequest,
json: true,
};
n8n_workflow_1.LoggerProxy.debug(who + 'Making HTTP request', { url: options.url, method: options.method, correlationId });
const response = await this.context.helpers.httpRequest({ ...options, returnFullResponse: true });
n8n_workflow_1.LoggerProxy.debug(who + 'Unprotection successful', {
originalFileStorageId: unprotectRequest.fileStorageId,
unprotectedFileStorageId: response.body.fileStorageId,
correlationId
});
const unprotectResponse = {
...response.body,
headers: response.headers
};
return unprotectResponse;
}
catch (error) {
n8n_workflow_1.LoggerProxy.error(who + 'Unprotection failed', {
error,
fileStorageId: unprotectRequest.fileStorageId,
correlationId
});
this.handleHttpError(error);
}
}
async uploadFile(fileBuffer, fileName, accessToken, correlationId) {
const who = "SecloreDRMApiService::uploadFile:: ";
try {
n8n_workflow_1.LoggerProxy.debug(who + 'Uploading file', {
fileName,
fileSize: fileBuffer.length,
correlationId
});
const headers = {
Authorization: `Bearer ${accessToken}`,
};
if (correlationId) {
headers['X-SECLORE-CORRELATION-ID'] = correlationId;
}
const formData = new FormData();
const file = new Blob([fileBuffer], { type: 'application/octet-stream' });
formData.append('file', file, fileName);
const options = {
method: 'POST',
url: `${this.baseUrl}/seclore/drm/filestorage/1.0/upload`,
headers,
body: formData,
};
n8n_workflow_1.LoggerProxy.debug(who + 'Making HTTP request', { url: options.url, method: options.method, fileName, correlationId });
const response = await this.context.helpers.httpRequest({ ...options, returnFullResponse: true });
n8n_workflow_1.LoggerProxy.debug(who + 'File upload successful', {
fileName,
fileStorageId: response.body.fileStorageId,
correlationId
});
const uploadResponse = {
...response.body,
headers: response.headers
};
return uploadResponse;
}
catch (error) {
n8n_workflow_1.LoggerProxy.error(who + 'File upload failed', {
error,
fileName,
fileSize: fileBuffer.length,
correlationId
});
this.handleHttpError(error);
}
}
async downloadFile(fileStorageId, accessToken, correlationId) {
const who = "SecloreDRMApiService::downloadFile:: ";
try {
n8n_workflow_1.LoggerProxy.debug(who + 'Downloading file', {
fileStorageId,
correlationId
});
const headers = {
Authorization: `Bearer ${accessToken}`,
};
if (correlationId) {
headers['X-SECLORE-CORRELATION-ID'] = correlationId;
}
const options = {
method: 'GET',
url: `${this.baseUrl}/seclore/drm/filestorage/1.0/download/${fileStorageId}`,
headers,
encoding: 'arraybuffer',
};
n8n_workflow_1.LoggerProxy.debug(who + 'Making HTTP request', { url: options.url, method: options.method, fileStorageId, correlationId });
const response = await this.context.helpers.httpRequest({ ...options, returnFullResponse: true });
const fileData = new Uint8Array(response.body);
n8n_workflow_1.LoggerProxy.debug(who + 'File download successful', {
fileStorageId,
fileSize: fileData.length,
correlationId
});
const downloadResponse = {
data: fileData,
headers: response.headers
};
return downloadResponse;
}
catch (error) {
n8n_workflow_1.LoggerProxy.error(who + 'File download failed', {
error,
fileStorageId,
correlationId
});
this.handleHttpError(error);
}
}
async deleteFile(fileStorageId, accessToken, correlationId) {
const who = "SecloreDRMApiService::deleteFile:: ";
try {
n8n_workflow_1.LoggerProxy.debug(who + 'Deleting file', {
fileStorageId,
correlationId
});
const headers = {
Authorization: `Bearer ${accessToken}`,
};
if (correlationId) {
headers['X-SECLORE-CORRELATION-ID'] = correlationId;
}
const options = {
method: 'DELETE',
url: `${this.baseUrl}/seclore/drm/filestorage/1.0/${fileStorageId}`,
headers,
};
n8n_workflow_1.LoggerProxy.debug(who + 'Making HTTP request', { url: options.url, method: options.method, fileStorageId, correlationId });
const response = await this.context.helpers.httpRequest({ ...options, returnFullResponse: true });
n8n_workflow_1.LoggerProxy.debug(who + 'File deletion successful', {
fileStorageId,
correlationId
});
const deleteResponse = {
headers: response.headers
};
return deleteResponse;
}
catch (error) {
n8n_workflow_1.LoggerProxy.error(who + 'File deletion failed', {
error,
fileStorageId,
correlationId
});
this.handleHttpError(error);
}
}
}
exports.SecloreDRMApiService = SecloreDRMApiService;
//# sourceMappingURL=SecloreDRMApiService.js.map