|
4 | 4 | import uuid from 'uuid/v4';
|
5 | 5 | import classnames from 'classnames';
|
6 | 6 | import PropTypes from 'prop-types';
|
7 |
| -import { has } from 'lodash'; |
| 7 | +import { has, reduceRight } from 'lodash'; |
8 | 8 |
|
9 | 9 | /**
|
10 | 10 | * WordPress dependencies
|
@@ -68,6 +68,8 @@ class PageEdit extends Component {
|
68 | 68 | }
|
69 | 69 |
|
70 | 70 | this.onSelectMedia = this.onSelectMedia.bind( this );
|
| 71 | + this.isVideoSizeExcessive = this.isVideoSizeExcessive.bind( this ); |
| 72 | + this.getSecondsFromTime = this.getSecondsFromTime.bind( this ); |
71 | 73 | }
|
72 | 74 |
|
73 | 75 | /**
|
@@ -110,14 +112,54 @@ class PageEdit extends Component {
|
110 | 112 | }
|
111 | 113 | const mediaUrl = has( media, [ 'sizes', MAX_IMAGE_SIZE_SLUG, 'url' ] ) ? media.sizes[ MAX_IMAGE_SIZE_SLUG ].url : media.url;
|
112 | 114 |
|
| 115 | + let isExcessiveVideoSize = false; |
| 116 | + if ( VIDEO_BACKGROUND_TYPE === mediaType ) { |
| 117 | + isExcessiveVideoSize = this.isVideoSizeExcessive( media.filesizeInBytes, media.fileLength ); |
| 118 | + } |
| 119 | + |
113 | 120 | this.props.setAttributes( {
|
114 | 121 | mediaUrl,
|
115 | 122 | mediaId: media.id,
|
116 | 123 | mediaType,
|
| 124 | + isExcessiveVideoSize, |
117 | 125 | poster: VIDEO_BACKGROUND_TYPE === mediaType && media.image && media.image.src !== media.icon ? media.image.src : undefined,
|
118 | 126 | } );
|
119 | 127 | }
|
120 | 128 |
|
| 129 | + /** |
| 130 | + * Gets whether the file is more than 1 MB per second, like 3 MB for 3 seconds. |
| 131 | + * |
| 132 | + * @param {number} fileSize The size of the file in bytes. |
| 133 | + * @param {string} length A colon-separated time length of the file, like '01:04'. |
| 134 | + * @return {boolean} Whether the file size is more than 1MB per second. |
| 135 | + */ |
| 136 | + isVideoSizeExcessive( fileSize, length ) { |
| 137 | + const fileLengthInSeconds = this.getSecondsFromTime( length ); |
| 138 | + const megabyteInBytes = 1000000; |
| 139 | + return fileSize > fileLengthInSeconds * megabyteInBytes; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Gets the number of seconds in a colon-separated time string, like '01:10'. |
| 144 | + * |
| 145 | + * @param {string} time A colon-separated time, like '0:12'. |
| 146 | + * @return {number} seconds The number of seconds in the time, like 12. |
| 147 | + */ |
| 148 | + getSecondsFromTime( time ) { |
| 149 | + const minuteInSeconds = 60; |
| 150 | + const splitTime = time.split( ':' ); |
| 151 | + |
| 152 | + return reduceRight( |
| 153 | + splitTime, |
| 154 | + ( totalSeconds, timeSection, index ) => { |
| 155 | + const distanceFromRight = splitTime.length - 1 - index; |
| 156 | + const multiple = Math.pow( minuteInSeconds, distanceFromRight ); // This should be 1 for seconds, 60 for minutes, etc... |
| 157 | + return totalSeconds + ( multiple * parseInt( timeSection ) ); |
| 158 | + }, |
| 159 | + 0 |
| 160 | + ); |
| 161 | + } |
| 162 | + |
121 | 163 | removeBackgroundColor( index ) {
|
122 | 164 | const { attributes, setAttributes } = this.props;
|
123 | 165 | const backgroundColors = JSON.parse( attributes.backgroundColors );
|
@@ -193,6 +235,7 @@ class PageEdit extends Component {
|
193 | 235 | mediaId,
|
194 | 236 | mediaType,
|
195 | 237 | mediaUrl,
|
| 238 | + isExcessiveVideoSize, |
196 | 239 | focalPoint = { x: .5, y: .5 },
|
197 | 240 | overlayOpacity,
|
198 | 241 | poster,
|
@@ -278,6 +321,12 @@ class PageEdit extends Component {
|
278 | 321 | </PanelColorSettings>
|
279 | 322 | <PanelBody title={ __( 'Background Media', 'amp' ) }>
|
280 | 323 | <>
|
| 324 | + { |
| 325 | + isExcessiveVideoSize && |
| 326 | + <Notice status="error" isDismissible={ false } > |
| 327 | + { __( 'The video size is more than 1 MB per second.', 'amp' ) } |
| 328 | + </Notice> |
| 329 | + } |
281 | 330 | <BaseControl>
|
282 | 331 | <MediaUploadCheck fallback={ instructions }>
|
283 | 332 | <MediaUpload
|
@@ -424,6 +473,7 @@ PageEdit.propTypes = {
|
424 | 473 | mediaId: PropTypes.number,
|
425 | 474 | mediaType: PropTypes.string,
|
426 | 475 | mediaUrl: PropTypes.string,
|
| 476 | + isExcessiveVideoSize: PropTypes.bool, |
427 | 477 | focalPoint: PropTypes.shape( {
|
428 | 478 | x: PropTypes.number.isRequired,
|
429 | 479 | y: PropTypes.number.isRequired,
|
|
0 commit comments