Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 30 additions & 25 deletions src/s3/image.uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {Request} from 'express';
import {v4 as uuidv4} from 'uuid'; // 고유한 식별자(UUID) 생성
import path from 'path'; // 확장자 처리
import process from 'process';

import {s3} from './awsS3Client.js';
import {
createMemoFolder,
Expand Down Expand Up @@ -33,7 +32,7 @@ export const imageUploader = multer({
s3: s3, // AWS S3 객체 설정
bucket: process.env.AWS_S3_BUCKET_NAME, // 업로드할 S3 버킷 이름
contentType: multerS3.AUTO_CONTENT_TYPE, // 업로드 파일의 MIME 타입 자동 설정
key: async (req: Request, file, callback) => {
key: (req: Request, file, callback) => {
// S3 버킷에 저장될 경로와 이름 정의
const userId = req.user!.id; // 사용자 ID

Expand All @@ -47,32 +46,38 @@ export const imageUploader = multer({
// 디렉토리 path 설정 과정
let uploadDirectory = null;
if (req.body.folderName) {
const createdMemoFolderId = await createMemoFolder(
bodyToMemoFolder(req.body),
userId,
);
if (createdMemoFolderId === null) {
return callback(
new FolderDuplicateError({folderName: req.body.folderName}),
);
}
uploadDirectory = createdMemoFolderId;
req.uploadDirectory = uploadDirectory; // 디렉토리 정보 저장
Promise.all([createMemoFolder(bodyToMemoFolder(req.body), userId)])
.then(([createdMemoFolderId]) => {
if (createdMemoFolderId === null) {
return callback(
new FolderDuplicateError({folderName: req.body.folderName}),
);
}
uploadDirectory = createdMemoFolderId;
req.uploadDirectory = uploadDirectory; // 디렉토리 정보 저장
callback(
null,
`${userId}/${uploadDirectory}/${uuid}_${file.originalname}`,
); // S3 버킷에서 파일이 저장될 key
})
.catch(err => callback(err));
} else {
const folderId = req.params.folderId;
const checkFolder = await getMemoFolder(BigInt(folderId));
if (checkFolder === null || checkFolder.userId !== userId) {
return callback(
new FolderNotFoundError({folderId: BigInt(folderId)}),
);
}
uploadDirectory = folderId;
Promise.all([getMemoFolder(BigInt(folderId))])
.then(([checkFolder]) => {
if (checkFolder === null || checkFolder.userId !== userId) {
return callback(
new FolderNotFoundError({folderId: BigInt(folderId)}),
);
}
uploadDirectory = folderId;
callback(
null,
`${userId}/${uploadDirectory}/${uuid}_${file.originalname}`,
); // S3 버킷에서 파일이 저장될 key
})
.catch(err => callback(err));
}

callback(
null,
`${userId}/${uploadDirectory}/${uuid}_${file.originalname}`,
); // S3 버킷에서 파일이 저장될 key
},
acl: 'private', // 비공개 설정 (업로드 파일을 버킷 소유자만 접근 가능)
}),
Expand Down