From 595426559212f7226880f0d703bb028a35aee54c Mon Sep 17 00:00:00 2001 From: Matthew Kevins Date: Tue, 14 May 2019 19:58:07 +1000 Subject: [PATCH] [Mobile] Hotfix release v1.3.1 - Fix crash on undefined image url (#15618) * Add safety check to image url before calling indexOf * Set attributes.id to null when an image upload is cancelled * Add console.warn and comments to help track WordPress-Android issue #9768 --- packages/block-library/src/image/edit.native.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/block-library/src/image/edit.native.js b/packages/block-library/src/image/edit.native.js index af26201f1d35de..9e1bf8ca7ce753 100644 --- a/packages/block-library/src/image/edit.native.js +++ b/packages/block-library/src/image/edit.native.js @@ -80,7 +80,14 @@ class ImageEdit extends React.Component { const { attributes, setAttributes } = this.props; - if ( attributes.id && ! isURL( attributes.url ) ) { + // This will warn when we have `id` defined, while `url` is undefined. + // This may help track this issue: https://github.com/wordpress-mobile/WordPress-Android/issues/9768 + // where a cancelled image upload was resulting in a subsequent crash. + if ( attributes.id && ! attributes.url ) { + console.warn( "Attributes has id with no url." ); + } + + if ( attributes.id && attributes.url && ! isURL( attributes.url ) ) { if ( attributes.url.indexOf( 'file:' ) === 0 ) { requestMediaImport( attributes.url, ( mediaId, mediaUri ) => { if ( mediaUri ) { @@ -128,7 +135,7 @@ class ImageEdit extends React.Component { this.finishMediaUploadWithFailure( payload ); break; case MEDIA_UPLOAD_STATE_RESET: - this.mediaUploadStateReset( payload ); + this.mediaUploadStateReset(); break; } } @@ -155,10 +162,10 @@ class ImageEdit extends React.Component { this.setState( { isUploadInProgress: false, isUploadFailed: true } ); } - mediaUploadStateReset( payload ) { + mediaUploadStateReset() { const { setAttributes } = this.props; - setAttributes( { id: payload.mediaId, url: null } ); + setAttributes( { id: null, url: null } ); this.setState( { isUploadInProgress: false, isUploadFailed: false } ); }