-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1c6a453
commit deac867
Showing
5 changed files
with
154 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import {plogAuthAxios} from "./axios"; | ||
import axios from "axios"; | ||
|
||
export function uploadFile(file: File, | ||
afterUploadCallback: (uploadedURL: string) => void, | ||
uploadFailedCallback: (error: any) => void = (error) => { | ||
console.error(error); | ||
}) { | ||
plogAuthAxios.post('/generate-presigned-url', { | ||
contentType: file.type, | ||
fileName: file.name | ||
}).then((res) => { | ||
const preSignedURL = res.data.preSignedURL; | ||
|
||
// URL을 받은 후에 PUT 요청으로 파일을 업로드 | ||
axios.put(preSignedURL, file, { | ||
headers: { | ||
'Content-Type': file.type | ||
} | ||
}).then((res) => { | ||
afterUploadCallback(preSignedURL.split('?')[0]); | ||
}).catch((err) => { | ||
uploadFailedCallback(err) | ||
}); | ||
}).catch((err) => { | ||
uploadFailedCallback(err) | ||
}); | ||
} | ||
|
||
function getExtensionFromMimeType(mimeType: string): string { | ||
const mapping: { [key: string]: string } = { | ||
'image/jpeg': 'jpg', | ||
'image/png': 'png', | ||
'image/gif': 'gif', | ||
'image/webp': 'webp', | ||
// ... 기타 MIME 타입에 대한 확장자 매핑 ... | ||
}; | ||
|
||
return mapping[mimeType] || ''; | ||
} | ||
|
||
|
||
export function ensureFile(fileOrBlob: File | Blob): File { | ||
const extension = getExtensionFromMimeType(fileOrBlob.type); | ||
const fileName = `image.${extension}`; | ||
|
||
if (fileOrBlob instanceof File) { | ||
// 이미 File 형태인 경우 이름은 그대로 유지하거나 필요한 경우 변경할 수 있습니다. | ||
return fileOrBlob; | ||
} | ||
|
||
// Blob 형태인 경우 File로 변환 | ||
return new File([fileOrBlob], fileName, { | ||
type: fileOrBlob.type, | ||
lastModified: new Date().getTime() | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters