| import { heicTo, isHeic } from 'heic-to'; |
|
|
| |
| |
| |
| |
| |
| export const isHEICFile = async (file: File): Promise<boolean> => { |
| try { |
| return await isHeic(file); |
| } catch (error) { |
| console.warn('Error checking if file is HEIC:', error); |
| |
| return file.type === 'image/heic' || file.type === 'image/heif'; |
| } |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| export const convertHEICToJPEG = async ( |
| file: File, |
| quality: number = 0.9, |
| onProgress?: (progress: number) => void, |
| ): Promise<File> => { |
| try { |
| |
| onProgress?.(0.3); |
|
|
| const convertedBlob = await heicTo({ |
| blob: file, |
| type: 'image/jpeg', |
| quality, |
| }); |
|
|
| |
| onProgress?.(0.8); |
|
|
| |
| const convertedFile = new File([convertedBlob], file.name.replace(/\.(heic|heif)$/i, '.jpg'), { |
| type: 'image/jpeg', |
| lastModified: file.lastModified, |
| }); |
|
|
| |
| onProgress?.(1.0); |
|
|
| return convertedFile; |
| } catch (error) { |
| console.error('Error converting HEIC to JPEG:', error); |
| throw new Error('Failed to convert HEIC image to JPEG'); |
| } |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| export const processFileForUpload = async ( |
| file: File, |
| quality: number = 0.9, |
| onProgress?: (progress: number) => void, |
| ): Promise<File> => { |
| const isHEIC = await isHEICFile(file); |
|
|
| if (isHEIC) { |
| console.log('HEIC file detected, converting to JPEG...'); |
| return convertHEICToJPEG(file, quality, onProgress); |
| } |
|
|
| return file; |
| }; |
|
|