|
| 1 | +import { getPresignedUrls, uploadToS3 } from '@/api/image/image.api'; |
| 2 | + |
| 3 | +export const useS3UploadFlow = async ( |
| 4 | + files: File[], |
| 5 | +): Promise<{ fileName: string; url: string }[]> => { |
| 6 | + if (files.length === 0) return []; |
| 7 | + |
| 8 | + const fileNames = files.map((file) => file.name); |
| 9 | + |
| 10 | + const response = await getPresignedUrls(fileNames); |
| 11 | + const presignedInfos = Array.isArray(response) ? response : [response]; |
| 12 | + |
| 13 | + if (!presignedInfos || presignedInfos.length !== files.length) { |
| 14 | + throw new Error('Presigned URL을 일부 가져오지 못했습니다.'); |
| 15 | + } |
| 16 | + |
| 17 | + const uploadResults = await Promise.all( |
| 18 | + files.map(async (file) => { |
| 19 | + const matched = presignedInfos.find((p) => p.fileName === file.name); |
| 20 | + if (!matched) throw new Error(`${file.name}에 대한 presigned URL이 없습니다.`); |
| 21 | + |
| 22 | + await uploadToS3(matched.preSignedUrl, file); |
| 23 | + |
| 24 | + return { |
| 25 | + fileName: file.name, |
| 26 | + url: matched.preSignedUrl, |
| 27 | + }; |
| 28 | + }), |
| 29 | + ); |
| 30 | + |
| 31 | + return uploadResults; |
| 32 | +}; |
| 33 | + |
| 34 | +// import { usePresignedUrlMutation } from '@/hooks/mutations/usePresignedUrlMutation'; |
| 35 | +// import { useUploadToS3Mutation } from '@/hooks/mutations/useUploadToS3Mutation'; |
| 36 | + |
| 37 | +// export const useS3UploadFlow = () => { |
| 38 | +// const { mutateAsync: getUrls } = usePresignedUrlMutation(); |
| 39 | +// const { mutateAsync: upload } = useUploadToS3Mutation(); |
| 40 | + |
| 41 | +// const uploadImagesToS3 = async (files: File[]): Promise<{ fileName: string; url: string }[]> => { |
| 42 | +// if (files.length === 0) return []; |
| 43 | + |
| 44 | +// const fileNames = files.map((file) => file.name); |
| 45 | + |
| 46 | +// const presignedInfos = await getUrls({ fileNames }); |
| 47 | + |
| 48 | +// if (!presignedInfos || presignedInfos.length !== files.length) { |
| 49 | +// throw new Error('Presigned URL을 일부 가져오지 못했습니다.'); |
| 50 | +// } |
| 51 | + |
| 52 | +// // 파일명 기준으로 presignedUrl 매칭 |
| 53 | +// const uploadResults = await Promise.all( |
| 54 | +// files.map(async (file) => { |
| 55 | +// const info = presignedInfos.find((p) => p.fileName === file.name); |
| 56 | +// if (!info) throw new Error(`${file.name}에 대한 presigned URL이 없습니다.`); |
| 57 | + |
| 58 | +// await upload({ url: info.preSignedUrl, file }); |
| 59 | + |
| 60 | +// return { |
| 61 | +// fileName: info.fileName, |
| 62 | +// url: info.preSignedUrl.split('?')[0], // S3 public URL |
| 63 | +// }; |
| 64 | +// }), |
| 65 | +// ); |
| 66 | + |
| 67 | +// return uploadResults; |
| 68 | +// }; |
| 69 | + |
| 70 | +// return { uploadImagesToS3 }; |
| 71 | +// }; |
0 commit comments