-
Notifications
You must be signed in to change notification settings - Fork 8
/
ooyala_uploader.min.js
25 lines (25 loc) · 12.2 KB
/
ooyala_uploader.min.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
(function(){var n,p,m,d=function(a,b){return function(){return a.apply(b,arguments)}},q=[].indexOf||function(a){for(var b=0,e=this.length;b<e;b++)if(b in this&&this[b]===a)return b;return-1},c=window,g=function(a){var b,e;null==a&&(a={});this.uploadFileUsingFlash=d(this.uploadFileUsingFlash,this);this.uploadError=d(this.uploadError,this);this.uploadComplete=d(this.uploadComplete,this);this.uploadProgress=d(this.uploadProgress,this);this.embedCodeReady=d(this.embedCodeReady,this);this.uploadFile=d(this.uploadFile,
this);this.off=d(this.off,this);this.on=d(this.on,this);this.chunkProgress={};this.eventListeners={};this.initializeListeners(a);this.uploaderType=null!=(b=null!=a?a.uploaderType:void 0)?b:"HTML5";if("Flash"!==(e=this.uploaderType)&&"HTML5"!==e)throw"uploaderType must be either HTML5 or Flash";if("Flash"===this.uploaderType){if(null==(null!=a?a.swfUploader:void 0))throw Error("a reference to the SWFUpload object is required for Flash uploads");this.swfUploader=a.swfUploader}};g.prototype.initializeListeners=
function(a){var b,e,h,c,f,d;f=["embedCodeReady","uploadProgress","uploadComplete","uploadError"];d=[];h=0;for(c=f.length;h<c;h++)b=f[h],e=null!=a[b]?a[b]instanceof Array?a[b]:[a[b]]:[],d.push(this.eventListeners[b]=e);return d};g.prototype.on=function(a,b){if(null==this.eventListeners[a])throw Error("invalid eventType");return this.eventListeners[a].push(b)};g.prototype.off=function(a,b){var e,h,c;null==b&&(b=null);if(null==b)this.eventListeners[a]=[];else{h=this.eventListeners[a];for(c=[];0<=(e=
h.indexOf(b));)c.push(h.splice(e,1));return c}};g.prototype.uploadFile=function(a,b){null==b&&(b={});if(!this.html5UploadSupported)return!1;(new m({embedCodeReady:this.embedCodeReady,uploadProgress:this.uploadProgress,uploadComplete:this.uploadComplete,uploadError:this.uploadError,uploaderType:this.uploaderType})).uploadFile(a,b);return!0};g.prototype.embedCodeReady=function(a){var b,e,h,c,f;c=null!=(b=this.eventListeners.embedCodeReady)?b:[];f=[];e=0;for(h=c.length;e<h;e++)b=c[e],f.push(b(a));return f};
g.prototype.uploadProgress=function(a,b){var e,h,c,f,d;if(b!==this.chunkProgress[a]){this.chunkProgress[a]=b;f=null!=(e=this.eventListeners.uploadProgress)?e:[];d=[];h=0;for(c=f.length;h<c;h++)e=f[h],d.push(e(a,b));return d}};g.prototype.uploadComplete=function(a){var b,e,h,c,d;delete this.chunkProgress[a];c=null!=(b=this.eventListeners.uploadComplete)?b:[];d=[];e=0;for(h=c.length;e<h;e++)b=c[e],d.push(b(a));return d};g.prototype.uploadError=function(a,b,e,c,d){var f,g,l,j,k;j=null!=(f=this.eventListeners.uploadError)?
f:[];k=[];g=0;for(l=j.length;g<l;g++)f=j[g],k.push(f(a,b,e,c,d));return k};g.prototype.uploadFileUsingFlash=function(a){null==a&&(a={});if("Flash"!==this.uploaderType)throw Error("uploaderType must be Flash to call this method");(new m({embedCodeReady:this.embedCodeReady,uploadProgress:this.uploadProgress,uploadComplete:this.uploadComplete,uploadError:this.uploadError,uploaderType:this.uploaderType,swfUploader:this.swfUploader})).uploadFileUsingFlash(a);return!0};g.prototype.html5UploadSupported=
"undefined"!==typeof FileReader&&null!==FileReader;c.OoyalaUploader=g;c=function(a){this.onError=d(this.onError,this);this.onAssetUploadComplete=d(this.onAssetUploadComplete,this);this.onChunkComplete=d(this.onChunkComplete,this);this.onChunkProgress=d(this.onChunkProgress,this);this.onFlashUploadError=d(this.onFlashUploadError,this);this.onFlashUploadComplete=d(this.onFlashUploadComplete,this);this.onFlashUploadProgress=d(this.onFlashUploadProgress,this);this.startFlashUpload=d(this.startFlashUpload,
this);this.startHTML5Upload=d(this.startHTML5Upload,this);this.onUploadUrlsReceived=d(this.onUploadUrlsReceived,this);this.onAssetCreated=d(this.onAssetCreated,this);this.createAsset=d(this.createAsset,this);this.setAssetMetadata=d(this.setAssetMetadata,this);this.uploadFileUsingFlash=d(this.uploadFileUsingFlash,this);this.uploadFile=d(this.uploadFile,this);var b,e,c,g,f;this.embedCodeReadyCallback=null!=(b=null!=a?a.embedCodeReady:void 0)?b:function(){};this.uploadProgressCallback=null!=(e=null!=
a?a.uploadProgress:void 0)?e:function(){};this.uploadCompleteCallback=null!=(c=null!=a?a.uploadComplete:void 0)?c:function(){};this.uploadErrorCallback=null!=(g=null!=a?a.uploadError:void 0)?g:function(){};this.uploaderType=null!=(f=null!=a?a.uploaderType:void 0)?f:"HTML5";"Flash"===this.uploaderType&&(this.swfUploader=a.swfUploader);this.chunkUploaders={};this.completedChunkIndexes=[];this.completedChunks=0;this.totalChunks};c.prototype.uploadFile=function(a,b){var e;this.file=a;console.log("Uploading file using browser: "+
navigator.userAgent);this.setAssetMetadata(b);if(null==(e=this.assetMetadata).assetName)e.assetName=this.file.name;this.assetMetadata.fileSize=this.file.size;this.assetMetadata.fileName=this.file.name;return this.createAsset()};c.prototype.uploadFileUsingFlash=function(a){var b;b=this.swfUploader.getFile(0);if(null==b)throw Error("Flash Upload: No Files Queued");this.setAssetMetadata(a);if(null==(a=this.assetMetadata).assetName)a.assetName=b.name;this.assetMetadata.fileSize=b.size;this.assetMetadata.fileName=
b.name;this.swfUploader.settings.upload_success_handler=this.onFlashUploadComplete;this.swfUploader.settings.upload_progress_handler=this.onFlashUploadProgress;this.swfUploader.settings.upload_error_handler=this.onFlashUploadError;return this.createAsset()};c.prototype.setAssetMetadata=function(a){var b,e,c,d,f,g,l,j,k;return this.assetMetadata={assetCreationUrl:null!=(b=a.assetCreationUrl)?b:"/v2/assets",assetUploadingUrl:null!=(e=a.assetUploadingUrl)?e:"/v2/assets/assetID/uploading_urls",assetStatusUpdateUrl:null!=
(c=a.assetStatusUpdateUrl)?c:"/v2/assets/assetID/upload_status",assetName:a.name,assetDescription:null!=(d=a.description)?d:"",assetType:null!=(f=a.assetType)?f:"video",createdAt:(new Date).getTime(),assetLabels:null!=(g=a.labels)?g:[],postProcessingStatus:null!=(l=a.postProcessingStatus)?l:"live",labelCreationUrl:null!=(j=a.labelCreationUrl)?j:"/v2/labels/by_full_path/paths",labelAssignmentUrl:null!=(k=a.labelAssignmentUrl)?k:"/v2/assets/assetID/labels",assetID:""}};c.prototype.createAsset=function(){var a,
b=this;a={name:this.assetMetadata.assetName,description:this.assetMetadata.assetDescription,file_name:this.assetMetadata.fileName,file_size:this.assetMetadata.fileSize,asset_type:this.assetMetadata.assetType,post_processing_status:this.assetMetadata.postProcessingStatus};"HTML5"===this.uploaderType&&(a.chunk_size=5242880);return jQuery.ajax({url:this.assetMetadata.assetCreationUrl,type:"POST",data:a,success:function(a){return b.onAssetCreated(a)},error:function(a){return b.onError(a,"Asset creation error")}})};
c.prototype.onAssetCreated=function(a){a=JSON.parse(a);this.assetMetadata.assetID=a.embed_code;this.embedCodeReadyCallback(this.assetMetadata.assetID);this.assetMetadata.assetLabels.filter(function(a){return a});0!==this.assetMetadata.assetLabels.length&&this.createLabels();return this.getUploadingUrls()};c.prototype.createLabels=function(){var a,b=this;a=this.assetMetadata.assetLabels.join(",");return jQuery.ajax({url:this.assetMetadata.labelCreationUrl.replace("paths",a),type:"POST",success:function(a){return b.assignLabels(a)},
error:function(a){return b.onError(a,"Label creation error")}})};c.prototype.assignLabels=function(a){var b,e=this;b=JSON.parse(a);var c,d,f;f=[];c=0;for(d=b.length;c<d;c++)a=b[c],f.push(a.id);return jQuery.ajax({url:this.assetMetadata.labelAssignmentUrl.replace("assetID",this.assetMetadata.assetID),type:"POST",data:JSON.stringify(f),success:function(a){return e.onLabelsAssigned(a)},error:function(a){return e.onError(a,"Label assignment error")}})};c.prototype.onLabelsAssigned=function(){return console.log("Creation and assignment of labels complete "+
this.assetMetadata.assetLabels)};c.prototype.getUploadingUrls=function(){var a=this;return jQuery.ajax({url:this.assetMetadata.assetUploadingUrl.split("assetID").join(this.assetMetadata.assetID),data:{asset_id:this.assetMetadata.assetID},success:function(b){return a.onUploadUrlsReceived(b)},error:function(b){return a.onError(b,"Error getting the uploading urls")}})};c.prototype.onUploadUrlsReceived=function(a){a=JSON.parse(a);this.totalChunks=a.length;return"HTML5"===this.uploaderType?this.startHTML5Upload(a):
this.startFlashUpload(a)};c.prototype.startHTML5Upload=function(a){var b,e=this;b=(new p(this.file,5242880)).getChunks();b.length!==this.totalChunks&&console.log("Sliced chunks ("+b.length+") and uploadingUrls ("+this.totalChunks+") disagree.");return jQuery.each(b,function(b,c){var d;if(!(0<=q.call(e.completedChunkIndexes,b)))return d=new n({assetMetadata:e.assetMetadata,chunkIndex:b,chunk:c,uploadUrl:a[b],progress:e.onChunkProgress,completed:e.onChunkComplete,error:e.uploadErrorCallback}),e.chunkUploaders[b]=
d,d.startUpload()})};c.prototype.startFlashUpload=function(a){this.swfUploader.setUploadURL(a[0]);return this.swfUploader.startUpload()};c.prototype.onFlashUploadProgress=function(a,b,c){a=Math.floor(100*b/c);a=Math.min(100,a);return this.uploadProgressCallback(this.assetMetadata.assetID,a)};c.prototype.onFlashUploadComplete=function(){return this.onAssetUploadComplete()};c.prototype.onFlashUploadError=function(a,b,c){return this.uploadErrorCallback({assetID:this.assetMetadata.assetID,type:this.assetMetadata.assetType,
fileName:this.assetMetadata.assetName,statusCode:b,message:c})};c.prototype.progressPercent=function(){var a,b,c,d;a=0;d=this.chunkUploaders;for(b in d)c=d[b],a+=c.bytesUploaded;return Math.min(100,Math.floor(100*(5242880*this.completedChunks+a)/this.assetMetadata.fileSize))};c.prototype.onChunkProgress=function(){return this.uploadProgressCallback(this.assetMetadata.assetID,this.progressPercent())};c.prototype.onChunkComplete=function(a,b){this.completedChunks++;this.completedChunkIndexes.push(b);
delete this.chunkUploaders[b];this.onChunkProgress();if(this.completedChunks===this.totalChunks)return this.onAssetUploadComplete()};c.prototype.onAssetUploadComplete=function(){var a=this;return jQuery.ajax({url:this.assetMetadata.assetStatusUpdateUrl.split("assetID").join(this.assetMetadata.assetID),data:{asset_id:this.assetMetadata.assetID,status:"uploaded"},type:"PUT",success:function(){return a.uploadCompleteCallback(a.assetMetadata.assetID)},error:function(b){return a.onError(b,"Setting asset status as uploaded error")}})};
c.prototype.onError=function(a,b){var c,d;try{d=JSON.parse(a.responseText),c=d.message}catch(g){c=a.statusText}console.log(""+this.assetMetadata.assetName+": "+b+" with status "+a.status+": "+c);return this.uploadErrorCallback({assetID:this.assetMetadata.assetID,type:this.assetMetadata.assetType,fileName:this.assetMetadata.assetName,statusCode:a.status,message:""+b+", "+c})};m=c;c=function(a){this.onXhrError=d(this.onXhrError,this);this.onXhrLoad=d(this.onXhrLoad,this);this.startUpload=d(this.startUpload,
this);this.assetMetadata=a.assetMetadata;this.chunk=a.chunk;this.chunkIndex=a.chunkIndex;this.progressHandler=a.progress;this.completedHandler=a.completed;this.uploadErrorCallback=a.error;this.uploadUrl=a.uploadUrl;this.bytesUploaded=0};c.prototype.startUpload=function(){var a=this;console.log(""+this.assetMetadata.assetID+": Starting upload of chunk "+this.chunkIndex);this.xhr=new XMLHttpRequest;this.xhr.upload.addEventListener("progress",function(b){a.bytesUploaded=b.loaded;return a.progressHandler()});
this.xhr.addEventListener("load",this.onXhrLoad);this.xhr.addEventListener("error",this.onXhrError);this.xhr.open("PUT",this.uploadUrl);return this.xhr.send(this.chunk)};c.prototype.onXhrLoad=function(a){if(400<=a.target.status)return onXhrError(a);this.bytesUploaded=5242880;return this.completedHandler(a,this.chunkIndex)};c.prototype.onXhrError=function(a){console.log(""+this.assetMetadata.assetID+": chunk "+this.chunkIndex+": Xhr Error Status "+a.target.status);return this.uploadErrorCallback({assetID:this.assetMetadata.assetID,
type:this.assetMetadata.assetType,fileName:this.assetMetadata.assetName,statusCode:a.status,message:a.responseText})};n=c;c=function(a,b){this.file=a;this.chunkSize=b};c.prototype.getChunks=function(){var a,b,c,d;if(!this.file.slice&&!this.file.mozSlice)return[this.file];d=[];a=b=0;for(c=Math.ceil(this.file.size/this.chunkSize);0<=c?b<c:b>c;a=0<=c?++b:--b)d.push(this.slice(a*this.chunkSize,(a+1)*this.chunkSize));return d};c.prototype.slice=function(a,b){if(this.file.slice)return this.file.slice(a,
b);if(this.file.mozSlice)return this.file.mozSlice(a,b)};p=c}).call(this);