@nestjs/platform-express#FilesInterceptor TypeScript Examples

The following examples show how to use @nestjs/platform-express#FilesInterceptor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: gcloud-storage-files.interceptor.ts    From nestjs-gcloud-storage with MIT License 6 votes vote down vote up
export function GCloudStorageFilesInterceptor(
  fieldName: string,
  localOptions?: MulterOptions,
  gcloudStorageOptions?: Partial<GCloudStoragePerRequestOptions>,
): Type<NestInterceptor> {
  @Injectable()
  class MixinInterceptor implements NestInterceptor {
    public interceptor: NestInterceptor;

    constructor(private readonly gcloudStorage: GCloudStorageService) {
      this.interceptor = new (FilesInterceptor(fieldName, 20, localOptions))();
    }

    async intercept(context: ExecutionContext, next: CallHandler): Promise<Observable<any>> {
      (await this.interceptor.intercept(context, next)) as Observable<any>;

      const request = context.switchToHttp().getRequest();

      const files = request[fieldName];

      if (!files.length) {
        Logger.error(
          'GCloudStorageFilesInterceptor',
          `Can not intercept field "${fieldName}". Did you specify the correct field name in @GCloudStorageFilesInterceptor('${fieldName}')?`,
        );
        return;
      }

      for (const file of files) file.storageUrl = await this.gcloudStorage.upload(file, gcloudStorageOptions);

      return next.handle();
    }
  }

  const Interceptor = mixin(MixinInterceptor);
  return Interceptor as Type<NestInterceptor>;
}
Example #2
Source File: time-series.controller.ts    From aqualink-app with MIT License 6 votes vote down vote up
@ApiOperation({ summary: 'Upload time series data' })
  @UseGuards(IsSiteAdminGuard)
  @Auth(AdminLevel.SiteManager, AdminLevel.SuperAdmin)
  @Post('sites/:siteId/site-survey-points/:surveyPointId/upload')
  @UseInterceptors(
    FilesInterceptor('files', MAX_FILE_COUNT, {
      dest: './upload',
      fileFilter,
      limits: {
        fileSize: MAX_FILE_SIZE_MB * 10 ** 6,
      },
    }),
  )
  uploadTimeSeriesData(
    @Param() surveyPointDataRangeDto: SurveyPointDataRangeDto,
    @UploadedFiles() files: Express.Multer.File[],
    @Body('sensor') sensor: SourceType,
    @Query('failOnWarning', ParseBoolPipe) failOnWarning?: boolean,
  ) {
    return this.timeSeriesService.uploadData(
      surveyPointDataRangeDto,
      sensor,
      files,
      failOnWarning,
    );
  }
Example #3
Source File: images.controller.ts    From Phantom with MIT License 6 votes vote down vote up
@Post('/me/uploadImage')
  @ApiConsumes('multipart/form-data')
  @UseInterceptors(FilesInterceptor('file'))
  async uploadImage(@UploadedFiles() files, @Request() req) {
    return await this.ImagesService.uploadFile(
      files[0].originalname,
      files[0].mimetype,
      files[0].buffer,
    );
  }