-
Notifications
You must be signed in to change notification settings - Fork 220
/
Video.js
68 lines (62 loc) · 2.51 KB
/
Video.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import React from 'react';
import PropTypes from 'prop-types';
import {Cloudinary, Transformation, Util} from 'cloudinary-core';
import CloudinaryComponent from '../CloudinaryComponent';
const DEFAULT_POSTER_OPTIONS = {
format: 'jpg',
resource_type: 'video'
};
/**
* A component representing a Cloudinary served video
*/
class Video extends CloudinaryComponent {
constructor(props, context) {
super(props, context);
this.state = {};
}
render() {
let {publicId, poster, sourceTypes, fallback, sourceTransformation: sourceTransformations, innerRef, ...options} = Object.assign({},
this.getContext(),
this.props);
sourceTransformations = sourceTransformations || {};
sourceTypes = sourceTypes || Cloudinary.DEFAULT_VIDEO_PARAMS.source_types;
options = CloudinaryComponent.normalizeOptions(options, {});
let cld = Cloudinary.new(Util.withSnakeCaseKeys(options));
let sources = [];
let tagAttributes = Transformation.new(options).toHtmlAttributes();
let childTransformations = this.getTransformation(options);
if (Util.isPlainObject(poster)) {
let defaults = poster.publicId !== undefined && poster.publicId !== null ? Cloudinary.DEFAULT_IMAGE_PARAMS : DEFAULT_POSTER_OPTIONS;
poster = cld.url(poster.publicId || publicId, Util.defaults({}, Util.withSnakeCaseKeys(poster), defaults));
}
if (!Util.isEmpty(poster)) {
tagAttributes.poster = poster;
}
if (!Util.isEmpty(this.state.poster)) {
tagAttributes.poster = this.state.poster;
}
if (Util.isArray(sourceTypes)) {
sources = sourceTypes.map(srcType => {
let sourceTransformation = sourceTransformations[srcType] || {};
let src = cld.url(publicId, Util.defaults({}, sourceTransformation, childTransformations, {resource_type: 'video', format: srcType}));
let mimeType = 'video/' + (srcType === 'ogv' ? 'ogg' : srcType);
return <source key={mimeType} src={src} type={mimeType}/>;
}
);
} else {
let sourceTransformation = sourceTransformations[sourceTypes] || {};
tagAttributes.src = cld.url(publicId, Util.defaults({}, sourceTransformation, childTransformations, {resource_type: 'video', format: sourceTypes}));
}
return (
<video ref={innerRef} {...tagAttributes}>
{sources}
{fallback}
{this.props.children}
</video>
);
}
}
Video.propTypes = {publicId: PropTypes.string};
Video.defaultProps = {};
Video.contextTypes = CloudinaryComponent.contextTypes;
export default Video;