Skip to content

Commit

Permalink
✨ feat: 모각밥 모집글 이미지 업로드 기능 (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanggwangseong committed Nov 24, 2024
1 parent c36ecd1 commit 63d1e38
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 4 deletions.
34 changes: 34 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@nestjs/core": "^10.4.8",
"@nestjs/jwt": "^10.2.0",
"@nestjs/platform-express": "^10.4.8",
"@nestjs/serve-static": "^4.0.2",
"@nestjs/swagger": "^8.0.7",
"@nestjs/typeorm": "^10.0.2",
"bcrypt": "^5.1.1",
Expand Down
6 changes: 6 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { MailerModule } from "@nestjs-modules/mailer";
import { ClassSerializerInterceptor, Module } from "@nestjs/common";
import { ConfigModule } from "@nestjs/config";
import { APP_GUARD, APP_INTERCEPTOR } from "@nestjs/core";
import { ServeStaticModule } from "@nestjs/serve-static";
import { TypeOrmModule } from "@nestjs/typeorm";
import { join } from "path";

import { EmailOptions } from "./common/config/email-config";
import { AccessTokenGuard } from "./common/guards/bearer-token.guard";
Expand All @@ -14,6 +16,10 @@ import { ParticipationsModule } from "./modules/participations.module";

@Module({
imports: [
ServeStaticModule.forRoot({
rootPath: join(__dirname, "..", "uploads"),
serveRoot: "/public",
}),
ConfigModule.forRoot({
envFilePath: [`${__dirname}/../.${process.env["NODE_ENV"]}.env`],
isGlobal: true,
Expand Down
11 changes: 11 additions & 0 deletions src/controllers/articles.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import {
Patch,
Post,
Put,
UploadedFile,
UseInterceptors,
} from "@nestjs/common";
import { FileInterceptor } from "@nestjs/platform-express";

import { CurrentMemberDecorator } from "@APP/common/decorators/current-member.decorator";
import { CreateArticleDto } from "@APP/dtos/create-article.dto";
Expand All @@ -34,6 +37,14 @@ export class ArticlesController {
return this.articlesService.createArticle(currentMemberId, body);
}

@Patch("upload-image")
@UseInterceptors(FileInterceptor("image"))
async patchUploadImage(@UploadedFile() file: Express.Multer.File) {
return {
filename: file.filename,
};
}

@Patch(":articleId")
async patchArticle(
@Param("articleId", new ParseIntPipe()) articleId: number,
Expand Down
13 changes: 12 additions & 1 deletion src/entities/article.entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Type } from "class-transformer";
import { Transform, Type } from "class-transformer";
import { IsDate, IsNotEmpty, IsNumber, IsString } from "class-validator";
import {
Column,
Expand Down Expand Up @@ -45,6 +45,17 @@ export class ArticleEntity {
@Column({ type: "datetime" })
endTime!: Date;

@Transform(({ value }) => {
if (!value) return null;

return new URL(
`/public/articles/${value}`,
process.env["API_BASE_URL"],
).toString();
})
@Column({ type: "varchar", length: 2048, nullable: true })
articleImage?: string;

@IsNotEmpty()
@IsNumber()
@Column({ type: "int" })
Expand Down
10 changes: 9 additions & 1 deletion src/entities/member.entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Exclude } from "class-transformer";
import { Exclude, Transform } from "class-transformer";
import { IsEmail, IsNotEmpty, IsString, Length } from "class-validator";
import {
Column,
Expand Down Expand Up @@ -43,6 +43,14 @@ export class MemberEntity {
@Column({ type: "varchar", length: 100, nullable: false, unique: true })
email!: string;

@Transform(({ value }) => {
if (!value) return null;

return new URL(
`/public/members/${value}`,
process.env["API_BASE_URL"],
).toString();
})
@Column({ type: "varchar", length: 2048, nullable: true })
profileImage?: string;

Expand Down
32 changes: 31 additions & 1 deletion src/modules/articles.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Module } from "@nestjs/common";
import { BadRequestException, Module } from "@nestjs/common";
import { MulterModule } from "@nestjs/platform-express";
import { TypeOrmModule } from "@nestjs/typeorm";
import multer from "multer";
import { extname } from "path";

import { ArticlesController } from "@APP/controllers/articles.controller";
import { ArticleLikeEntity } from "@APP/entities/article-like.entity";
Expand All @@ -24,6 +27,33 @@ import { ArticlesService } from "@APP/services/articles.service";
RegionEntity,
ArticleLikeEntity,
]),
MulterModule.register({
limits: {
fileSize: 10000000,
},
fileFilter: (_req, file, cb) => {
const ext = extname(file.originalname);
if (ext !== ".jpg" && ext !== ".jpeg" && ext !== ".png") {
return cb(
new BadRequestException(
"jpg/jpeg/png 파일만 업로드 가능합니다",
),
false,
);
}

return cb(null, true);
},

storage: multer.diskStorage({
destination: function (_req, _file, cb) {
cb(null, "uploads/articles/");
},
filename: function (_req, file, cb) {
cb(null, `${Date.now()}-${file.originalname}`);
},
}),
}),
],
controllers: [ArticlesController],
providers: [
Expand Down
2 changes: 1 addition & 1 deletion src/modules/members.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { MembersService } from "@APP/services/members.service";

storage: multer.diskStorage({
destination: function (_req, _file, cb) {
cb(null, "uploads/");
cb(null, "uploads/members/");
},
filename: function (_req, file, cb) {
cb(null, `${Date.now()}-${file.originalname}`);
Expand Down

0 comments on commit 63d1e38

Please sign in to comment.