Skip to content

Commit

Permalink
feat: 이미지 접근 URL 조회 API 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
yangchef1 committed Nov 26, 2024
1 parent 2fc2d49 commit 0ba52ca
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
18 changes: 14 additions & 4 deletions apps/server/src/image/controller/image.controller.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import { Controller, Get, Query } from '@nestjs/common';
import { Body, Controller, Get, Param, Post, Query } from '@nestjs/common';
import { ImageService } from '@/image/service/image.service';
import { ResponseMessage } from '@/common/decorator/response-message.decorator';
import { FileNameRequest } from '../dto/file-name-request.dto';
import { Account } from '@/account/entity/account.entity';
import { AuthUser } from '@/account/decorator/authUser.decorator';
import { PresignedUrlResponse } from '../dto/presigned-url-response.dto';

@Controller('image')
export class ImageController {
constructor(private readonly imageService: ImageService) {}

@Get('presigned-url')
@Post('presigned-url')
@ResponseMessage('Presigned URL이 성공적으로 생성되었습니다.')
async getUploadUrl(@Query('name') name: string) {
return { presignedUrl: await this.imageService.getUploadUrl(name) };
async getUploadUrl(@Body() fileNameRequest: FileNameRequest) {
return await this.imageService.getUploadUrl(fileNameRequest.fileName);
}

@Get('access-url/:key')
@ResponseMessage('이미지의 접근 URL이 성공적으로 조회되었습니다.')
async getPublicUrl(@AuthUser() user: Account, @Param('key') key: string) {
return await this.imageService.getAccessUrl(user.id, key);
}
}
7 changes: 7 additions & 0 deletions apps/server/src/image/dto/access-url-response.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class AccessUrlResponse {
accessUrl: string;

constructor(accessUrl: string) {
this.accessUrl = accessUrl;
}
}
7 changes: 7 additions & 0 deletions apps/server/src/image/dto/file-name-request.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { IsNotEmpty, IsString } from 'class-validator';

export class FileNameRequest {
@IsNotEmpty()
@IsString()
fileName: string;
}
10 changes: 10 additions & 0 deletions apps/server/src/image/dto/presigned-url-response.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export class PresignedUrlResponse {
presignedUrl: string;

key: string;

constructor(presignedUrl: string, key: string) {
this.presignedUrl = presignedUrl;
this.key = key;
}
}
16 changes: 14 additions & 2 deletions apps/server/src/image/service/image.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import { v4 as uuidv4 } from 'uuid';
import * as dotenv from 'dotenv';
import { UserService } from '@/account/user.service';
import { PresignedUrlResponse } from '../dto/presigned-url-response.dto';
import { AccessUrlResponse } from '../dto/access-url-response.dto';

dotenv.config();

@Injectable()
export class ImageService {
private readonly s3Client: S3Client;

private readonly userService: UserService;
private readonly bucketName = process.env.OBJECT_STORAGE_BUCKET_NAME;

constructor() {
Expand All @@ -30,7 +33,16 @@ export class ImageService {
Key: key,
});

return getSignedUrl(this.s3Client, command, { expiresIn: 3600 });
return new PresignedUrlResponse(
await getSignedUrl(this.s3Client, command, { expiresIn: 3600 }),
key
);
}

async getAccessUrl(id: number, key: string) {
const accessUrl = `https://${process.env.OBJECT_STORAGE_BUCKET_NAME}.${process.env.OBJECT_STORAGE_REGION}.ncloudstorage.com/${key}`;
await this.userService.updateProfileImage(id, accessUrl);
return new AccessUrlResponse(accessUrl);
}

private generateKey(name: string) {
Expand Down

0 comments on commit 0ba52ca

Please sign in to comment.