Skip to content

Commit bd8d73b

Browse files
committed
Add a notice for an excessive video file sizev
This is the first iteration, and it might need more information in the notice.
1 parent 1de5ff8 commit bd8d73b

File tree

1 file changed

+51
-1
lines changed
  • assets/src/stories-editor/blocks/amp-story-page

1 file changed

+51
-1
lines changed

assets/src/stories-editor/blocks/amp-story-page/edit.js

+51-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import uuid from 'uuid/v4';
55
import classnames from 'classnames';
66
import PropTypes from 'prop-types';
7-
import { has } from 'lodash';
7+
import { has, reduceRight } from 'lodash';
88

99
/**
1010
* WordPress dependencies
@@ -68,6 +68,8 @@ class PageEdit extends Component {
6868
}
6969

7070
this.onSelectMedia = this.onSelectMedia.bind( this );
71+
this.isVideoSizeExcessive = this.isVideoSizeExcessive.bind( this );
72+
this.getSecondsFromTime = this.getSecondsFromTime.bind( this );
7173
}
7274

7375
/**
@@ -110,14 +112,54 @@ class PageEdit extends Component {
110112
}
111113
const mediaUrl = has( media, [ 'sizes', MAX_IMAGE_SIZE_SLUG, 'url' ] ) ? media.sizes[ MAX_IMAGE_SIZE_SLUG ].url : media.url;
112114

115+
let isExcessiveVideoSize = false;
116+
if ( VIDEO_BACKGROUND_TYPE === mediaType ) {
117+
isExcessiveVideoSize = this.isVideoSizeExcessive( media.filesizeInBytes, media.fileLength );
118+
}
119+
113120
this.props.setAttributes( {
114121
mediaUrl,
115122
mediaId: media.id,
116123
mediaType,
124+
isExcessiveVideoSize,
117125
poster: VIDEO_BACKGROUND_TYPE === mediaType && media.image && media.image.src !== media.icon ? media.image.src : undefined,
118126
} );
119127
}
120128

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+
121163
removeBackgroundColor( index ) {
122164
const { attributes, setAttributes } = this.props;
123165
const backgroundColors = JSON.parse( attributes.backgroundColors );
@@ -193,6 +235,7 @@ class PageEdit extends Component {
193235
mediaId,
194236
mediaType,
195237
mediaUrl,
238+
isExcessiveVideoSize,
196239
focalPoint = { x: .5, y: .5 },
197240
overlayOpacity,
198241
poster,
@@ -278,6 +321,12 @@ class PageEdit extends Component {
278321
</PanelColorSettings>
279322
<PanelBody title={ __( 'Background Media', 'amp' ) }>
280323
<>
324+
{
325+
isExcessiveVideoSize &&
326+
<Notice status="error" isDismissible={ false } >
327+
{ __( 'The video size is more than 1 MB per second.', 'amp' ) }
328+
</Notice>
329+
}
281330
<BaseControl>
282331
<MediaUploadCheck fallback={ instructions }>
283332
<MediaUpload
@@ -424,6 +473,7 @@ PageEdit.propTypes = {
424473
mediaId: PropTypes.number,
425474
mediaType: PropTypes.string,
426475
mediaUrl: PropTypes.string,
476+
isExcessiveVideoSize: PropTypes.bool,
427477
focalPoint: PropTypes.shape( {
428478
x: PropTypes.number.isRequired,
429479
y: PropTypes.number.isRequired,

0 commit comments

Comments
 (0)