📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
added validation of uploaded files
Yevhenii Kulisidi7b0b548284eb
diff --git a/package.json b/package.jsonindex 25ff209d5..f290c4d46 100644--- a/package.json+++ b/package.json@@ -56,6 +56,7 @@"jsonwebtoken": "^9.0.0","lodash": "^4.17.21","mysql2": "^2.3.3",+ "nestjs-form-data": "^1.8.7","nestjs-i18n": "^10.2.4","nestjs-telegraf": "^2.6.3","newrelic": "^9.7.4",diff --git a/src/app.module.ts b/src/app.module.tsindex be29adf4c..c9584e9f1 100644--- a/src/app.module.ts+++ b/src/app.module.ts@@ -19,6 +19,7 @@ import { AlertModule } from './alert/alert.module'import { I18nModule } from 'nestjs-i18n'import { getI18nConfig } from './configs'import { AuthModule } from './auth/auth.module'+import { NestjsFormDataModule } from 'nestjs-form-data'const modules = [ConfigModule.forRoot({ envFilePath: '.env', isGlobal: true }),@@ -34,6 +35,7 @@ const modules = [}),I18nModule.forRootAsync(getI18nConfig()),ScheduleModule.forRoot(),+ NestjsFormDataModule.config({ isGlobal: true }),TaskManagerModule,UserModule,MailerModule,diff --git a/src/marketplace/cdn/cdn.service.ts b/src/marketplace/cdn/cdn.service.tsindex 15a85d33c..25b31e97a 100644--- a/src/marketplace/cdn/cdn.service.ts+++ b/src/marketplace/cdn/cdn.service.ts@@ -13,9 +13,14 @@ export class CdnService {private readonly configService: ConfigService,) {}- async uploadFile(file: Express.Multer.File): Promise<{ filename: string }> {+ /**+ * To clarify, in this case the file parameter type was set to "any" because+ * Multer from Express returns the "originalname" property,+ * whereas the nestjs-form-data package uses the "originalName" property.+ */+ async uploadFile(file: any): Promise<{ filename: string }> {try {- const filePath = `${tmpdir()}/${file.originalname}`+ const filePath = `${tmpdir()}/${file.originalName}`await writeFile(filePath, file.buffer)const form = new FormData()diff --git a/src/marketplace/extensions/dtos/create-extension.dto.ts b/src/marketplace/extensions/dtos/create-extension.dto.tsindex ea67f80cb..7f4d86a80 100644--- a/src/marketplace/extensions/dtos/create-extension.dto.ts+++ b/src/marketplace/extensions/dtos/create-extension.dto.ts@@ -5,6 +5,7 @@ import {IsString,Length,} from 'class-validator'+import { HasMimeType, IsFile, IsFiles, MaxFileSize } from 'nestjs-form-data'export class CreateExtensionBodyDto {@IsNotEmpty()@@ -20,4 +21,22 @@ export class CreateExtensionBodyDto {@IsOptional()@IsNumberString()readonly price?: string++ @IsOptional()+ @IsFile()+ @MaxFileSize(10 * 1024 * 1024)+ @HasMimeType(['image/jpeg', 'image/png'])+ readonly mainImage?: Express.Multer.File++ @IsOptional()+ @IsFiles()+ @MaxFileSize(10 * 1024 * 1024, { each: true })+ @HasMimeType(['image/jpeg', 'image/png'], { each: true })+ readonly additionalImages?: Express.Multer.File[]++ @IsOptional()+ @IsFile()+ @MaxFileSize(10 * 1024 * 1024)+ @HasMimeType(['text/javascript', 'application/javascript'])+ readonly extensionScript?: Express.Multer.File}diff --git a/src/marketplace/extensions/extensions.controller.ts b/src/marketplace/extensions/extensions.controller.tsindex d6246ffc6..3520ef9c0 100644--- a/src/marketplace/extensions/extensions.controller.ts+++ b/src/marketplace/extensions/extensions.controller.ts@@ -3,15 +3,12 @@ import {ConflictException,Controller,Delete,- FileTypeValidator,ForbiddenException,Get,InternalServerErrorException,Logger,- MaxFileSizeValidator,NotFoundException,Param,- ParseFilePipe,Patch,Post,Query,@@ -54,7 +51,7 @@ import { ExtensionStatus } from './enums/extension-status.enum'import { JwtAccessTokenGuard } from 'src/auth/guards'import { Auth } from 'src/auth/decorators'import { CreateExtensionBodyDto } from './dtos'-import { CreateExtensionFilesType } from './types'+import { FormDataRequest } from 'nestjs-form-data'@ApiTags('extensions')@UsePipes(@@ -416,45 +413,14 @@ export class ExtensionsController {@Post()@Auth([UserType.CUSTOMER, UserType.ADMIN])- @UseInterceptors(- FileFieldsInterceptor([- { name: 'mainImage', maxCount: 1 },- { name: 'additionalImages', maxCount: 5 },- { name: 'extensionScript', maxCount: 1 },- ]),- )+ @FormDataRequest()async createExtension(@Body() body: CreateExtensionBodyDto,- @UploadedFiles(- new ParseFilePipe({- fileIsRequired: false,- validators: [- new MaxFileSizeValidator({ maxSize: 10 * 1024 * 1024 }),- new FileTypeValidator({ fileType: /^image\/(jpg|jpeg|png|gif)$/ }),- ],- }),- new ParseFilePipe({- fileIsRequired: false,- validators: [- new MaxFileSizeValidator({ maxSize: 10 * 1024 * 1024 }),- new FileTypeValidator({ fileType: /^image\/(jpg|jpeg|png|gif)$/ }),- ],- }),- new ParseFilePipe({- fileIsRequired: false,- validators: [- new MaxFileSizeValidator({ maxSize: 10 * 1024 * 1024 }),- new FileTypeValidator({ fileType: /^text\/javascript$/ }),- ],- }),- )- files: CreateExtensionFilesType,@CurrentUserId() userId: string,): Promise<Extension> {return await this.extensionsService.createExtension({ownerId: userId,...body,- files,})}diff --git a/src/marketplace/extensions/extensions.service.ts b/src/marketplace/extensions/extensions.service.tsindex 708d31117..c9b552552 100644--- a/src/marketplace/extensions/extensions.service.ts+++ b/src/marketplace/extensions/extensions.service.ts@@ -174,19 +174,19 @@ export class ExtensionsService {ownerId: extension.ownerId,name: extension.name,description: extension.description,- status: extension.files.extensionScript+ status: extension.extensionScript? ExtensionStatus.PENDING: ExtensionStatus.NO_EXTENSION_UPLOADED,price: extension.price && Number(extension.price),mainImage:- extension.files.mainImage &&+ extension.mainImage &&(- await this.cdnService.uploadFile(extension.files.mainImage[0])+ await this.cdnService.uploadFile(extension.mainImage)).filename,- additionalImages: extension.files.additionalImages+ additionalImages: extension.additionalImages? [...(await Promise.all(- extension.files.additionalImages.map(+ extension.additionalImages.map(async image =>(await this.cdnService.uploadFile(image)@@ -196,9 +196,9 @@ export class ExtensionsService {]: [],fileURL:- extension.files.extensionScript &&+ extension.extensionScript &&(- await this.cdnService.uploadFile(extension.files.extensionScript[0])+ await this.cdnService.uploadFile(extension.extensionScript)).filename,})}diff --git a/src/marketplace/extensions/types/create-extension.type.ts b/src/marketplace/extensions/types/create-extension.type.tsindex 26ee50a52..9ce70e1d4 100644--- a/src/marketplace/extensions/types/create-extension.type.ts+++ b/src/marketplace/extensions/types/create-extension.type.ts@@ -1,13 +1,9 @@-export type CreateExtensionFilesType = {- mainImage?: Express.Multer.File[]- additionalImages?: Express.Multer.File[]- extensionScript?: Express.Multer.File[]-}-export type CreateExtensionType = {ownerId: stringname: stringdescription?: stringprice?: string- files: CreateExtensionFilesType+ mainImage?: Express.Multer.File+ additionalImages?: Express.Multer.File[]+ extensionScript?: Express.Multer.File}