Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isFileURL method and use it on all native media upload checks. #20985

Merged
merged 4 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/block-library/src/gallery/gallery-image.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { Component } from '@wordpress/element';
import { Icon } from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
import { Caption, MediaUploadProgress } from '@wordpress/block-editor';
import { isURL } from '@wordpress/url';
import { getProtocol } from '@wordpress/url';
import { withPreferredColorScheme } from '@wordpress/compose';
import { close, arrowLeft, arrowRight } from '@wordpress/icons';

Expand Down Expand Up @@ -91,7 +91,10 @@ class GalleryImage extends Component {

if ( this.state.isUploadInProgress ) {
requestImageUploadCancelDialog( id );
} else if ( this.state.didUploadFail || ( id && ! isURL( url ) ) ) {
} else if (
this.state.didUploadFail ||
( id && getProtocol( url ) === 'file:' )
) {
requestImageFailedRetryDialog( id );
}
}
Expand Down
20 changes: 13 additions & 7 deletions packages/block-library/src/image/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import {
MediaEdit,
} from '@wordpress/block-editor';
import { __, sprintf } from '@wordpress/i18n';
import { isURL } from '@wordpress/url';
import { getProtocol } from '@wordpress/url';
import { doAction, hasAction } from '@wordpress/hooks';
import { compose, withPreferredColorScheme } from '@wordpress/compose';
import { withSelect } from '@wordpress/data';
Expand Down Expand Up @@ -83,8 +83,6 @@ const getUrlForSlug = ( image, { sizeSlug } ) => {
return get( image, [ 'media_details', 'sizes', sizeSlug, 'source_url' ] );
};

const isFileUrl = ( url ) => url && url.startsWith( 'file:' );

export class ImageEdit extends React.Component {
constructor( props ) {
super( props );
Expand Down Expand Up @@ -130,7 +128,7 @@ export class ImageEdit extends React.Component {
if (
! attributes.id &&
attributes.url &&
isFileUrl( attributes.url )
getProtocol( attributes.url ) === 'file:'
) {
requestMediaImport( attributes.url, ( id, url ) => {
if ( url ) {
Expand All @@ -141,7 +139,11 @@ export class ImageEdit extends React.Component {

// Make sure we mark any temporary images as failed if they failed while
// the editor wasn't open
if ( attributes.id && attributes.url && ! isURL( attributes.url ) ) {
if (
attributes.id &&
attributes.url &&
getProtocol( attributes.url ) === 'file:'
) {
mediaUploadSync();
}
}
Expand Down Expand Up @@ -180,7 +182,10 @@ export class ImageEdit extends React.Component {

if ( this.state.isUploadInProgress ) {
requestImageUploadCancelDialog( attributes.id );
} else if ( attributes.id && ! isURL( attributes.url ) ) {
} else if (
attributes.id &&
getProtocol( attributes.url ) === 'file:'
) {
requestImageFailedRetryDialog( attributes.id );
} else if ( ! this.state.isCaptionSelected ) {
requestImageFullscreenPreview(
Expand Down Expand Up @@ -669,7 +674,8 @@ export default compose( [
} = props;
const { imageSizes } = getSettings();

const shouldGetMedia = id && isSelected && ! isFileUrl( url );
const shouldGetMedia =
id && isSelected && getProtocol( url ) !== 'file:';
return {
image: shouldGetMedia ? getMedia( id ) : null,
imageSizes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
} from '@wordpress/block-editor';
import { Component } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { isURL } from '@wordpress/url';
import { isURL, getProtocol } from '@wordpress/url';
import { compose, withPreferredColorScheme } from '@wordpress/compose';
import { replace } from '@wordpress/icons';

Expand Down Expand Up @@ -74,7 +74,7 @@ class MediaContainer extends Component {

// Make sure we mark any temporary images as failed if they failed while
// the editor wasn't open
if ( mediaId && mediaUrl && mediaUrl.indexOf( 'file:' ) === 0 ) {
if ( mediaId && mediaUrl && getProtocol( mediaUrl ) === 'file:' ) {
mediaUploadSync();
}
}
Expand All @@ -101,7 +101,7 @@ class MediaContainer extends Component {

if ( this.state.isUploadInProgress ) {
requestImageUploadCancelDialog( mediaId );
} else if ( mediaId && ! isURL( mediaUrl ) ) {
} else if ( mediaId && getProtocol( mediaUrl ) === 'file:' ) {
requestImageFailedRetryDialog( mediaId );
}
}
Expand Down
9 changes: 6 additions & 3 deletions packages/block-library/src/video/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
InspectorControls,
} from '@wordpress/block-editor';
import { __, sprintf } from '@wordpress/i18n';
import { isURL } from '@wordpress/url';
import { isURL, getProtocol } from '@wordpress/url';
import { doAction, hasAction } from '@wordpress/hooks';
import { video as SvgIcon, replace } from '@wordpress/icons';

Expand Down Expand Up @@ -80,7 +80,7 @@ class VideoEdit extends React.Component {

componentDidMount() {
const { attributes } = this.props;
if ( attributes.id && ! isURL( attributes.src ) ) {
if ( attributes.id && getProtocol( attributes.src ) === 'file:' ) {
mediaUploadSync();
}
}
Expand Down Expand Up @@ -111,7 +111,10 @@ class VideoEdit extends React.Component {

if ( this.state.isUploadInProgress ) {
requestImageUploadCancelDialog( attributes.id );
} else if ( attributes.id && ! isURL( attributes.src ) ) {
} else if (
attributes.id &&
getProtocol( attributes.src ) === 'file:'
) {
requestImageFailedRetryDialog( attributes.id );
}

Expand Down
1 change: 1 addition & 0 deletions packages/url/src/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ describe( 'getProtocol', () => {
expect( getProtocol( 'https://localhost:8080' ) ).toBe( 'https:' );
expect( getProtocol( 'tel:1234' ) ).toBe( 'tel:' );
expect( getProtocol( 'blob:data' ) ).toBe( 'blob:' );
expect( getProtocol( 'file:///folder/file.txt' ) ).toBe( 'file:' );
} );

it( 'returns undefined when the provided value does not contain a URL protocol', () => {
Expand Down