-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from souravbhowmik1999/fileUpload
PS-2419 Multiple File upload on tenant
- Loading branch information
Showing
9 changed files
with
3,397 additions
and
1,248 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Injectable, BadRequestException, InternalServerErrorException } from '@nestjs/common'; | ||
import { extname } from 'path'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import { HeadObjectCommand, PutObjectCommand, S3Client } from '@aws-sdk/client-s3'; | ||
import { ConfigService } from "@nestjs/config"; | ||
|
||
|
||
@Injectable() | ||
export class FilesUploadService { | ||
private readonly s3Client: S3Client; | ||
private readonly bucketName: string = this.configService.get<string>("AWS_BUCKET_NAME"); | ||
|
||
constructor( | ||
private configService: ConfigService, | ||
) { | ||
this.s3Client = new S3Client({ | ||
region: this.configService.get<string>("AWS_REGION"), | ||
credentials: { | ||
accessKeyId: this.configService.get<string>("AWS_ACCESS_KEY_ID"), | ||
secretAccessKey: this.configService.get<string>("AWS_SECRET_ACCESS_KEY"), | ||
}, | ||
}); | ||
} | ||
|
||
async saveFile(file: Express.Multer.File): Promise<{ filePath: string; fileSize: number }> { | ||
const allowedExtensions: string[] = ['.jpg', '.jpeg', '.png', '.gif', '.ico', '.webp']; | ||
const fileExtension = extname(file.originalname).toLowerCase(); | ||
|
||
if (!allowedExtensions.includes(fileExtension)) { | ||
throw new BadRequestException(`File type ${fileExtension} is not allowed.`); | ||
} | ||
|
||
const uniqueFileName = `${uuidv4()}${fileExtension}`; | ||
const fileUrl = `https://${this.bucketName}.s3.${this.configService.get<string>("AWS_REGION")}.amazonaws.com/${uniqueFileName}`; | ||
|
||
const params = { | ||
Bucket: this.bucketName, | ||
Key: uniqueFileName, | ||
Body: file.buffer, | ||
ContentType: file.mimetype, | ||
}; | ||
|
||
try { | ||
await this.s3Client.send(new PutObjectCommand(params)); | ||
} catch (error) { | ||
throw new InternalServerErrorException(`Failed to upload file to S3. Error: ${error?.message || error}`); | ||
} | ||
|
||
const metadata = await this.s3Client.send(new HeadObjectCommand({ | ||
Bucket: this.bucketName, | ||
Key: uniqueFileName, | ||
})); | ||
|
||
if (!metadata.ContentLength) { | ||
throw new InternalServerErrorException('Failed to upload file to S3.'); | ||
} | ||
|
||
return { | ||
filePath: fileUrl, | ||
fileSize: file.size, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Expose, Type } from "class-transformer"; | ||
import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger"; | ||
import { IsNotEmpty, IsString } from "class-validator"; | ||
|
||
export class TenantUpdateDto { | ||
//tenant name | ||
@ApiPropertyOptional({ | ||
type: String, | ||
description: "Tenant name", | ||
default: "", | ||
}) | ||
@IsString() | ||
@Expose() | ||
name: string; | ||
|
||
//domain | ||
@ApiPropertyOptional({ | ||
type: String, | ||
description: "Domain Name", | ||
default: "", | ||
}) | ||
@IsString() | ||
@Expose() | ||
domain: string; | ||
|
||
//params | ||
@ApiPropertyOptional({ | ||
type: Object, | ||
description: "Params", | ||
default: "", | ||
}) | ||
@Expose() | ||
params: object; | ||
|
||
//file path | ||
@ApiPropertyOptional({ | ||
type: String, | ||
}) | ||
@IsString() | ||
@Expose() | ||
programImages: string[]; | ||
|
||
|
||
@ApiPropertyOptional({ type: String }) | ||
@IsString() | ||
@Expose() | ||
description: string; | ||
|
||
constructor(obj?: Partial<TenantUpdateDto>) { | ||
if (obj) { | ||
Object.assign(this, obj); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { TenantController } from './tenant.controller'; | ||
import { TenantService } from './tenant.service'; | ||
import { Tenants } from 'src/tenant/entities/tenent.entity'; | ||
import { Tenant } from 'src/tenant/entities/tenent.entity'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { FilesUploadService } from 'src/common/services/upload-file'; | ||
|
||
@Module({ | ||
imports: [ | ||
TypeOrmModule.forFeature([Tenants]) | ||
TypeOrmModule.forFeature([Tenant]) | ||
], | ||
controllers: [TenantController], | ||
providers: [TenantService] | ||
providers: [TenantService, FilesUploadService] | ||
}) | ||
export class TenantModule { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters