From fac3b7746f2fe5d8172aea95cb60d936af8c695f Mon Sep 17 00:00:00 2001 From: Nikki Wines Date: Tue, 27 Feb 2024 12:09:16 -0800 Subject: [PATCH] Merge pull request #37322 from dukenv0307/fix/37240 Fix unable to add the file that is not image (cherry picked from commit 59511b0c10aa3d123fc1f0baf36e050a35fd3adc) --- .../AttachmentPicker/index.native.js | 50 ++++++++++++------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/src/components/AttachmentPicker/index.native.js b/src/components/AttachmentPicker/index.native.js index d4d3d0696c59..0387ee087127 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'; @@ -230,6 +231,28 @@ function AttachmentPicker({type, children, shouldHideCameraOption}) { setIsVisible(false); }; + /** + * @param {Object} fileData + * @returns {Promise} + */ + const validateAndCompleteAttachmentSelection = 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) @@ -244,24 +267,17 @@ 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; + return validateAndCompleteAttachmentSelection(fileData); + }); + } else { + return validateAndCompleteAttachmentSelection(fileData); + } }, - [showGeneralAlert, showImageCorruptionAlert], + [validateAndCompleteAttachmentSelection], ); /**