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
16 changes: 7 additions & 9 deletions src/components/AttachmentPicker/index.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useStyleUtils from '@hooks/useStyleUtils';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import {cleanFileName, resizeImageIfNeeded, showCameraPermissionsAlert, verifyFileFormat} from '@libs/fileDownload/FileUtils';
import {cleanFileName, showCameraPermissionsAlert, verifyFileFormat} from '@libs/fileDownload/FileUtils';
import Log from '@libs/Log';
import CONST from '@src/CONST';
import type {TranslationPaths} from '@src/languages/types';
Expand Down Expand Up @@ -429,10 +429,9 @@ function AttachmentPicker({

if (!shouldValidateImage && fileDataName && Str.isImage(fileDataName)) {
return getDataForUpload(fileDataObject)
.then((file) => resizeImageIfNeeded(file))
.then((resizedFile) =>
ImageSize.getSize(resizedFile.uri ?? '').then(({width, height}) => ({
...resizedFile,
.then((file) =>
ImageSize.getSize(file.uri ?? '').then(({width, height}) => ({
...file,
width,
height,
})),
Expand All @@ -442,16 +441,15 @@ function AttachmentPicker({

if (fileDataName && Str.isImage(fileDataName)) {
return getDataForUpload(fileDataObject)
.then((file) => resizeImageIfNeeded(file))
.then((resizedFile) =>
ImageSize.getSize(resizedFile.uri ?? '').then(({width, height}) => {
.then((file) =>
ImageSize.getSize(file.uri ?? '').then(({width, height}) => {
if (width <= 0 || height <= 0) {
showImageCorruptionAlert();
return null;
}

return {
...resizedFile,
...file,
width,
height,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ function MoneyRequestReceiptView({
openPicker({
onPicked: (files) => {
onPickerClosed();
validateFiles(files);
validateFiles(files, undefined, {isValidatingReceipts: false});
},
onCanceled: onPickerClosed,
});
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useFilesValidation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const sortFilesByOriginalOrder = (files: FileObject[], orderMap: Map<string, num
return files.sort((a, b) => (orderMap.get(a.uri ?? '') ?? 0) - (orderMap.get(b.uri ?? '') ?? 0));
};

const isImageFile = (file: FileObject) => hasHeicOrHeifExtension(file) ?? Str.isImage(file.name ?? '');
const isImageFile = (file: FileObject) => !!hasHeicOrHeifExtension(file) || Str.isImage(file.name ?? '');

function useFilesValidation(onFilesValidated: (files: FileObject[], dataTransferItems: DataTransferItem[]) => void) {
const styles = useThemeStyles();
Expand Down Expand Up @@ -166,7 +166,7 @@ function useFilesValidation(onFilesValidated: (files: FileObject[], dataTransfer
return;
}

if (result.error === CONST.FILE_VALIDATION_ERRORS.FILE_TOO_LARGE && isImageFile(file) && validationState.isValidatingReceipts) {
if (result.error === CONST.FILE_VALIDATION_ERRORS.FILE_TOO_LARGE && isImageFile(file)) {
filesToResize.push(file);
return;
}
Expand Down
4 changes: 1 addition & 3 deletions src/libs/validateAttachmentFile.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {Str} from 'expensify-common';
import type {ValueOf} from 'type-fest';
import CONST from '@src/CONST';
import type {FileObject} from '@src/types/utils/Attachment';
Expand Down Expand Up @@ -29,9 +28,8 @@ async function validateAttachmentFile(file: FileObject, item?: DataTransferItem,
return {isValid: false, error: CONST.FILE_VALIDATION_ERRORS.HEIC_OR_HEIF_IMAGE};
}

const isImage = Str.isImage(file.name);
const maxFileSize = isValidatingReceipts ? CONST.API_ATTACHMENT_VALIDATIONS.RECEIPT_MAX_SIZE : CONST.API_ATTACHMENT_VALIDATIONS.MAX_SIZE;
if (!isImage && !hasHeicOrHeifExtension(file) && file.size > maxFileSize) {
if (file.size > maxFileSize) {
return {isValid: false, error: CONST.FILE_VALIDATION_ERRORS.FILE_TOO_LARGE};
}

Expand Down
8 changes: 6 additions & 2 deletions tests/unit/ValidateAttachmentFileTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,15 @@ describe('validateAttachmentFile', () => {
expect(error.error).toEqual(CONST.FILE_VALIDATION_ERRORS.FILE_TOO_LARGE);
});

it('returns valid result for image over RECEIPT_MAX_SIZE (images skip non-image size check)', async () => {
it('returns invalid result for image over RECEIPT_MAX_SIZE', async () => {
const file = createMockFile('receipt.jpg', CONST.API_ATTACHMENT_VALIDATIONS.MAX_SIZE + 1);
const error = await validateAttachmentFile(file, undefined, true);

expect(error.isValid).toBe(true);
if (error.isValid) {
throw new Error('validateAttachmentFile should return an invalid result');
}

expect(error.error).toEqual(CONST.FILE_VALIDATION_ERRORS.FILE_TOO_LARGE);
});

it('returns valid result when non-image is exactly at MAX_SIZE', async () => {
Expand Down
Loading