Skip to content

Commit 4c73868

Browse files
authored
Merge pull request #373 from boostcampwm2023/BE-feature/profile-auth
Multer ๋™์ž‘ ๋ฏธ๋“ค์›จ์–ด๋กœ ์ด๋™
2 parents 754c89b + ae40972 commit 4c73868

File tree

4 files changed

+208
-154
lines changed

4 files changed

+208
-154
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Injectable, NestMiddleware } from '@nestjs/common';
2+
import { Request, Response, NextFunction } from 'express';
3+
import * as multer from 'multer';
4+
5+
export function MulterFileMiddleware(fieldName: string) {
6+
@Injectable()
7+
class MulterSingleMiddleware implements NestMiddleware {
8+
readonly multerInstance = multer();
9+
10+
use(req: Request, res: Response, next: NextFunction) {
11+
this.multerInstance.single(fieldName)(req, res, next);
12+
}
13+
}
14+
15+
return MulterSingleMiddleware;
16+
}

โ€Žnestjs-BE/server/src/spaces/spaces.controller.tsโ€Ž

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ import {
55
Body,
66
Patch,
77
Param,
8-
UseInterceptors,
98
UploadedFile,
109
ValidationPipe,
1110
Header,
1211
HttpStatus,
1312
Delete,
1413
UseGuards,
1514
} from '@nestjs/common';
16-
import { FileInterceptor } from '@nestjs/platform-express';
1715
import { ApiTags, ApiResponse, ApiOperation, ApiQuery } from '@nestjs/swagger';
1816
import { SpacesService } from './spaces.service';
1917
import { CreateSpaceDto } from './dto/create-space.dto';
@@ -28,7 +26,6 @@ export class SpacesController {
2826

2927
@Post()
3028
@UseGuards(MatchUserProfileGuard)
31-
@UseInterceptors(FileInterceptor('icon'))
3229
@ApiOperation({ summary: 'Create space' })
3330
@ApiResponse({
3431
status: HttpStatus.CREATED,
@@ -104,7 +101,6 @@ export class SpacesController {
104101
@Patch(':space_uuid')
105102
@UseGuards(MatchUserProfileGuard)
106103
@UseGuards(IsProfileInSpaceGuard)
107-
@UseInterceptors(FileInterceptor('icon'))
108104
@ApiOperation({ summary: 'Update space by space_uuid' })
109105
@ApiResponse({
110106
status: HttpStatus.OK,
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
1-
import { Module } from '@nestjs/common';
1+
import {
2+
MiddlewareConsumer,
3+
Module,
4+
NestModule,
5+
RequestMethod,
6+
} from '@nestjs/common';
27
import { SpacesService } from './spaces.service';
38
import { SpacesController } from './spaces.controller';
49
import { UploadModule } from '../upload/upload.module';
510
import { ProfileSpaceModule } from '../profile-space/profile-space.module';
611
import { AuthModule } from '../auth/auth.module';
12+
import { MulterFileMiddleware } from '../common/middlewares/multer-file.middleware';
713

814
@Module({
915
imports: [ProfileSpaceModule, UploadModule, AuthModule],
1016
controllers: [SpacesController],
1117
providers: [SpacesService],
1218
exports: [SpacesService],
1319
})
14-
export class SpacesModule {}
20+
export class SpacesModule implements NestModule {
21+
configure(consumer: MiddlewareConsumer) {
22+
consumer
23+
.apply(MulterFileMiddleware('icon'))
24+
.forRoutes(
25+
{ path: '/spaces', method: RequestMethod.POST },
26+
{ path: '/spaces/:space_uuid', method: RequestMethod.PATCH },
27+
);
28+
}
29+
}

0 commit comments

Comments
ย (0)