Skip to content

Commit

Permalink
add: upload profil photo module
Browse files Browse the repository at this point in the history
  • Loading branch information
recepkefelii committed Mar 16, 2023
1 parent 9f601a4 commit 7a312d4
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ yarn-error.log
/quick-start
/example_dist
/example
/upload

# tests
/test
Expand Down
6 changes: 3 additions & 3 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import { FollowModule } from './follow/follow.module';
MongooseModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
uri: configService.getOrThrow<string>('MONGO_DB_URL'),
uri: configService.getOrThrow<string>("MONGO_DB_URL")
}),
inject: [ConfigService],
}),
inject: [ConfigService]
})
],
controllers: [],
})
Expand Down
4 changes: 4 additions & 0 deletions src/auth/register/register.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@ export class RegisterService {
const saltRounds = 10
const hashedPassword = await bcrypt.hash(password, saltRounds)

const randomNumber = Math.floor(Math.random() * (999 - 100 + 1) + 100);

const user = await this.userModel.create({
name: body.name,
username: body.name + randomNumber,
email: body.email,
password: hashedPassword
})

const payload: IUserPayload = {
name: body.name,
username: body.name + randomNumber,
email: body.email,
id: user.id
}
Expand Down
34 changes: 34 additions & 0 deletions src/users/photo-upload/photo.upload.controller.ts
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)
}
}
20 changes: 20 additions & 0 deletions src/users/photo-upload/photo.upload.module.ts
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 {

}
24 changes: 24 additions & 0 deletions src/users/photo-upload/photo.upload.service.ts
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()
}
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"noImplicitAny": false,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false
"noFallthroughCasesInSwitch": false,

}
}

0 comments on commit 7a312d4

Please sign in to comment.