Skip to content

Commit

Permalink
Merge pull request #28 from recepkefelii/aws-s3
Browse files Browse the repository at this point in the history
Aws s3
  • Loading branch information
recepkefelii authored Mar 17, 2023
2 parents da25b2a + e0ba1b3 commit d30e61f
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 37 deletions.
12 changes: 9 additions & 3 deletions src/post/create-post/create.post.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Body, Controller, Post, UseGuards } from "@nestjs/common";
import { Body, Controller, Post, UploadedFile, UseGuards, UseInterceptors } from "@nestjs/common";
import { FileInterceptor } from "@nestjs/platform-express";
import { CurrentUser } from "src/common/decorators/auth.decorator";
import { AuthGuard } from "src/common/guards/auth.guard";
import { UserdDto } from "src/users/dto/user.dto";
Expand All @@ -10,7 +11,12 @@ import { CreatePostDto } from "./dto/create.post.dto";
export class CreatePostController {
constructor(private readonly createPostService: CreatePostService) { }
@Post('create')
async createPost(@Body() post: CreatePostDto, @CurrentUser() author: UserdDto) { // değişiklik yapıldı
return this.createPostService.createPost(post, author)
@UseInterceptors(FileInterceptor('file'))
async createPost(
@Body() post: CreatePostDto,
@CurrentUser() author: UserdDto,
@UploadedFile() file: Express.Multer.File,
) {
return this.createPostService.createPost(file, post, author)
}
}
6 changes: 5 additions & 1 deletion src/post/create-post/create.post.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { Module } from "@nestjs/common";
import { JwtService } from "@nestjs/jwt";
import { MongooseModule } from "@nestjs/mongoose";
import { AwsS3Module } from "src/aws-s3/aws-s3.module";
import { Post, PostSchema } from "../schemas/post.schema";
import { CreatePostController } from "./create.post.controller";
import { CreatePostService } from "./create.post.service";

@Module({
imports: [MongooseModule.forFeature([{ name: Post.name, schema: PostSchema }])],
imports: [
MongooseModule.forFeature([{ name: Post.name, schema: PostSchema }]),
AwsS3Module
],
providers: [CreatePostService, JwtService],
controllers: [CreatePostController]
})
Expand Down
22 changes: 17 additions & 5 deletions src/post/create-post/create.post.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,29 @@ import { Post, PostDocument } from "../schemas/post.schema";
import { Model } from "mongoose";
import { UserdDto } from "src/users/dto/user.dto";
import { CreatePostDto } from "./dto/create.post.dto";
import { AwsS3Service } from "src/aws-s3/aws-s3.service";

@Injectable()
export class CreatePostService {
constructor(@InjectModel(Post.name) private postModel: Model<PostDocument>) { }
constructor(
@InjectModel(Post.name) private postModel: Model<PostDocument>,
private readonly awsS3Service: AwsS3Service
) { }

async createPost(post: CreatePostDto, author: UserdDto): Promise<Post> {
async createPost(file: Express.Multer.File, post: CreatePostDto, author: UserdDto): Promise<Post> {
let photo_url = null
if (file) {
const uploadedFile = await this.awsS3Service.uploadFile(file.buffer, file.originalname);
photo_url = uploadedFile.url;
}

const createPost = await this.postModel.create({
content: post.content,
title: post.title,
author: author
})
return createPost
author: author,
photo_url: photo_url
});
return createPost;
}

}
3 changes: 3 additions & 0 deletions src/post/schemas/post.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export class Post {
@Prop({ default: Date.now })
updatedDate: Date;

@Prop()
photo_url: string | null

@Prop({ default: 0 })
likes: number;
}
Expand Down
18 changes: 2 additions & 16 deletions src/users/photo-upload/photo.upload.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,10 @@ import { PhotoUploadService } from "./photo.upload.service";
@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);
},
}),
}),
)
@UseInterceptors(FileInterceptor('file'))
uploadProfilPhoto(@UploadedFile() file: Express.Multer.File, @CurrentUser() user: UserdDto) {
console.log(user);

return this.photoUplaodService.uploadProfilPhoto(file, user)
}
}
2 changes: 2 additions & 0 deletions src/users/photo-upload/photo.upload.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ 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 { AwsS3Module } from "src/aws-s3/aws-s3.module";
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' }),
AwsS3Module
],
controllers: [PhotoUploadController],
providers: [PhotoUploadService, JwtService],
Expand Down
19 changes: 8 additions & 11 deletions src/users/photo-upload/photo.upload.service.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
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 { AwsS3Service } from "src/aws-s3/aws-s3.service";
import { UserdDto } from "../dto/user.dto";

@Injectable()
export class PhotoUploadService {
constructor(@InjectModel(User.name) private userModel: Model<UserDocument>) { }
constructor(
@InjectModel(User.name) private userModel: Model<UserDocument>,
private readonly awsS3Service: AwsS3Service
) { }

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()
const uploadedFile = await this.awsS3Service.uploadFile(file.buffer, file.originalname);
userProfile.profil_photo_url = uploadedFile.url
return await userProfile.save();
}
}
2 changes: 1 addition & 1 deletion src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class UsersController {
async userProfile(@CurrentUser() user: UserdDto) {
return await this.userService.userProfile(user)
}



}

0 comments on commit d30e61f

Please sign in to comment.