1
1
import lodashCompact from 'lodash/compact' ;
2
2
import PropTypes from 'prop-types' ;
3
3
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' ;
5
5
import RNFetchBlob from 'react-native-blob-util' ;
6
6
import RNDocumentPicker from 'react-native-document-picker' ;
7
7
import { launchImageLibrary } from 'react-native-image-picker' ;
@@ -57,11 +57,22 @@ const getImagePickerOptions = (type) => {
57
57
} ;
58
58
59
59
/**
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 }
61
63
*/
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
+ } ;
65
76
} ;
66
77
67
78
/**
@@ -158,15 +169,15 @@ function AttachmentPicker({type, children, shouldHideCameraOption}) {
158
169
*/
159
170
const showDocumentPicker = useCallback (
160
171
( ) =>
161
- RNDocumentPicker . pick ( documentPickerOptions ) . catch ( ( error ) => {
172
+ RNDocumentPicker . pick ( getDocumentPickerOptions ( type ) ) . catch ( ( error ) => {
162
173
if ( RNDocumentPicker . isCancel ( error ) ) {
163
174
return ;
164
175
}
165
176
166
177
showGeneralAlert ( error . message ) ;
167
178
throw error ;
168
179
} ) ,
169
- [ showGeneralAlert ] ,
180
+ [ showGeneralAlert , type ] ,
170
181
) ;
171
182
172
183
const menuItemData = useMemo ( ( ) => {
@@ -181,15 +192,15 @@ function AttachmentPicker({type, children, shouldHideCameraOption}) {
181
192
textTranslationKey : 'attachmentPicker.chooseFromGallery' ,
182
193
pickAttachment : ( ) => showImagePicker ( launchImageLibrary ) ,
183
194
} ,
184
- type !== CONST . ATTACHMENT_PICKER_TYPE . IMAGE && {
195
+ {
185
196
icon : Expensicons . Paperclip ,
186
197
textTranslationKey : 'attachmentPicker.chooseDocument' ,
187
198
pickAttachment : showDocumentPicker ,
188
199
} ,
189
200
] ) ;
190
201
191
202
return data ;
192
- } , [ showDocumentPicker , showImagePicker , type , shouldHideCameraOption ] ) ;
203
+ } , [ showDocumentPicker , showImagePicker , shouldHideCameraOption ] ) ;
193
204
194
205
const [ focusedIndex , setFocusedIndex ] = useArrowKeyFocusManager ( { initialFocusedIndex : - 1 , maxIndex : menuItemData . length - 1 , isActive : isVisible } ) ;
195
206
@@ -232,22 +243,23 @@ function AttachmentPicker({type, children, shouldHideCameraOption}) {
232
243
onCanceled . current ( ) ;
233
244
return Promise . resolve ( ) ;
234
245
}
235
-
236
246
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
+ } ) ;
251
263
} ,
252
264
[ showGeneralAlert , showImageCorruptionAlert ] ,
253
265
) ;
0 commit comments