Skip to content

Commit e9fb49d

Browse files
authored
Merge pull request #35164 from dukenv0307/fix/32307
fix logic get document from native
2 parents 1366fb7 + f409821 commit e9fb49d

File tree

1 file changed

+36
-24
lines changed

1 file changed

+36
-24
lines changed

src/components/AttachmentPicker/index.native.js

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import lodashCompact from 'lodash/compact';
22
import PropTypes from 'prop-types';
33
import React, {useCallback, useMemo, useRef, useState} from 'react';
4-
import {Alert, View} from 'react-native';
4+
import {Alert, Image as RNImage, View} from 'react-native';
55
import RNFetchBlob from 'react-native-blob-util';
66
import RNDocumentPicker from 'react-native-document-picker';
77
import {launchImageLibrary} from 'react-native-image-picker';
@@ -57,11 +57,22 @@ const getImagePickerOptions = (type) => {
5757
};
5858

5959
/**
60-
* See https://github.com/rnmods/react-native-document-picker#options for DocumentPicker configuration options
60+
* Return documentPickerOptions based on the type
61+
* @param {String} type
62+
* @returns {Object}
6163
*/
62-
const documentPickerOptions = {
63-
type: [RNDocumentPicker.types.allFiles],
64-
copyTo: 'cachesDirectory',
64+
65+
const getDocumentPickerOptions = (type) => {
66+
if (type === CONST.ATTACHMENT_PICKER_TYPE.IMAGE) {
67+
return {
68+
type: [RNDocumentPicker.types.images],
69+
copyTo: 'cachesDirectory',
70+
};
71+
}
72+
return {
73+
type: [RNDocumentPicker.types.allFiles],
74+
copyTo: 'cachesDirectory',
75+
};
6576
};
6677

6778
/**
@@ -158,15 +169,15 @@ function AttachmentPicker({type, children, shouldHideCameraOption}) {
158169
*/
159170
const showDocumentPicker = useCallback(
160171
() =>
161-
RNDocumentPicker.pick(documentPickerOptions).catch((error) => {
172+
RNDocumentPicker.pick(getDocumentPickerOptions(type)).catch((error) => {
162173
if (RNDocumentPicker.isCancel(error)) {
163174
return;
164175
}
165176

166177
showGeneralAlert(error.message);
167178
throw error;
168179
}),
169-
[showGeneralAlert],
180+
[showGeneralAlert, type],
170181
);
171182

172183
const menuItemData = useMemo(() => {
@@ -181,15 +192,15 @@ function AttachmentPicker({type, children, shouldHideCameraOption}) {
181192
textTranslationKey: 'attachmentPicker.chooseFromGallery',
182193
pickAttachment: () => showImagePicker(launchImageLibrary),
183194
},
184-
type !== CONST.ATTACHMENT_PICKER_TYPE.IMAGE && {
195+
{
185196
icon: Expensicons.Paperclip,
186197
textTranslationKey: 'attachmentPicker.chooseDocument',
187198
pickAttachment: showDocumentPicker,
188199
},
189200
]);
190201

191202
return data;
192-
}, [showDocumentPicker, showImagePicker, type, shouldHideCameraOption]);
203+
}, [showDocumentPicker, showImagePicker, shouldHideCameraOption]);
193204

194205
const [focusedIndex, setFocusedIndex] = useArrowKeyFocusManager({initialFocusedIndex: -1, maxIndex: menuItemData.length - 1, isActive: isVisible});
195206

@@ -232,22 +243,23 @@ function AttachmentPicker({type, children, shouldHideCameraOption}) {
232243
onCanceled.current();
233244
return Promise.resolve();
234245
}
235-
236246
const fileData = _.first(attachments);
237-
238-
if (fileData.width === -1 || fileData.height === -1) {
239-
showImageCorruptionAlert();
240-
return Promise.resolve();
241-
}
242-
243-
return getDataForUpload(fileData)
244-
.then((result) => {
245-
completeAttachmentSelection.current(result);
246-
})
247-
.catch((error) => {
248-
showGeneralAlert(error.message);
249-
throw error;
250-
});
247+
RNImage.getSize(fileData.uri, (width, height) => {
248+
fileData.width = width;
249+
fileData.height = height;
250+
if (fileData.width === -1 || fileData.height === -1) {
251+
showImageCorruptionAlert();
252+
return Promise.resolve();
253+
}
254+
return getDataForUpload(fileData)
255+
.then((result) => {
256+
completeAttachmentSelection.current(result);
257+
})
258+
.catch((error) => {
259+
showGeneralAlert(error.message);
260+
throw error;
261+
});
262+
});
251263
},
252264
[showGeneralAlert, showImageCorruptionAlert],
253265
);

0 commit comments

Comments
 (0)