Skip to content

Instantly share code, notes, and snippets.

@msalahz
Created April 29, 2015 15:32
Show Gist options
  • Save msalahz/f1b698421d922bb83418 to your computer and use it in GitHub Desktop.
Save msalahz/f1b698421d922bb83418 to your computer and use it in GitHub Desktop.
Ember Video Uploader Component
import Ember from 'ember';
import EmberUploader from 'ember-uploader';
export default EmberUploader.FileField.extend({
// restrict video upload only
accept: "video/*",
// video property default value
videoFile: null,
// videoUrl property default value
videoUrl: null,
// videoType property default value
videoType: null,
// videoType property default value
videoExt: null,
// videoName property default value
videoName: null,
// videoDate property default value
videoDate: null,
// videoDateWithType property default value
videoDateWithoutType: null,
// on file-upload value change
filesDidChange: (function () {
// get component reference
let component = this,
// get thumbnail reference and default thumbnail path
data = this.getProperties('video', 'files');
// get uploaded videos list
// if there are uploaded videos
if (!Ember.isEmpty(data.files) && data.files.length === 1 && data.files[0].type.indexOf('video/') > -1) {
// get uploaded video base64 binary string
this.fileToBinary(data.files[0], (function () {
return function (e) {
// set video with uploaded video and video name
component.setProperties({
videoFile: data.files[0],
videoType: data.files[0].type,
videoExt: data.files[0].name.split('.').pop(),
videoDate: e.target.result,
videoDateWithoutType: e.target.result.split(',')[1],
videoName: data.files[0].name
});
};
})(data.files[0]));
// get uploaded video client url
this.fileToArrayBuffer(data.files[0], (function () {
return function (e) {
let bolb = URL.createObjectURL(new Blob([e.target.result], {type: data.files[0].type}));
// set video with uploaded video and video name
component.set('videoUrl', bolb);
};
})(data.files[0]));
} else {
component.setProperties({
videoFile: null,
videoType: null,
videoUrl: null,
videoExt: null,
videoDate: null,
videoDateWithoutType: null,
videoName: null
});
this.notifier.error("Invalid Video.");
//this.set('files', []);
}
}).observes('files'),
// this function responsible convert form data file to base64 string
fileToBinary(photo, onLoad) {
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = onLoad;
// Read in the video file as a data URL.
reader.readAsDataURL(photo);
},
// this function responsible convert form data file to base64 string
fileToArrayBuffer(photo, onLoad) {
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = onLoad;
// Read in the video file as a data URL.
reader.readAsArrayBuffer(photo);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment