-
Notifications
You must be signed in to change notification settings - Fork 17
/
resumableupload2_js.min.js
1 lines (1 loc) · 2.91 KB
/
resumableupload2_js.min.js
1
(function(e){let t;t=function(){function t(){this.obj={},this.chunkSize=52428800,this.endpoint="https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&supportsAllDrives=true"}t.prototype.Do=async function(e,t){t({status:"initialize"},null);try{this.obj=await n.call(this,e)}catch(e){return void t(null,e)}try{const n=await i.call(this);this.location=n.get("location"),t({status:"getLocation"},null),t({status:"start"},null);const s=e.file.size,r=Math.ceil(s/this.chunkSize);for(let n=0;n<r;n++){let i=n*this.chunkSize,a=s<i+this.chunkSize?s:i+this.chunkSize,l=e.file.slice(i,a);a-=1,t({status:"Uploading",progressNumber:{current:n,end:r},progressByte:{current:i,end:a,total:s}},null);try{const e=await o.call(this,{fileSize:s,len:r,start:i,end:a,data:l,i:n});if("Next"!=e.status&&("Done"!=e.status||n!=r-1))return void t(null,"Internal error.");t(e,null)}catch(e){return void t(null,e)}}}catch(e){return void t(null,e)}};const n=function(e){return new Promise((t,n)=>{if(!("accessToken"in e&&"file"in e))return void n({Error:"There are no required parameters. accessToken, fileName, fileSize, fileType and fileBuffer are required."});let i={};i.resource=e,"chunkSize"in e&&e.chunkSize>=262144&&e.chunkSize%262144==0&&(this.chunkSize=e.chunkSize),"fields"in e&&""!=e.fields&&(this.endpoint+="&fields="+encodeURIComponent(e.fields)),"convertToGoogleDocs"in e&&e.convertToGoogleDocs?fetch("https://www.googleapis.com/drive/v3/about?fields=importFormats",{method:"GET",headers:{Authorization:"Bearer "+e.accessToken}}).then(o=>{200==o.status?o.json().then(n=>{e.file.type in n.importFormats&&(i.resource.fileType=n.importFormats[e.fileType][0]),t(i)}):o.json().then(e=>n(e))}).catch(e=>{n(e)}):t(i)})},i=function(){return new Promise((e,t)=>{const n=this.obj.resource,i=n.accessToken;let o={mimeType:n.file.type,name:n.file.name};"folderId"in n&&""!=n.folderId&&(o.parents=[n.folderId]),fetch(this.endpoint,{method:"POST",body:JSON.stringify(o),headers:{Authorization:"Bearer "+i,"Content-Type":"application/json"}}).then(n=>{200==n.status?e(n.headers):n.json().then(e=>t(e))}).catch(e=>{t(e)})})},o=function({fileSize:e,len:t,start:n,end:i,data:o,i:r}){const a=this.location;return new Promise(function(l,c){const u=new FileReader;u.onload=async function(){const o=u.result,h={data:new Uint8Array(o),length:i-n+1,range:"bytes "+n+"-"+i+"/"+e,startByte:n,endByte:i,total:e,cnt:r,totalChunkNumber:t};await s(h,a).then(e=>l(e)).catch(e=>c(e))},u.readAsArrayBuffer(o)})},s=function(t,n){return new Promise(function(i,o){fetch(n,{method:"PUT",body:t.data,headers:{"Content-Range":t.range}}).then(t=>{const n=t.status;if(308==n)i({status:"Next",result:e});else{if(200!=n)return void t.json().then(e=>{e.additionalInformation="When the file size is large, there is the case that the file cannot be converted to Google Docs. Please be careful this.",o(e)});t.json().then(e=>i({status:"Done",result:e}))}}).catch(e=>{o(e)})})};return t}(),e.ResumableUploadToGoogleDrive2=t})(this);