Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
"expo-image-manipulator": "^13.0.6",
"fast-equals": "^4.0.3",
"focus-trap-react": "^11.0.3",
"heic-to": "^1.1.13",
"howler": "^2.2.4",
"htmlparser2": "^7.2.0",
"idb-keyval": "^6.2.1",
Expand Down
2 changes: 1 addition & 1 deletion src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ const CONST = {
MIN_SIZE: 240,

// Allowed extensions for receipts
ALLOWED_RECEIPT_EXTENSIONS: ['jpg', 'jpeg', 'gif', 'png', 'pdf', 'htm', 'html', 'text', 'rtf', 'doc', 'tif', 'tiff', 'msword', 'zip', 'xml', 'message'],
ALLOWED_RECEIPT_EXTENSIONS: ['heif', 'heic', 'jpg', 'jpeg', 'gif', 'png', 'pdf', 'htm', 'html', 'text', 'rtf', 'doc', 'tif', 'tiff', 'msword', 'zip', 'xml', 'message'],
},

// Allowed extensions for spreadsheets import
Expand Down
113 changes: 113 additions & 0 deletions src/libs/fileDownload/heicConverter/index.native.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import {ImageManipulator, SaveFormat} from 'expo-image-manipulator';
import type {FileObject} from '@components/AttachmentModal';
import {verifyFileFormat} from '@libs/fileDownload/FileUtils';
import CONST from '@src/CONST';
import type {HeicConverterFunction} from './types';

/**
* Helper function to convert HEIC/HEIF image to JPEG using ImageManipulator
* @param file - The original file object
* @param sourceUri - URI of the image to convert
* @param originalExtension - The original file extension pattern to replace
* @param callbacks - Callback functions for the conversion process
*/
const convertImageWithManipulator = (
file: FileObject,
sourceUri: string,
originalExtension: RegExp,
{
onSuccess = () => {},
onError = () => {},
onFinish = () => {},
}: {
onSuccess?: (convertedFile: FileObject) => void;
onError?: (error: unknown, originalFile: FileObject) => void;
onFinish?: () => void;
} = {},
) => {
ImageManipulator.manipulate(sourceUri)
.renderAsync()
.then((manipulatedImage) => manipulatedImage.saveAsync({format: SaveFormat.JPEG}))
.then((manipulationResult) => {
const convertedFile = {
uri: manipulationResult.uri,
name: file.name?.replace(originalExtension, '.jpg') ?? 'converted-image.jpg',
type: 'image/jpeg',
size: file.size,
width: manipulationResult.width,
height: manipulationResult.height,
};
onSuccess(convertedFile);
})
.catch((err) => {
console.error('Error converting HEIC/HEIF to JPEG:', err);
onError(err, file);
})
.finally(() => {
onFinish();
});
};

/**
* Native implementation for converting HEIC/HEIF images to JPEG
* @param file - The file to check and potentially convert
* @param callbacks - Object containing callback functions for different stages of conversion
*/
const convertHeicImage: HeicConverterFunction = (file, {onSuccess = () => {}, onError = () => {}, onStart = () => {}, onFinish = () => {}} = {}) => {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
const needsConversion = file.name?.toLowerCase().endsWith('.heic') || file.name?.toLowerCase().endsWith('.heif');

if (!needsConversion || !file.uri || !file.type?.startsWith('image')) {
onSuccess(file);
return;
}

onStart();

if (!file.uri) {
onError(new Error('File URI is undefined'), file);
onFinish();
return;
}

// Conversion based on extension
if (needsConversion) {
const fileUri = file.uri;
convertImageWithManipulator(file, fileUri, /\.(heic|heif)$/i, {
onSuccess,
onError,
onFinish,
});
return;
}

// If not detected by extension, check using file signatures
verifyFileFormat({fileUri: file.uri, formatSignatures: CONST.HEIC_SIGNATURES})
.then((isHEIC) => {
if (isHEIC) {
const fileUri = file.uri;
if (!fileUri) {
onError(new Error('File URI is undefined'), file);
onFinish();
return;
}
convertImageWithManipulator(file, fileUri, /\.heic$/i, {
onSuccess,
onError,
onFinish,
});
return;
}

onSuccess(file);
})
.catch((err) => {
console.error('Error processing the file:', err);
onError(err, file);
})
.finally(() => {
onFinish();
});
};

export default convertHeicImage;
65 changes: 65 additions & 0 deletions src/libs/fileDownload/heicConverter/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import {heicTo, isHeic} from 'heic-to';
import type {HeicConverterFunction} from './types';

/**
* Web implementation for converting HEIC/HEIF images to JPEG
* @param file - The file to check and potentially convert
* @param callbacks - Object containing callback functions for different stages of conversion
*/
const convertHeicImage: HeicConverterFunction = (file, {onSuccess = () => {}, onError = () => {}, onStart = () => {}, onFinish = () => {}} = {}) => {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
const needsConversion = file.name?.toLowerCase().endsWith('.heic') || file.name?.toLowerCase().endsWith('.heif');

if (!needsConversion || !file.uri || !file.type?.startsWith('image')) {
onSuccess(file);
return;
}

onStart();

if (!file.uri) {
onError(new Error('File URI is undefined'), file);
onFinish();
return;
}

fetch(file.uri)
.then((response) => response.blob())
.then((blob) => {
const fileFromBlob = new File([blob], file.name ?? 'temp-file', {
type: blob.type,
});

return isHeic(fileFromBlob).then((isHEIC) => {
if (isHEIC || needsConversion) {
return heicTo({
blob,
type: 'image/jpeg',
})
.then((convertedBlob) => {
const fileName = file.name ? file.name.replace(/\.(heic|heif)$/i, '.jpg') : 'converted-image.jpg';
const jpegFile = new File([convertedBlob], fileName, {type: 'image/jpeg'});
jpegFile.uri = URL.createObjectURL(jpegFile);
onSuccess(jpegFile);
})
.catch((err) => {
console.error('Error converting image format to JPEG:', err);
onError(err, file);
})
.finally(() => {
onFinish();
});
}

onSuccess(file);
onFinish();
});
})
.catch((err) => {
console.error('Error processing the file:', err);
onError(err, file);
onFinish();
});
};

export default convertHeicImage;
12 changes: 12 additions & 0 deletions src/libs/fileDownload/heicConverter/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type {FileObject} from '@components/AttachmentModal';

type HeicConverterCallbacks = {
onSuccess?: (convertedFile: FileObject) => void;
onError?: (error: unknown, originalFile: FileObject) => void;
onStart?: () => void;
onFinish?: () => void;
};

type HeicConverterFunction = (file: FileObject, callbacks?: HeicConverterCallbacks) => void;

export type {HeicConverterCallbacks, HeicConverterFunction};
84 changes: 62 additions & 22 deletions src/pages/iou/request/step/IOURequestStepScan/index.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import setTestReceipt from '@libs/actions/setTestReceipt';
import {dismissProductTraining} from '@libs/actions/Welcome';
import {readFileAsync, resizeImageIfNeeded, showCameraPermissionsAlert, splitExtensionFromFileName} from '@libs/fileDownload/FileUtils';
import getPhotoSource from '@libs/fileDownload/getPhotoSource';
import convertHeicImage from '@libs/fileDownload/heicConverter';
import getCurrentPosition from '@libs/getCurrentPosition';
import getPlatform from '@libs/getPlatform';
import getReceiptsUploadFolderPath from '@libs/getReceiptsUploadFolderPath';
Expand Down Expand Up @@ -520,6 +521,22 @@ function IOURequestStepScan({
},
);

/**
* Converts HEIC image to JPEG using promises
*/
const convertHeicImageToJpegPromise = (file: FileObject): Promise<FileObject> => {
return new Promise((resolve, reject) => {
convertHeicImage(file, {
onStart: () => setIsLoaderVisible(true),
onSuccess: (convertedFile) => resolve(convertedFile),
onError: (nonConvertedFile) => {
reject(nonConvertedFile);
},
onFinish: () => setIsLoaderVisible(false),
});
});
};

/**
* Sets the Receipt objects and navigates the user to the next page
*/
Expand All @@ -534,33 +551,56 @@ function IOURequestStepScan({
return;
}

resizeImageIfNeeded(originalFile).then((file) => {
// Store the receipt on the transaction object in Onyx
// On Android devices, fetching blob for a file with name containing spaces fails to retrieve the type of file.
// So, let us also save the file type in receipt for later use during blob fetch
setMoneyRequestReceipt(initialTransactionID, file?.uri ?? '', file.name ?? '', !isEditing, file.type);
// Helper function to process the file after any conversion
const processFile = (file: FileObject) => {
resizeImageIfNeeded(file).then((resizedFile) => {
// Store the receipt on the transaction object in Onyx
// On Android devices, fetching blob for a file with name containing spaces fails to retrieve the type of file.
// So, let us also save the file type in receipt for later use during blob fetch
setMoneyRequestReceipt(initialTransactionID, resizedFile?.uri ?? '', resizedFile.name ?? '', !isEditing, resizedFile.type);

if (isEditing) {
updateScanAndNavigate(file, file?.uri ?? '');
return;
}

const newReceiptFiles = [{file, source: file?.uri ?? '', transactionID: initialTransactionID}];
if (isEditing) {
updateScanAndNavigate(resizedFile, resizedFile?.uri ?? '');
return;
}

if (shouldSkipConfirmation) {
setReceiptFiles(newReceiptFiles);
const gpsRequired = initialTransaction?.amount === 0 && iouType !== CONST.IOU.TYPE.SPLIT && file;
const newReceiptFiles = [{file: resizedFile, source: resizedFile?.uri ?? '', transactionID: initialTransactionID}];

if (gpsRequired) {
const beginLocationPermissionFlow = shouldStartLocationPermissionFlow();
if (beginLocationPermissionFlow) {
setStartLocationPermissionFlow(true);
return;
if (shouldSkipConfirmation) {
setReceiptFiles(newReceiptFiles);
const gpsRequired = initialTransaction?.amount === 0 && iouType !== CONST.IOU.TYPE.SPLIT && resizedFile;

if (gpsRequired) {
const beginLocationPermissionFlow = shouldStartLocationPermissionFlow();
if (beginLocationPermissionFlow) {
setStartLocationPermissionFlow(true);
return;
}
}
}
}
navigateToConfirmationStep(newReceiptFiles, false);
});
navigateToConfirmationStep(newReceiptFiles, false);
});
};

// Check if the file is HEIC/HEIF and needs conversion
if (
originalFile?.type?.startsWith('image') &&
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
(originalFile.name?.toLowerCase().endsWith('.heic') || originalFile.name?.toLowerCase().endsWith('.heif'))
) {
convertHeicImageToJpegPromise(originalFile)
.then((convertedFile) => {
processFile(convertedFile);
})
.catch((fallbackFile: FileObject) => {
// Use the original file if conversion fails
processFile(fallbackFile);
});
return;
}

// Process the file directly if no conversion is needed
processFile(originalFile);
};

const capturePhoto = useCallback(() => {
Expand Down
Loading