-
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #62 from bakaqc/feature/handle-upload-image_chuong
Feature/handle upload image
- Loading branch information
Showing
13 changed files
with
223 additions
and
7 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { ConfigService } from '@nestjs/config'; | ||
import { v2 as cloudinary } from 'cloudinary'; | ||
|
||
export const configureCloudinary = (configService: ConfigService) => { | ||
cloudinary.config({ | ||
cloud_name: configService.get<string>('CLOUDINARY_CLOUD_NAME'), | ||
api_key: configService.get<string>('CLOUDINARY_API_KEY'), | ||
api_secret: configService.get<string>('CLOUDINARY_API_SECRET'), | ||
}); | ||
}; | ||
|
||
export { cloudinary }; |
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,15 @@ | ||
import * as multer from 'multer'; | ||
|
||
export const multerOptions = { | ||
storage: multer.memoryStorage(), | ||
limits: { | ||
fileSize: 5 * 1024 * 1024, | ||
}, | ||
fileFilter: (req, file, cb) => { | ||
if (!file.mimetype.match(/\/(jpg|jpeg|png|gif)$/)) { | ||
cb(new Error('Only image files are allowed!'), false); | ||
} else { | ||
cb(null, true); | ||
} | ||
}, | ||
}; |
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,61 @@ | ||
import { | ||
BadRequestException, | ||
Controller, | ||
Post, | ||
UploadedFiles, | ||
UseInterceptors, | ||
} from '@nestjs/common'; | ||
import { FilesInterceptor } from '@nestjs/platform-express'; | ||
import { ApiBearerAuth, ApiBody, ApiConsumes, ApiTags } from '@nestjs/swagger'; | ||
|
||
import { multerOptions } from '@/domains/upload/config/multer.config'; | ||
import { UploadService } from '@/domains/upload/upload.service'; | ||
|
||
@ApiTags('Upload') | ||
@ApiBearerAuth() | ||
@Controller('upload') | ||
export class UploadController { | ||
constructor(private readonly uploadService: UploadService) {} | ||
|
||
@Post() | ||
@ApiConsumes('multipart/form-data') | ||
@ApiBody({ | ||
description: 'File upload (only image files are allowed)', | ||
schema: { | ||
type: 'object', | ||
properties: { | ||
files: { | ||
type: 'array', | ||
items: { | ||
type: 'string', | ||
format: 'binary', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
@UseInterceptors(FilesInterceptor('files', 20, multerOptions)) | ||
async uploadFiles(@UploadedFiles() files: Express.Multer.File[]) { | ||
if (!files || files.length === 0) { | ||
throw new BadRequestException('At least one file is required'); | ||
} | ||
|
||
try { | ||
const uploadResults = await Promise.all( | ||
files.map((file) => this.uploadService.uploadImage(file)), | ||
); | ||
|
||
return { | ||
message: 'Files uploaded successfully', | ||
uploads: uploadResults.map((result) => ({ | ||
url: result.secure_url, | ||
public_id: result.public_id, | ||
})), | ||
}; | ||
} catch (error) { | ||
throw new BadRequestException( | ||
`Failed to upload files: ${error.message || 'Unknown error'}`, | ||
); | ||
} | ||
} | ||
} |
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,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { UploadController } from '@/domains/upload/upload.controller'; | ||
import { UploadService } from '@/domains/upload/upload.service'; | ||
|
||
@Module({ | ||
controllers: [UploadController], | ||
providers: [UploadService], | ||
exports: [UploadService], | ||
}) | ||
export class UploadModule {} |
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,38 @@ | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
|
||
import { cloudinary } from '@/domains/upload/config/cloudinary.config'; | ||
|
||
@Injectable() | ||
export class UploadService { | ||
private readonly logger = new Logger(UploadService.name); | ||
|
||
async uploadImage(file: Express.Multer.File): Promise<any> { | ||
try { | ||
const result = await new Promise((resolve, reject) => { | ||
const uploadStream = cloudinary.uploader.upload_stream( | ||
{ folder: process.env.CLOUDINARY_FOLDER_STORAGE || 'default_folder' }, | ||
(error, result) => { | ||
if (error) { | ||
const err = new Error( | ||
`Error uploading image to Cloudinary: ${error.message}`, | ||
); | ||
this.logger.error(err.message, error.stack); | ||
reject(err); | ||
} else { | ||
this.logger.log('Image uploaded to Cloudinary successfully'); | ||
resolve(result); | ||
} | ||
}, | ||
); | ||
uploadStream.end(file.buffer); | ||
}); | ||
return result; | ||
} catch (error) { | ||
this.logger.error( | ||
`Error uploading image to Cloudinary: ${error.message}`, | ||
error.stack, | ||
); | ||
throw error; | ||
} | ||
} | ||
} |
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