Skip to content

Commit

Permalink
Merge pull request #83 from boostcampwm2023/feat/67-convert-stream-to…
Browse files Browse the repository at this point in the history
…-buffer

[Feat] 스트림 파일에 대해 StreamableFile 반환 기능 구현
  • Loading branch information
mingxoxo authored Nov 22, 2023
2 parents 5a906f0 + 0bece74 commit 4316dc7
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 14 deletions.
1 change: 1 addition & 0 deletions BE/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 BE/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"dotenv": "^16.3.1",
"jsonwebtoken": "^9.0.2",
"mysql2": "^3.6.3",
"passport": "^0.6.0",
"passport-jwt": "^4.0.1",
Expand Down
2 changes: 1 addition & 1 deletion BE/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import { NoDuplicateLoginGuard } from "./guard/auth.user-guard";
export class AuthController {
constructor(private authService: AuthService) {}

@UseGuards(NoDuplicateLoginGuard)
@Post("/signin")
@UseGuards(NoDuplicateLoginGuard)
signIn(
@Body(ValidationPipe) authCredentialsDto: AuthCredentialsDto,
): Promise<AccessTokenDto> {
Expand Down
20 changes: 17 additions & 3 deletions BE/src/introduce/introduce.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import { Controller, Get } from "@nestjs/common";
import {
Controller,
Get,
Header,
InternalServerErrorException,
StreamableFile,
} from "@nestjs/common";
import { IntroduceService } from "./introduce.service";

@Controller("introduce")
export class IntroduceController {
constructor(private introduceService: IntroduceService) {}

@Get()
async getIntroduceVideo(): Promise<object> {
return this.introduceService.getIntroduceVideo();
@Header("Content-Type", "video/mp4")
async getIntroduceVideo(): Promise<StreamableFile> {
const stream = await this.introduceService.getIntroduceVideo();
try {
return new StreamableFile(stream);
} catch (error) {
throw new InternalServerErrorException(
"파일을 읽어오는 도중 서버에서 문제가 발생했습니다.",
);
}
}
}
5 changes: 3 additions & 2 deletions BE/src/introduce/introduce.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Injectable } from "@nestjs/common";
import { Injectable, StreamableFile } from "@nestjs/common";
import { getFileFromS3 } from "src/utils/e3";
import { Readable } from "stream";

@Injectable()
export class IntroduceService {
async getIntroduceVideo(): Promise<object> {
async getIntroduceVideo(): Promise<Readable> {
return getFileFromS3("test.mp4");
}
}
22 changes: 19 additions & 3 deletions BE/src/shapes/shapes.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { Controller, Get, Param, UseGuards } from "@nestjs/common";
import {
Controller,
Get,
Header,
InternalServerErrorException,
Param,
StreamableFile,
UseGuards,
} from "@nestjs/common";
import { ShapesService } from "./shapes.service";
import { AuthGuard } from "@nestjs/passport";
import { Shape } from "./shapes.entity";
Expand All @@ -16,10 +24,18 @@ export class ShapesController {

@Get("/:uuid")
@UseGuards(AuthGuard())
@Header("Content-Type", "image/png")
async getShapeFilesByUuid(
@Param("uuid") uuid: string,
@GetUser() user: User,
): Promise<object> {
return this.shapesService.getShapeFileByUuid(uuid, user);
): Promise<StreamableFile> {
const stream = await this.shapesService.getShapeFileByUuid(uuid, user);
try {
return new StreamableFile(stream);
} catch (error) {
throw new InternalServerErrorException(
"파일을 읽어오는 도중 서버에서 문제가 발생했습니다.",
);
}
}
}
3 changes: 2 additions & 1 deletion BE/src/shapes/shapes.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getFileFromS3 } from "src/utils/e3";
import { defaultShapes } from "./shapes.default";
import { Shape } from "./shapes.entity";
import { User } from "src/users/users.entity";
import { Readable } from "stream";

@Injectable()
export class ShapesService {
Expand All @@ -18,7 +19,7 @@ export class ShapesService {
return shapeFiles;
}

async getShapeFileByUuid(uuid: string, user: User): Promise<object> {
async getShapeFileByUuid(uuid: string, user: User): Promise<Readable> {
const shape = await this.shapesRepository.getShapeByUuid(uuid);
const { userId, id } = await shape.user;

Expand Down
8 changes: 4 additions & 4 deletions BE/src/utils/e3.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "dotenv/config";
import { S3, Endpoint } from "aws-sdk";
import { ReadStream, createReadStream } from "fs";
import { Readable } from "stream";

const endpoint = new Endpoint("https://kr.object.ncloudstorage.com");
const region = "kr-standard";
Expand All @@ -16,11 +16,11 @@ const s3 = new S3({
},
});

export async function getFileFromS3(fileName: string): Promise<object> {
const readStream = await s3
export function getFileFromS3(filePath: string): Readable {
const readStream = s3
.getObject({
Bucket: "byeolsoop-bucket",
Key: fileName,
Key: filePath,
})
.createReadStream();

Expand Down

0 comments on commit 4316dc7

Please sign in to comment.