-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added maximum file size validation to media upload; Passed maxUploadS…
…ize client-assets. This change makes client size validation of the file size, avoiding spending time uploading invalid files to server. It also shows a friendly error message if the file size validation fails.
- Loading branch information
1 parent
a528611
commit 9ba0213
Showing
18 changed files
with
361 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
.block-notices { | ||
margin: 0 0 12px 0; | ||
width: 100%; | ||
|
||
.notice { | ||
margin-left: 0px; | ||
margin-right: 0px; | ||
|
||
p { | ||
font-size: $default-font-size; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,63 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { noop } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { select } from '@wordpress/data'; | ||
import { mediaUpload } from '@wordpress/utils'; | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Upload a media file when the file upload button is activated. | ||
* Wrapper around mediaUpload() that injects the current post ID. | ||
* | ||
* @param {Array} filesList List of files. | ||
* @param {Function} onFileChange Function to be called each time a file or a temporary representation of the file is available. | ||
* @param {string} allowedType The type of media that can be uploaded. | ||
* @param {Object} $0 Parameters object passed to the function. | ||
* @param {string} $0.allowedType The type of media that can be uploaded. | ||
* @param {?Object} $0.additionalData Additional data to include in the request. | ||
* @param {Array} $0.filesList List of files. | ||
* @param {?number} $0.maxUploadFileSize Maximum upload size in bytes allowed for the site. | ||
* @param {Function} $0.onError Function called when an error happens. | ||
* @param {Function} $0.onFileChange Function called each time a file or a temporary representation of the file is available. | ||
*/ | ||
export default function editorMediaUpload( filesList, onFileChange, allowedType ) { | ||
export default function editorMediaUpload( { | ||
allowedType, | ||
filesList, | ||
maxUploadFileSize, | ||
onError = noop, | ||
onFileChange, | ||
} ) { | ||
let postId = null; | ||
// Editor isn't guaranteed in block context. | ||
if ( select( 'core/editor' ) ) { | ||
postId = select( 'core/editor' ).getCurrentPostId(); | ||
} | ||
mediaUpload( filesList, onFileChange, allowedType, { | ||
post: postId, | ||
const errorHandler = ( { file, sizeAboveLimit, generalError } ) => { | ||
let errorMsg; | ||
if ( sizeAboveLimit ) { | ||
errorMsg = sprintf( | ||
__( '%s exceeds the maximum upload size for this site.' ), | ||
file.name | ||
); | ||
} else if ( generalError ) { | ||
errorMsg = sprintf( | ||
__( 'Error while uploading file %s to the media library.' ), | ||
file.name | ||
); | ||
} | ||
onError( errorMsg ); | ||
}; | ||
|
||
mediaUpload( { | ||
allowedType, | ||
filesList, | ||
onFileChange, | ||
additionalData: { | ||
post: postId, | ||
}, | ||
maxUploadFileSize, | ||
onError: errorHandler, | ||
} ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import uuid from 'uuid/v4'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component, createHigherOrderComponent } from '@wordpress/element'; | ||
|
||
/** | ||
* Override the default edit UI to include notices if supported. | ||
* | ||
* @param {function|Component} OriginalComponent Original component. | ||
* @return {Component} Wrapped component. | ||
*/ | ||
export default createHigherOrderComponent( ( OriginalComponent ) => { | ||
return class WrappedBlockEdit extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
|
||
this.createNotice = this.createNotice.bind( this ); | ||
this.createErrorNotice = this.createErrorNotice.bind( this ); | ||
this.removeNotice = this.removeNotice.bind( this ); | ||
|
||
this.state = { | ||
noticeList: [], | ||
}; | ||
} | ||
|
||
/** | ||
* Function passed down as a prop that adds a new notice. | ||
* | ||
* @param {Object} notice Notice to add. | ||
*/ | ||
createNotice( notice ) { | ||
const noticeToAdd = notice.id ? notice : { ...notice, id: uuid() }; | ||
this.setState( ( state ) => ( { | ||
noticeList: [ ...state.noticeList, noticeToAdd ], | ||
} ) ); | ||
} | ||
|
||
/** | ||
* Function passed as a prop that adds a new error notice. | ||
* | ||
* @param {string} msg Error message of the notice. | ||
*/ | ||
createErrorNotice( msg ) { | ||
this.createNotice( { status: 'error', content: msg } ); | ||
} | ||
|
||
/** | ||
* Removes a notice by id. | ||
* | ||
* @param {string} id Id of the notice to remove. | ||
*/ | ||
removeNotice( id ) { | ||
this.setState( ( state ) => ( { | ||
noticeList: state.noticeList.filter( ( notice ) => notice.id !== id ), | ||
} ) ); | ||
} | ||
|
||
/** | ||
* Removes all notices | ||
*/ | ||
removeAllNotices() { | ||
this.setState( { | ||
noticeList: [], | ||
} ); | ||
} | ||
|
||
render() { | ||
const notices = { | ||
createNotice: this.createNotice, | ||
createErrorNotice: this.createErrorNotice, | ||
removeAllNotices: this.removeAllNotices, | ||
removeNotice: this.removeNotice, | ||
noticeList: this.state.noticeList, | ||
}; | ||
return ( | ||
<OriginalComponent | ||
{ ...this.props } | ||
notices={ notices } /> | ||
); | ||
} | ||
}; | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.