-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[온보딩] step02 썸네일 등록 시 아이폰 이미지 확장자(heic) 업로드 안되는 현상 수정 #497
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
05c3a62
chore: heic 변환 라이브러리 설치
ExceptAnyone e3a24be
feat: heic 확장자 input으로 받도록 추가
ExceptAnyone 58b6d44
refactor: 함수 분리 & 고용량 이미지 이슈 예외처리
ExceptAnyone 5849c64
feat: heic 확장자 업로드 가능하도록 수정
ExceptAnyone 477df7c
Merge branch 'develop' into feat/#496-onboarding-heic
ExceptAnyone 0703198
fix: step02 파일 인풋 대소문자 구분x로 인해 확장자 하나 삭제
ExceptAnyone 482eb1c
chore: 콘솔로그 제거
ExceptAnyone File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -2,72 +2,105 @@ import { toast } from 'react-toastify'; | |
import { usePreviewImageContext } from '../../context/Onboarding/PreviewImageContext'; | ||
import { IMAGE_HEIGHT, MESSAGE } from '../../core/toast-messages'; | ||
import Resizer from 'react-image-file-resizer'; | ||
import heic2any from 'heic2any'; | ||
|
||
const usePreviewImage = () => { | ||
const { previewImageInfo, updatePreviewImageInfo } = usePreviewImageContext(); | ||
|
||
const handleImageUpload = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
const { files } = event.target; | ||
const uploadFalse = () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분을 함수 분리하셨군요! 가독성이 더 좋아졌네요👍 |
||
updatePreviewImageInfo({ | ||
isImageUploaded: false, | ||
file: null, | ||
previewImage: null, | ||
imageName: '', | ||
}); | ||
}; | ||
|
||
const uploadFalse = () => { | ||
updatePreviewImageInfo({ | ||
isImageUploaded: false, | ||
file: null, | ||
previewImage: null, | ||
imageName: '', | ||
}); | ||
}; | ||
const resizedFile = (file: File, previewImage: string) => { | ||
new Promise((resolve) => { | ||
Resizer.imageFileResizer( | ||
file, | ||
480, | ||
480, | ||
'WEBP', | ||
75, | ||
0, | ||
(uri) => { | ||
updatePreviewImageInfo({ | ||
isImageUploaded: true, | ||
imageName: (uri as File).name, | ||
file: uri as File, | ||
previewImage: previewImage, | ||
}); | ||
resolve(uri); | ||
console.log('됐다?'); | ||
}, | ||
'file', | ||
); | ||
}); | ||
}; | ||
|
||
const resizedFile = (file: File, previewImage: string) => { | ||
new Promise((resolve) => { | ||
Resizer.imageFileResizer( | ||
file, | ||
480, | ||
480, | ||
'WEBP', | ||
75, | ||
0, | ||
(uri) => { | ||
updatePreviewImageInfo({ | ||
isImageUploaded: true, | ||
imageName: (uri as File).name, | ||
file: uri as File, | ||
previewImage: previewImage, | ||
}); | ||
resolve(uri); | ||
console.log('됐다?'); | ||
}, | ||
'file', | ||
); | ||
}); | ||
const isFailUploadImageToast = (selectedFile: Blob | MediaSource) => { | ||
const img = new Image(); | ||
|
||
img.onload = function () { | ||
if (img.height <= IMAGE_HEIGHT.MIN) { | ||
// 이미지 너비가 어느 수준 이하일 때 업로드 x | ||
toast(MESSAGE.HEIGHT_SMALL); | ||
uploadFalse(); | ||
} else if (img.height > IMAGE_HEIGHT.MAX) { | ||
toast(MESSAGE.HEIGHT_BIG); | ||
uploadFalse(); | ||
} | ||
}; | ||
|
||
if (files && files.length > 0) { | ||
const selectedFiles = files as FileList; | ||
/**@see 추후 유니크한 이미지 네임 필요할 수 있으니 일단 주석처리 */ | ||
// const imageName = files[0].name.trim(); | ||
|
||
/**@todo 파싱 유틸 함수 공용으로 따로 작성 */ | ||
// const uploadTime = new Date().toISOString(); | ||
|
||
// const uniqueName = `${uploadTime}${imageName}`; | ||
// const finalImageName = uniqueName | ||
// .replace(/\//g, '') // 폴더링 방지를 위해 '/' 제거 | ||
// .replace(/\s/g, ''); // 공백 제거 | ||
resizedFile(selectedFiles[0], URL.createObjectURL(selectedFiles[0])); | ||
|
||
const img = new Image(); | ||
img.onload = function () { | ||
if (img.height <= IMAGE_HEIGHT.MIN) { | ||
// 이미지 너비가 어느 수준 이하일 때 업로드 x | ||
toast(MESSAGE.HEIGHT_SMALL); | ||
uploadFalse(); | ||
} else if (img.height > IMAGE_HEIGHT.MAX) { | ||
toast(MESSAGE.HEIGHT_BIG); | ||
uploadFalse(); | ||
img.src = URL.createObjectURL(selectedFile); | ||
}; | ||
|
||
const handleImageUpload = async (event: React.ChangeEvent<HTMLInputElement>) => { | ||
const { files } = event.target; | ||
|
||
try { | ||
if (files && files.length > 0) { | ||
const selectedFiles = files as FileList; | ||
|
||
let convertedFile = selectedFiles[0]; | ||
console.log('convertedFile', convertedFile); | ||
|
||
if (selectedFiles[0].type === 'image/heic' || selectedFiles[0].type === 'image/HEIC') { | ||
let blob = selectedFiles[0]; | ||
console.log('blob', blob); | ||
const resultBlob = await heic2any({ blob, toType: 'image/webp' }); | ||
convertedFile = new File( | ||
[resultBlob as Blob], | ||
selectedFiles[0].name.split('.')[0] + '.webp', | ||
{ type: 'image/webp', lastModified: new Date().getTime() }, | ||
); | ||
updatePreviewImageInfo({ | ||
isImageUploaded: true, | ||
imageName: convertedFile.name, | ||
file: convertedFile, | ||
previewImage: URL.createObjectURL(selectedFiles[0]), | ||
}); | ||
} | ||
}; | ||
img.src = URL.createObjectURL(selectedFiles[0]); | ||
|
||
/**@see 추후 유니크한 이미지 네임 필요할 수 있으니 일단 주석처리 */ | ||
// const imageName = files[0].name.trim(); | ||
|
||
/**@todo 파싱 유틸 함수 공용으로 따로 작성 */ | ||
// const uploadTime = new Date().toISOString(); | ||
|
||
// const uniqueName = `${uploadTime}${imageName}`; | ||
// const finalImageName = uniqueName | ||
// .replace(/\//g, '') // 폴더링 방지를 위해 '/' 제거 | ||
// .replace(/\s/g, ''); // 공백 제거 | ||
|
||
isFailUploadImageToast(selectedFiles[0]); | ||
|
||
resizedFile(selectedFiles[0], URL.createObjectURL(selectedFiles[0])); | ||
} | ||
} catch (err) { | ||
console.log('error:', err); | ||
} | ||
}; | ||
|
||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
앗 이거 저도 수정해야 하는데, MIME type은 대소문자 구분을 하지 않고 주로 소문자로 쓰인다고 해서 image/heic만 유지해도 될 것 같아요!
MIME 타입