1- import type {
2- IUploadService ,
3- UploadResult ,
4- UploadOptions ,
5- } from './types' ;
6- import {
7- DEFAULT_UPLOAD_CONFIG ,
8- UploadError ,
9- } from './types' ;
1+ import { PutObjectCommand } from '@aws-sdk/client-s3' ;
2+ import type { IUploadService , UploadResult , UploadOptions } from './types' ;
3+ import { DEFAULT_UPLOAD_CONFIG , UploadError } from './types' ;
4+ import { BUCKET_NAME , serverS3Client } from '@/lib/utils/server-s3-client' ;
105
116export class UploadService implements IUploadService {
127 private static instance : UploadService ;
@@ -30,36 +25,66 @@ export class UploadService implements IUploadService {
3025 formData . append ( 'file' , file ) ;
3126
3227 try {
33- const response = await fetch ( '/api/upload' , {
34- method : 'POST' ,
35- body : formData ,
28+ // const response = await fetch('/api/upload', {
29+ // method: 'POST',
30+ // body: formData,
31+ // });
32+ const file = formData . get ( 'file' ) as File ;
33+ const timestamp = Date . now ( ) ;
34+ const randomString = Math . random ( ) . toString ( 36 ) . substring ( 2 , 15 ) ;
35+ const fileExtension = file . name . split ( '.' ) . pop ( ) ?. toLowerCase ( ) ;
36+ const fileType = file . type . startsWith ( 'image/' ) ? 'images' : 'videos' ;
37+ const key = `uploads/${ fileType } /${ timestamp } -${ randomString } .${ fileExtension } ` ;
38+ const buffer = Buffer . from ( await file . arrayBuffer ( ) ) ;
39+
40+ // S3에 업로드
41+ const command = new PutObjectCommand ( {
42+ Bucket : BUCKET_NAME ,
43+ Key : key ,
44+ Body : buffer ,
45+ ContentType : file . type ,
46+ Metadata : {
47+ originalName : Buffer . from ( file . name , 'utf-8' ) . toString ( 'base64' ) ,
48+ uploadedAt : new Date ( ) . toISOString ( ) ,
49+ } ,
3650 } ) ;
51+ await serverS3Client . send ( command ) ;
3752
38- if ( ! response . ok ) {
39- const errorData = await response . json ( ) as { error ?: string } ;
40- throw new UploadError (
41- errorData . error ?? '업로드에 실패했습니다.' ,
42- 'UPLOAD_FAILED' ,
43- file . name ,
44- ) ;
45- }
53+ // public URL 생성
54+ const publicUrl = `https://${ BUCKET_NAME } .s3.${ process . env . AWS_REGION } .amazonaws.com/${ key } ` ;
4655
47- const result = await response . json ( ) as {
48- url : string ;
49- fileName : string ;
50- fileSize : number ;
51- fileType : string ;
52- metadata ?: Record < string , unknown > ;
56+ const result = {
57+ success : true ,
58+ url : publicUrl ,
59+ fileName : file . name ,
60+ fileSize : file . size ,
61+ fileType : file . type ,
5362 } ;
5463
64+ // if (!response.ok) {
65+ // const errorData = await response.json() as { error?: string };
66+ // throw new UploadError(
67+ // errorData.error ?? '업로드에 실패했습니다.',
68+ // 'UPLOAD_FAILED',
69+ // file.name,
70+ // );
71+ // }
72+
73+ // const result = await response.json() as {
74+ // url: string;
75+ // fileName: string;
76+ // fileSize: number;
77+ // fileType: string;
78+ // metadata?: Record<string, unknown>;
79+ // };
80+
5581 return {
5682 success : true ,
5783 url : result . url ,
5884 fileName : result . fileName ,
5985 fileSize : result . fileSize ,
6086 fileType : result . fileType ,
6187 uploadTime : Date . now ( ) ,
62- metadata : result . metadata ?? { } ,
6388 } ;
6489 } catch ( error ) {
6590 if ( error instanceof UploadError ) {
0 commit comments