From 939b053faa8141fd75c2e9f6d5d87fba119eb5ad Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Wed, 28 Feb 2024 00:17:58 +0700 Subject: [PATCH 1/5] fix unable to add the file that is not image --- .../AttachmentPicker/index.native.js | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/components/AttachmentPicker/index.native.js b/src/components/AttachmentPicker/index.native.js index d4d3d0696c59..804e5c564972 100644 --- a/src/components/AttachmentPicker/index.native.js +++ b/src/components/AttachmentPicker/index.native.js @@ -244,22 +244,25 @@ function AttachmentPicker({type, children, shouldHideCameraOption}) { return Promise.resolve(); } const fileData = _.first(attachments); - RNImage.getSize(fileData.uri, (width, height) => { - fileData.width = width; - fileData.height = height; - if (fileData.width === -1 || fileData.height === -1) { - showImageCorruptionAlert(); - return Promise.resolve(); - } - return getDataForUpload(fileData) - .then((result) => { - completeAttachmentSelection.current(result); - }) - .catch((error) => { - showGeneralAlert(error.message); - throw error; - }); - }); + if (Str.isImage(fileData.fileName || fileData.name)) { + RNImage.getSize(fileData.fileCopyUri || fileData.uri, (width, height) => { + fileData.width = width; + fileData.height = height; + }) + } + + if (fileData.width === -1 || fileData.height === -1) { + showImageCorruptionAlert(); + return Promise.resolve(); + } + return getDataForUpload(fileData) + .then((result) => { + completeAttachmentSelection.current(result); + }) + .catch((error) => { + showGeneralAlert(error.message); + throw error; + }); }, [showGeneralAlert, showImageCorruptionAlert], ); From def61e1aa88ce80587234460005f40881c2d3feb Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Wed, 28 Feb 2024 00:43:22 +0700 Subject: [PATCH 2/5] fix lint --- .../AttachmentPicker/index.native.js | 42 ++++++++++++------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/src/components/AttachmentPicker/index.native.js b/src/components/AttachmentPicker/index.native.js index 804e5c564972..f6d6166ce02b 100644 --- a/src/components/AttachmentPicker/index.native.js +++ b/src/components/AttachmentPicker/index.native.js @@ -1,3 +1,4 @@ +import Str from 'expensify-common/lib/str'; import lodashCompact from 'lodash/compact'; import PropTypes from 'prop-types'; import React, {useCallback, useMemo, useRef, useState} from 'react'; @@ -248,21 +249,34 @@ function AttachmentPicker({type, children, shouldHideCameraOption}) { RNImage.getSize(fileData.fileCopyUri || fileData.uri, (width, height) => { fileData.width = width; fileData.height = height; - }) - } - - if (fileData.width === -1 || fileData.height === -1) { - showImageCorruptionAlert(); - return Promise.resolve(); - } - return getDataForUpload(fileData) - .then((result) => { - completeAttachmentSelection.current(result); - }) - .catch((error) => { - showGeneralAlert(error.message); - throw error; + + if (fileData.width === -1 || fileData.height === -1) { + showImageCorruptionAlert(); + return Promise.resolve(); + } + return getDataForUpload(fileData) + .then((result) => { + completeAttachmentSelection.current(result); + }) + .catch((error) => { + showGeneralAlert(error.message); + throw error; + }); }); + } else { + if (fileData.width === -1 || fileData.height === -1) { + showImageCorruptionAlert(); + return Promise.resolve(); + } + return getDataForUpload(fileData) + .then((result) => { + completeAttachmentSelection.current(result); + }) + .catch((error) => { + showGeneralAlert(error.message); + throw error; + }); + } }, [showGeneralAlert, showImageCorruptionAlert], ); From 1e7e0d2c3303943ad028e2800875fde853889719 Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Wed, 28 Feb 2024 02:00:08 +0700 Subject: [PATCH 3/5] dry code --- .../AttachmentPicker/index.native.js | 51 +++++++++---------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/src/components/AttachmentPicker/index.native.js b/src/components/AttachmentPicker/index.native.js index f6d6166ce02b..3ab26369f3ad 100644 --- a/src/components/AttachmentPicker/index.native.js +++ b/src/components/AttachmentPicker/index.native.js @@ -231,6 +231,28 @@ function AttachmentPicker({type, children, shouldHideCameraOption}) { setIsVisible(false); }; + /** + * @param {Object} fileData + * @returns {Promise} + */ + const validateAttachment = useCallback( + (fileData) => { + if (fileData.width === -1 || fileData.height === -1) { + showImageCorruptionAlert(); + return Promise.resolve(); + } + return getDataForUpload(fileData) + .then((result) => { + completeAttachmentSelection.current(result); + }) + .catch((error) => { + showGeneralAlert(error.message); + throw error; + }); + }, + [showGeneralAlert, showImageCorruptionAlert], + ); + /** * Handles the image/document picker result and * sends the selected attachment to the caller (parent component) @@ -249,36 +271,13 @@ function AttachmentPicker({type, children, shouldHideCameraOption}) { RNImage.getSize(fileData.fileCopyUri || fileData.uri, (width, height) => { fileData.width = width; fileData.height = height; - - if (fileData.width === -1 || fileData.height === -1) { - showImageCorruptionAlert(); - return Promise.resolve(); - } - return getDataForUpload(fileData) - .then((result) => { - completeAttachmentSelection.current(result); - }) - .catch((error) => { - showGeneralAlert(error.message); - throw error; - }); + return validateAttachment(fileData); }); } else { - if (fileData.width === -1 || fileData.height === -1) { - showImageCorruptionAlert(); - return Promise.resolve(); - } - return getDataForUpload(fileData) - .then((result) => { - completeAttachmentSelection.current(result); - }) - .catch((error) => { - showGeneralAlert(error.message); - throw error; - }); + return validateAttachment(fileData); } }, - [showGeneralAlert, showImageCorruptionAlert], + [validateAttachment], ); /** From 36efdd11895478b04e4fb73ada8337ac3f40f846 Mon Sep 17 00:00:00 2001 From: dukenv0307 <129500732+dukenv0307@users.noreply.github.com> Date: Wed, 28 Feb 2024 02:23:48 +0700 Subject: [PATCH 4/5] Update src/components/AttachmentPicker/index.native.js Co-authored-by: Rajat --- src/components/AttachmentPicker/index.native.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/AttachmentPicker/index.native.js b/src/components/AttachmentPicker/index.native.js index 3ab26369f3ad..581218e4af48 100644 --- a/src/components/AttachmentPicker/index.native.js +++ b/src/components/AttachmentPicker/index.native.js @@ -235,7 +235,7 @@ function AttachmentPicker({type, children, shouldHideCameraOption}) { * @param {Object} fileData * @returns {Promise} */ - const validateAttachment = useCallback( + const validateAndCompleteAttachmentSelection = useCallback( (fileData) => { if (fileData.width === -1 || fileData.height === -1) { showImageCorruptionAlert(); From f2c9300040e69644fc91b4c170ade2744b2f0cd0 Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Wed, 28 Feb 2024 02:33:19 +0700 Subject: [PATCH 5/5] rename reference --- src/components/AttachmentPicker/index.native.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/components/AttachmentPicker/index.native.js b/src/components/AttachmentPicker/index.native.js index 581218e4af48..0387ee087127 100644 --- a/src/components/AttachmentPicker/index.native.js +++ b/src/components/AttachmentPicker/index.native.js @@ -271,13 +271,13 @@ function AttachmentPicker({type, children, shouldHideCameraOption}) { RNImage.getSize(fileData.fileCopyUri || fileData.uri, (width, height) => { fileData.width = width; fileData.height = height; - return validateAttachment(fileData); + return validateAndCompleteAttachmentSelection(fileData); }); } else { - return validateAttachment(fileData); + return validateAndCompleteAttachmentSelection(fileData); } }, - [validateAttachment], + [validateAndCompleteAttachmentSelection], ); /**