Skip to content

Commit

Permalink
Merge pull request #15 from zzuda/feature/FileUpload-Swagger
Browse files Browse the repository at this point in the history
파일 업로드 기능 Swagger 설명 추가
  • Loading branch information
SkyLightQP authored Jun 5, 2021
2 parents a6dd122 + 4d203e1 commit 7dd276c
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 46 deletions.
13 changes: 13 additions & 0 deletions src/file/dto/file-body.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { IsNumberString, IsOptional, IsString } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';

export class FileBodyDTO {
@IsNumberString()
@ApiProperty({ type: Number, description: '방 ID' })
roomId!: number;

@IsString()
@IsOptional()
@ApiProperty({ type: String, description: '파일 이름', required: false })
fileName?: string;
}
10 changes: 0 additions & 10 deletions src/file/dto/upload-file-body.dto.ts

This file was deleted.

90 changes: 62 additions & 28 deletions src/file/file.controller.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,74 @@
import { Controller, Post, UploadedFiles, UseInterceptors, Body } from '@nestjs/common';
import { FilesInterceptor } from '@nestjs/platform-express';
import { Express } from 'express';
/* eslint-disable max-classes-per-file */
import {Controller, Post, UploadedFiles, UseInterceptors, Body} from '@nestjs/common';
import {
ApiTags,
ApiOperation,
ApiBody,
PickType,
ApiBadRequestResponse,
ApiInternalServerErrorResponse,
ApiNotFoundResponse
} from '@nestjs/swagger'
import {FilesInterceptor} from '@nestjs/platform-express';
import {Express} from 'express';

import { FileService } from './file.service';
import { FileBodyDTO } from './dto/upload-file-body.dto';
import {FileService} from './file.service';
import {FileBodyDTO} from './dto/file-body.dto';

export class FileUploadDTO extends PickType(FileBodyDTO, ['roomId']) {}

export class FileStorageDTO extends PickType(FileBodyDTO, ['roomId']) {}

@Controller('file')
@ApiTags("file")
export class FileController {
constructor(private readonly fileService: FileService) {}
constructor(private readonly fileService : FileService) {}

@Post('upload')
@UseInterceptors(
FilesInterceptor('files', 20, {
dest: './fileStorage/temp'
@Post('upload')
@ApiOperation({
summary: '파일 업로드 API',
description: '- 파일을 업로드 합니다 \n - 한 파일당 최대 크기는 100MB \n - 한번에 최대 20개 까지 업로드 할 수 있습니다 \n - 해당 ' +
'방 ID에 해당하는 roomStorage(저장 스토리지)가 존재하지 않으면 자동 생성됩니다.'
})
)
uploadFiles(
@UploadedFiles() files: Express.Multer.File[],
@Body() fileBodyDTO: FileBodyDTO
): string {
const result = this.fileService.moveFile(files, fileBodyDTO);
@ApiBody({type: FileUploadDTO})
@ApiBadRequestResponse()
@ApiInternalServerErrorResponse()
@UseInterceptors(FilesInterceptor('files', 20, {dest: './fileStorage/temp'}))
uploadFiles(
@UploadedFiles()files : Express.Multer.File[],
@Body()fileBodyDTO : FileBodyDTO
): string {
const result = this
.fileService
.moveFile(files, fileBodyDTO);

return result;
}
return result;
}

@Post('delete')
deleteFile(@Body() FileBody: FileBodyDTO): string {
const result = this.fileService.deleteFile(FileBody);
@Post('delete')
@ApiOperation({summary: '파일 삭제', description: '- 해당 roomId에 해당하는 파일을 삭제합니다'})
@ApiBody({type: FileBodyDTO})
@ApiInternalServerErrorResponse()
deleteFile(@Body()FileBody : FileBodyDTO): string {
const result = this
.fileService
.deleteFile(FileBody);

return result;
}
return result;
}

@Post('deleteStorage')
deleteDir(@Body() roomID: FileBodyDTO): string {
const result = this.fileService.removeRoomStorage(roomID);
@Post('deleteStorage')
@ApiOperation(
{summary: '파일 스토리지 삭제', description: '- 해당 roomId에 해당하는 저장공간을 삭제합니다'}
)
@ApiBody({type: FileStorageDTO})
@ApiInternalServerErrorResponse()
@ApiNotFoundResponse()
deleteDir(@Body()roomID : FileBodyDTO): string {
const result = this
.fileService
.removeRoomStorage(roomID);

return result;
}
return result;
}
}
16 changes: 8 additions & 8 deletions src/file/file.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ConfigService } from '@nestjs/config';
import { existsSync, mkdirSync, rename, rmdir } from 'fs';

import { Express } from 'express';
import { FileBodyDTO } from './dto/upload-file-body.dto';
import { FileBodyDTO } from './dto/file-body.dto';
import { FileError } from '../shared/errors/file.error';

@Injectable()
Expand All @@ -17,13 +17,13 @@ export class FileService {

moveFile(files: Express.Multer.File[], fileBody: FileBodyDTO): string {
const filePath = 'fileStorage';
const { roomID } = fileBody;
const { roomId } = fileBody;
const backSlash = '\\';
const temp = 'temp';

const fileStorage = filePath + backSlash;
const tempStorage: string = fileStorage + backSlash + temp;
const roomStorage: string = fileStorage + backSlash + roomID;
const roomStorage: string = fileStorage + backSlash + roomId;

if (!existsSync(fileStorage)) mkdirSync(fileStorage);
if (!existsSync(tempStorage)) mkdirSync(tempStorage);
Expand Down Expand Up @@ -57,24 +57,24 @@ export class FileService {

deleteFile(fileBody: FileBodyDTO): string {
const fileStorage = 'fileStorage';
const { fileName, roomID } = fileBody;
const { fileName, roomId } = fileBody;
const backSlash = '\\';

rmdir(fileStorage + backSlash + roomID + backSlash + fileName, { recursive: true }, (err) => {
rmdir(fileStorage + backSlash + roomId + backSlash + fileName, { recursive: true }, (err) => {
if (err) throw new InternalServerErrorException(FileError.FILE_DELETION_FAILED);
});
return '파일이 삭제되었습니다';
}

removeRoomStorage(fileBody: FileBodyDTO): string {
const fileStorage = 'fileStorage';
const { roomID } = fileBody;
const { roomId } = fileBody;
const backSlash = '\\';

if (!existsSync(fileStorage + backSlash + roomID))
if (!existsSync(fileStorage + backSlash + roomId))
throw new NotFoundException(FileError.FILE_STORAGE_NOT_FOUND);

rmdir(fileStorage + backSlash + roomID, { recursive: true }, (err) => {
rmdir(fileStorage + backSlash + roomId, { recursive: true }, (err) => {
if (err) throw new InternalServerErrorException(FileError.FILE_STORAGE_DELETION_FAILED);
});
return 'file Storage Removed';
Expand Down
12 changes: 12 additions & 0 deletions src/shared/errors/attendance.error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ErrorInfo } from 'src/types';

export const AttendError: ErrorInfo = {
WRONG_ATTENDANCE_TYPE: {
code: 'attend-404',
message: '해당 attendance 타입은 존재하지 않습니다.'
},
ROOM_ALREADY_EXIST: {
code: 'attend-409',
message: '해당 방의 초대 코드가 이미 생성되어 있습니다.'
},
};
6 changes: 6 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@ export interface RoomInteractReturn {
readonly id: string;
readonly roomInfo: Room;
}

export interface CreatedWordReturn{
readonly message: string;
readonly count: number;
readonly words: string[];
}

0 comments on commit 7dd276c

Please sign in to comment.