-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9f601a4
commit 7a312d4
Showing
7 changed files
with
88 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,7 @@ yarn-error.log | |
/quick-start | ||
/example_dist | ||
/example | ||
/upload | ||
|
||
# tests | ||
/test | ||
|
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,34 @@ | ||
import { Controller, Post, UploadedFile, UseGuards, UseInterceptors } from "@nestjs/common"; | ||
import { FileInterceptor } from "@nestjs/platform-express"; | ||
import { diskStorage } from "multer"; | ||
import { extname } from "path"; | ||
import { CurrentUser } from "src/common/decorators/auth.decorator"; | ||
import { AuthGuard } from "src/common/guards/auth.guard"; | ||
import { UserdDto } from "../dto/user.dto"; | ||
import { PhotoUploadService } from "./photo.upload.service"; | ||
|
||
@UseGuards(AuthGuard) | ||
@Controller('/upload') | ||
export class PhotoUploadController { | ||
constructor(private readonly photoUplaodService: PhotoUploadService) { } | ||
@Post('/profil-photo') | ||
@UseInterceptors( | ||
FileInterceptor('file', { | ||
storage: diskStorage({ | ||
destination: './upload', | ||
filename: (req, file, callback) => { | ||
const uniqueSuffix = | ||
Date.now() + '-' + Math.round(Math.random() * 1e9); | ||
const ext = extname(file.originalname); | ||
const filename = `${uniqueSuffix}${ext}`; | ||
callback(null, filename); | ||
}, | ||
}), | ||
}), | ||
) | ||
uploadProfilPhoto(@UploadedFile() file: Express.Multer.File, @CurrentUser() user: UserdDto) { | ||
console.log(user); | ||
|
||
return this.photoUplaodService.uploadProfilPhoto(file, user) | ||
} | ||
} |
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,20 @@ | ||
import { Module } from "@nestjs/common"; | ||
import { JwtService } from "@nestjs/jwt"; | ||
import { MongooseModule } from "@nestjs/mongoose"; | ||
import { MulterModule } from "@nestjs/platform-express"; | ||
import { User, UserSchema } from "src/auth/schema/user.schema"; | ||
import { PhotoUploadController } from "./photo.upload.controller"; | ||
import { PhotoUploadService } from "./photo.upload.service"; | ||
|
||
@Module({ | ||
imports: [ | ||
MongooseModule.forFeature([{ name: User.name, schema: UserSchema }]), | ||
MulterModule.register({ dest: './upload' }), | ||
], | ||
controllers: [PhotoUploadController], | ||
providers: [PhotoUploadService, JwtService], | ||
exports: [PhotoUploadService] | ||
}) | ||
export class PhotoUploadModule { | ||
|
||
} |
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,24 @@ | ||
import { Injectable } from "@nestjs/common"; | ||
import { InjectModel } from "@nestjs/mongoose"; | ||
import { Model } from "mongoose"; | ||
import { extname } from "path"; | ||
import { User, UserDocument } from "src/auth/schema/user.schema"; | ||
import { UserdDto } from "../dto/user.dto"; | ||
|
||
@Injectable() | ||
export class PhotoUploadService { | ||
constructor(@InjectModel(User.name) private userModel: Model<UserDocument>) { } | ||
|
||
async uploadProfilPhoto(file: Express.Multer.File, user: UserdDto) { | ||
const userProfile = await this.userModel.findByIdAndUpdate(user.id).select("-password") | ||
console.log(userProfile); | ||
|
||
const uniqueSuffix = | ||
Date.now() + '-' + Math.round(Math.random() * 1e9); | ||
const ext = extname(file.originalname); | ||
const filename = `${uniqueSuffix}${ext}`; | ||
userProfile.profil_photo_url = filename | ||
|
||
return await userProfile.save() | ||
} | ||
} |
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