Skip to content

Add support for multiple files #518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions lib/interceptors/amazon-s3-files.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import {
Type,
NestInterceptor,
Optional,
Inject,
ExecutionContext,
CallHandler,
mixin,
} from '@nestjs/common';
import multer from 'multer';
import { Observable } from 'rxjs';
import { MulterModuleOptions } from '@nestjs/platform-express';
import { MULTER_MODULE_OPTIONS } from '@nestjs/platform-express/multer/files.constants';
import { MulterExtendedOptions } from '../interfaces';
import { AmazonS3Storage, ExtendedOptions, transformException } from '../multer-sharp';
import { S3StorageOptions } from '../multer-sharp/interfaces/s3-storage.interface';
import { MulterField } from '@nestjs/platform-express/multer/interfaces/multer-options.interface';

type MulterInstance = any;

export function AmazonS3FilesInterceptor(
fieldNames: MulterField[],
localOptions?: MulterExtendedOptions,
): Type<NestInterceptor> {
class MixinInterceptor implements NestInterceptor {
protected multer: MulterInstance;
private localOptions: MulterExtendedOptions;
private options: MulterModuleOptions;

constructor(
@Optional()
@Inject(MULTER_MODULE_OPTIONS)
options: MulterModuleOptions = {},
) {
this.localOptions = localOptions;
this.options = options;

this.multer = (multer as any)({
...this.options,
...this.localOptions,
});
}

async intercept(context: ExecutionContext, next: CallHandler): Promise<Observable<any>> {
const ctx = context.switchToHttp();

if (this.localOptions) {
this.multer.storage = this.pickStorageOptions();
}

this.multer.s;

await new Promise<void>((resolve, reject) =>
this.multer.fields(fieldNames)(ctx.getRequest(), ctx.getResponse(), (err: any) => {
if (err) {
const error = transformException(err);
return reject(error);
}
resolve();
}),
);

return next.handle();
}

private pickStorageOptions() {
let storageOptions: S3StorageOptions;
const extendedOptionProperty = Object.keys(this.localOptions)[0];

switch (extendedOptionProperty) {
case ExtendedOptions.CREATE_THUMBNAIL:
storageOptions = {
...this.options.storage.storageOpts,
resize: [this.localOptions[extendedOptionProperty], { suffix: 'original' }],
ignoreAspectRatio: true,
dynamicPath: this.localOptions.dynamicPath,
};
return AmazonS3Storage(storageOptions);
case ExtendedOptions.RESIZE_IMAGE:
storageOptions = {
...this.options.storage.storageOpts,
resize: this.localOptions[extendedOptionProperty],
dynamicPath: this.localOptions.dynamicPath,
};
return AmazonS3Storage(storageOptions);
case ExtendedOptions.RESIZE_IMAGE_MULTIPLE_SIZES:
storageOptions = {
...this.options.storage.storageOpts,
resizeMultiple: this.localOptions[extendedOptionProperty],
ignoreAspectRatio: true,
dynamicPath: this.localOptions.dynamicPath,
};
return AmazonS3Storage(storageOptions);
default:
return AmazonS3Storage({ ...this.options.storage.storageOpts, ...this.localOptions });
}
}
}
const Interceptor = mixin(MixinInterceptor);
return Interceptor as Type<NestInterceptor>;
}
1 change: 1 addition & 0 deletions lib/interceptors/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { AmazonS3FileInterceptor } from './amazon-s3-file.interceptor';
export { AmazonS3FilesInterceptor } from './amazon-s3-files.interceptor';