| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- import { createReadStream } from 'fs';
- import type {
- CleanupResult,
- MediaManagerService as MediaManagerServiceContract,
- StorageAdapter,
- StorageStrategy,
- UploadInput,
- UploadResult,
- } from './types';
- import { validateRelativePath } from './validators/path-validator';
- export class MediaManagerService implements MediaManagerServiceContract {
- constructor(
- private readonly localAdapter: StorageAdapter,
- private readonly s3Adapter: StorageAdapter,
- ) {}
- async upload(input: UploadInput): Promise<UploadResult> {
- const {
- storageStrategy,
- relativePath,
- localStoragePrefix = 'local',
- fileStreams,
- } = input;
- if (relativePath.length !== 1 || fileStreams.length !== 1) {
- return this.buildFailure(storageStrategy, relativePath);
- }
- const pathEntry = relativePath[0];
- try {
- validateRelativePath(pathEntry);
- } catch {
- return this.buildFailure(storageStrategy, relativePath);
- }
- const stream = fileStreams[0];
- if (!stream) {
- return this.buildFailure(storageStrategy, relativePath);
- }
- switch (storageStrategy) {
- case 'LOCAL_ONLY':
- return this.uploadLocal(
- pathEntry,
- localStoragePrefix,
- storageStrategy,
- stream,
- );
- case 'S3_ONLY':
- return this.uploadS3Only(
- pathEntry,
- localStoragePrefix,
- storageStrategy,
- stream,
- );
- case 'S3_AND_LOCAL':
- return this.uploadS3AndLocal(
- pathEntry,
- localStoragePrefix,
- storageStrategy,
- stream,
- );
- default:
- return this.buildFailure(storageStrategy, relativePath);
- }
- }
- async cleanup(
- storageStrategy: StorageStrategy,
- relativePath: string[],
- localStoragePrefix: string = 'local',
- ): Promise<CleanupResult> {
- if (relativePath.length === 0) {
- return { status: 0 };
- }
- for (const pathEntry of relativePath) {
- try {
- validateRelativePath(pathEntry);
- } catch {
- return { status: 0 };
- }
- }
- try {
- switch (storageStrategy) {
- case 'LOCAL_ONLY':
- await this.localAdapter.delete(relativePath, localStoragePrefix);
- return { status: 1 };
- case 'S3_ONLY':
- await this.s3Adapter.delete(relativePath, localStoragePrefix);
- return { status: 1 };
- case 'S3_AND_LOCAL': {
- const localResult = await this.localAdapter
- .delete(relativePath, localStoragePrefix)
- .then(() => true)
- .catch(() => false);
- const s3Result = await this.s3Adapter
- .delete(relativePath, localStoragePrefix)
- .then(() => true)
- .catch(() => false);
- return { status: localResult && s3Result ? 1 : 0 };
- }
- default:
- return { status: 0 };
- }
- } catch {
- return { status: 0 };
- }
- }
- private async uploadLocal(
- relativePath: string,
- localStoragePrefix: string,
- storageStrategy: StorageStrategy,
- stream: NodeJS.ReadableStream,
- ): Promise<UploadResult> {
- try {
- const { savedPath } = await this.localAdapter.put(
- relativePath,
- localStoragePrefix,
- stream,
- );
- return this.buildSuccess(storageStrategy, [relativePath], savedPath);
- } catch {
- return this.buildFailure(storageStrategy, [relativePath]);
- }
- }
- private async uploadS3Only(
- relativePath: string,
- localStoragePrefix: string,
- storageStrategy: StorageStrategy,
- stream: NodeJS.ReadableStream,
- ): Promise<UploadResult> {
- let localResult;
- try {
- localResult = await this.localAdapter.put(
- relativePath,
- localStoragePrefix,
- stream,
- );
- } catch {
- return this.buildFailure(storageStrategy, [relativePath]);
- }
- let s3Stream: ReturnType<typeof createReadStream> | undefined;
- let s3SavedPath = '';
- try {
- s3Stream = createReadStream(localResult.savedPath);
- const s3Result = await this.s3Adapter.put(
- relativePath,
- localStoragePrefix,
- s3Stream,
- );
- s3SavedPath = s3Result.savedPath;
- } catch {
- await this.localAdapter
- .delete([relativePath], localStoragePrefix)
- .catch(() => undefined);
- return this.buildFailure(storageStrategy, [relativePath]);
- } finally {
- if (s3Stream) {
- s3Stream.destroy();
- }
- }
- await this.localAdapter
- .delete([relativePath], localStoragePrefix)
- .catch(() => undefined);
- return this.buildSuccess('S3_ONLY', [relativePath], s3SavedPath);
- }
- private async uploadS3AndLocal(
- relativePath: string,
- localStoragePrefix: string,
- storageStrategy: StorageStrategy,
- stream: NodeJS.ReadableStream,
- ): Promise<UploadResult> {
- let localResult;
- try {
- localResult = await this.localAdapter.put(
- relativePath,
- localStoragePrefix,
- stream,
- );
- } catch {
- return this.buildFailure(storageStrategy, [relativePath]);
- }
- let s3Stream: ReturnType<typeof createReadStream> | undefined;
- try {
- s3Stream = createReadStream(localResult.savedPath);
- await this.s3Adapter.put(relativePath, localStoragePrefix, s3Stream);
- return this.buildSuccess(
- 'S3_AND_LOCAL',
- [relativePath],
- localResult.savedPath,
- );
- } catch {
- return this.buildSuccess(
- 'LOCAL_ONLY',
- [relativePath],
- localResult.savedPath,
- );
- } finally {
- if (s3Stream) {
- s3Stream.destroy();
- }
- }
- }
- private buildFailure(
- storageStrategy: StorageStrategy,
- relativePath: string[],
- ): UploadResult {
- return {
- status: 0,
- storageStrategy,
- relativePath,
- savedPath: '',
- };
- }
- private buildSuccess(
- storageStrategy: StorageStrategy,
- relativePath: string[],
- savedPath: string,
- ): UploadResult {
- return {
- status: 1,
- storageStrategy,
- relativePath,
- savedPath,
- };
- }
- }
|