Skip to content

Commit

Permalink
[Mobile] Hotfix release v1.3.1 - Fix crash on undefined image url (#1…
Browse files Browse the repository at this point in the history
…5618)

* 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
  • Loading branch information
mkevins committed May 14, 2019
1 parent a8073b4 commit 5954265
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions packages/block-library/src/image/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -128,7 +135,7 @@ class ImageEdit extends React.Component {
this.finishMediaUploadWithFailure( payload );
break;
case MEDIA_UPLOAD_STATE_RESET:
this.mediaUploadStateReset( payload );
this.mediaUploadStateReset();
break;
}
}
Expand All @@ -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 } );
}

Expand Down

0 comments on commit 5954265

Please sign in to comment.