refactor(core): Create controller for binary data (no-changelog) (#7363)

This PR adds a controller for binary data + integration tests.
This commit is contained in:
Iván Ovejero
2023-10-06 16:21:13 +02:00
committed by GitHub
parent 63e11e4be9
commit 34bda535e6
9 changed files with 217 additions and 53 deletions

View File

@@ -26,13 +26,11 @@ import type { RequestOptions } from 'oauth-1.0a';
import clientOAuth1 from 'oauth-1.0a';
import {
BinaryDataService,
Credentials,
LoadMappingOptions,
LoadNodeParameterOptions,
LoadNodeListSearch,
UserSettings,
FileNotFoundError,
} from 'n8n-core';
import type {
@@ -74,7 +72,6 @@ import {
import { credentialsController } from '@/credentials/credentials.controller';
import { oauth2CredentialController } from '@/credentials/oauth2Credential.api';
import type {
BinaryDataRequest,
CurlHelper,
ExecutionRequest,
NodeListSearchRequest,
@@ -99,6 +96,7 @@ import {
WorkflowStatisticsController,
} from '@/controllers';
import { BinaryDataController } from './controllers/binaryData.controller';
import { ExternalSecretsController } from '@/ExternalSecrets/ExternalSecrets.controller.ee';
import { executionsController } from '@/executions/executions.controller';
import { isApiEnabled, loadPublicApiVersions } from '@/PublicApi';
@@ -208,8 +206,6 @@ export class Server extends AbstractServer {
push: Push;
binaryDataService: BinaryDataService;
constructor() {
super('main');
@@ -374,7 +370,6 @@ export class Server extends AbstractServer {
this.endpointPresetCredentials = config.getEnv('credentials.overwrite.endpoint');
this.push = Container.get(Push);
this.binaryDataService = Container.get(BinaryDataService);
await super.start();
LoggerProxy.debug(`Server ID: ${this.uniqueInstanceId}`);
@@ -581,6 +576,7 @@ export class Server extends AbstractServer {
Container.get(ExternalSecretsController),
Container.get(OrchestrationController),
Container.get(WorkflowHistoryController),
Container.get(BinaryDataController),
];
if (isLdapEnabled()) {
@@ -1442,50 +1438,6 @@ export class Server extends AbstractServer {
}),
);
// ----------------------------------------
// Binary data
// ----------------------------------------
// View or download binary file
this.app.get(
`/${this.restEndpoint}/data`,
async (req: BinaryDataRequest, res: express.Response): Promise<void> => {
const { id: binaryDataId, action } = req.query;
let { fileName, mimeType } = req.query;
const [mode] = binaryDataId.split(':') as ['filesystem' | 's3', string];
try {
const binaryPath = this.binaryDataService.getPath(binaryDataId);
if (!fileName || !mimeType) {
try {
const metadata = await this.binaryDataService.getMetadata(binaryDataId);
fileName = metadata.fileName;
mimeType = metadata.mimeType;
res.setHeader('Content-Length', metadata.fileSize);
} catch {}
}
if (mimeType) res.setHeader('Content-Type', mimeType);
if (action === 'download') {
res.setHeader('Content-Disposition', `attachment; filename="${fileName}"`);
}
if (mode === 's3') {
const readStream = await this.binaryDataService.getAsStream(binaryDataId);
readStream.pipe(res);
return;
} else {
res.sendFile(binaryPath);
}
} catch (error) {
if (error instanceof FileNotFoundError) res.writeHead(404).end();
else throw error;
}
},
);
// ----------------------------------------
// Settings
// ----------------------------------------