|
| 1 | +import type { ApiResponse } from '@/shared/api/types' |
| 2 | +import { axiosInstance } from '@/shared/api' |
| 3 | +import type { |
| 4 | + ProfileImageUploadUrlRequest, |
| 5 | + ProfileImageUploadUrlResponse, |
| 6 | + ProfileImageUpdateRequest, |
| 7 | + MyProfileDto, |
| 8 | +} from '../types' |
| 9 | + |
| 10 | +export async function postProfileImageUploadUrl( |
| 11 | + payload: ProfileImageUploadUrlRequest, |
| 12 | +): Promise<ProfileImageUploadUrlResponse> { |
| 13 | + const response = await axiosInstance.post< |
| 14 | + ApiResponse<ProfileImageUploadUrlResponse> |
| 15 | + >('/user/profile-image/upload-url', payload) |
| 16 | + return response.data.responseDto |
| 17 | +} |
| 18 | + |
| 19 | +export async function uploadFileToS3( |
| 20 | + uploadUrl: string, |
| 21 | + file: File, |
| 22 | +): Promise<void> { |
| 23 | + const controller = new AbortController() |
| 24 | + const timeout = setTimeout(() => controller.abort(), 30000) |
| 25 | + try { |
| 26 | + const response = await fetch(uploadUrl, { |
| 27 | + method: 'PUT', |
| 28 | + body: file, |
| 29 | + headers: { |
| 30 | + 'Content-Type': file.type, |
| 31 | + 'Cache-Control': 'public, max-age=31536000, immutable', |
| 32 | + }, |
| 33 | + signal: controller.signal, |
| 34 | + }) |
| 35 | + if (!response.ok) { |
| 36 | + throw new Error(`S3 업로드 실패: ${response.status}`) |
| 37 | + } |
| 38 | + } catch (error) { |
| 39 | + if (error instanceof DOMException && error.name === 'AbortError') { |
| 40 | + throw new Error('S3 업로드 타임아웃: 30초 초과') |
| 41 | + } |
| 42 | + throw error |
| 43 | + } finally { |
| 44 | + clearTimeout(timeout) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +export async function putProfileImage( |
| 49 | + payload: ProfileImageUpdateRequest, |
| 50 | +): Promise<MyProfileDto> { |
| 51 | + const response = await axiosInstance.put<ApiResponse<MyProfileDto>>( |
| 52 | + '/user/profile-image', |
| 53 | + payload, |
| 54 | + ) |
| 55 | + return response.data.responseDto |
| 56 | +} |
0 commit comments