mirror of
https://github.com/antos-rde/antosdk-apps.git
synced 2024-12-27 12:38:22 +01:00
1 line
9.1 KiB
JavaScript
1 line
9.1 KiB
JavaScript
var OS;!function(e){let t;!function(t){let i;!function(i){let a={"gdv://":{id:"root",mime:"dir"}};class r extends i.BaseFileHandle{constructor(t){super(t),r.API_META?(this.isRoot()&&(this.gid="root"),this.cache="",this.local_copy=void 0):e.announcer.oserror(__("Unknown API setting for GAPI"),e.API.throwe("OS.VFS"))}fields(){return"webContentLink, id, name,mimeType,description, kind, parents, properties, iconLink, createdTime, modifiedTime, owners, permissions, fullFileExtension, fileExtension, size, version"}isFolder(){return"application/vnd.google-apps.folder"===this.info.mimeType}load(e){const i=t.mid();return new Promise(async(a,r)=>{t.loading(i,"GAPI");try{let r=await e;return t.loaded(i,"GAPI","OK"),a(r)}catch(e){return t.loaded(i,"GAPI","FAIL"),r(__e(e))}})}sync(t){return new Promise(async(a,r)=>{try{if(this.info&&this.info.version==t.version||"application/vnd.google-apps.folder"===t.mimeType)a(!0);else{await i.mkdirAll(["home://.gdv_cache","home://.gdv_cache/"+t.id],!0);let e=`home://.gdv_cache/${t.id}/${t.version}.${t.fullFileExtension}`.asFileHandle();try{await e.onready()}catch(a){await("home://.gdv_cache/"+t.id).asFileHandle().remove(),await i.mkdirAll(["home://.gdv_cache/"+t.id]);let r=await this.load(gapi.client.drive.files.get({fileId:t.id,alt:"media"}));if(!r.body)throw new Error(__("VFS cannot download file : {0}",this.path).__());e.cache=new Blob([r.body.asUint8Array()],{type:"octet/stream"}),await e.write(t.mimeType)}this.local_copy=e,a(!0)}}catch(t){e.announcer.oserror(t.toString(),t),r(__e(t))}})}meta(){return new Promise(async(t,i)=>{try{if(await this.oninit(),a[this.path]&&(this.gid=a[this.path].id),this.gid){let e=await this.load(gapi.client.drive.files.get({fileId:this.gid,fields:this.fields()}));if(!e.result)throw new Error(__("VFS cannot get meta data for {0}",this.gid).__());e.result.mime=e.result.mimeType,await this.load(this.sync(e.result)),t(e)}else{const e=this.parent().asFileHandle(),i=(await e.meta()).result;a[e.path]={id:i.id,mime:i.mimeType};let r=await this.load(gapi.client.drive.files.list({q:`name = '${this.basename}' and '${i.id}' in parents and trashed = false`,fields:`files(${this.fields()})`}));if(!(r.result.files&&r.result.files.length>0))throw new Error(__("VFS cannot get meta data for {0}",this.path).__());a[this.path]={id:r.result.files[0].id,mime:r.result.files[0].mimeType},r.result.files[0].mime=r.result.files[0].mimeType,this.gid=a[this.path].id,await this.load(this.sync(r.result.files[0])),t({result:r.result.files[0],error:!1})}}catch(t){e.announcer.oserror(t.toString(),t),i(__e(t))}})}oninit(){return new Promise(async(i,n)=>{const o=async function(e){if(e)return i(!0);a={"gdv://":{id:"root",mime:"dir"}};try{let e=await gapi.auth2.getAuthInstance().signIn();i(e)}catch(e){n(__e(e))}};try{if(!r.API_META)throw new Error(__("No GAPI meta found").__());if(t.libready(r.API_META.apilink))gapi.auth2.getAuthInstance().isSignedIn.listen(e=>o(e)),o(gapi.auth2.getAuthInstance().isSignedIn.get());else{if(await this.load(t.requires(r.API_META.apilink,!1)),await this.load(new Promise((e,t)=>{gapi.load("client:auth2",e)})),await this.load(gapi.client.init({apiKey:r.API_META.API_KEY,clientId:r.API_META.CLIENT_ID,discoveryDocs:r.API_META.DISCOVERY_DOCS,scope:r.API_META.SCOPES})),gapi.auth2.getAuthInstance().isSignedIn.listen(e=>o(e)),!await e.GUI.openDialog("YesNoDialog",{title:__("Authentication"),text:__("Would you like to login to GoogleDrive?")}))throw new Error(__("User abort the authentication").__());o(gapi.auth2.getAuthInstance().isSignedIn.get())}}catch(t){e.announcer.oserror(t.toString(),t),n(__e(t))}})}getlink(){if(this.local_copy)return this.local_copy.getlink()}child(e){if(this.isFolder())return`${this.path}/${e}`}_rd(t){return new Promise(async(i,r)=>{try{if(!this.info.id)throw new Error(__("File ID is not valid").__());if(this.isFolder()){let e=await this.load(gapi.client.drive.files.list({q:`'${this.info.id}' in parents and trashed = false`,fields:`files(${this.fields()})`}));if(!e.result.files)throw new Error(__("File {0} not found",this.info.id).__());for(let t of e.result.files)t.path=this.child(t.name),t.mime=t.mimeType,t.filename=t.name,t.type="file",t.gid=t.id,"application/vnd.google-apps.folder"===t.mimeType&&(t.mime="dir",t.type="dir",t.size=0),a[t.path]={id:t.gid,mime:t.mime};i({result:e.result.files,error:!1})}else{if(!this.local_copy)throw new Error(__("Cannot find local copy of file; {0}",this.path).__());i(await this.local_copy.read(t))}}catch(t){e.announcer.oserror(t.toString(),t),r(__e(t))}})}save(t,i){return new Promise(async(a,n)=>{try{const o=gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse().access_token,s=new XMLHttpRequest,l=__(r.API_META.uploadlink,t).__();s.open("PATCH",l),s.setRequestHeader("Authorization","Bearer "+o),s.setRequestHeader("Content-Type",i),s.setRequestHeader("Content-Encoding","base64"),s.setRequestHeader("Content-Transfer-Encoding","base64");let d=t=>(e.announcer.oserror(__("VFS cannot save : {0}",this.path),t),n(t));s.onreadystatechange=()=>{if(4===s.readyState){if(200===s.status)return a({result:JSON.parse(s.responseText),error:!1});d(e.API.throwe("OS.VFS"))}},s.onerror=()=>d(e.API.throwe("OS.VFS"));let c=this.cache;"base64"!==i&&(c=await this.b64(i)),s.send(c.replace(/^data:[^;]+;base64,/g,"")),a(!0)}catch(e){n(__e(e))}})}_wr(t,i){return new Promise(async(i,r)=>{try{var n=void 0;if(a[this.path]&&(n=a[this.path].id),n)i(await this.load(this.save(n,t)));else{const e=this.parent().asFileHandle();await e.onready();const r={name:this.basename,mimeType:t,parents:[e.info.id]};let n=await this.load(gapi.client.drive.files.create({resource:r,fields:"id"}));if(!n||!n.result)throw new Error(__("VFS cannot write : {0}",this.path).__());a[this.path]={id:n.result.id,mime:t},i(this.load(this.save(n.result.id,t)))}}catch(t){e.announcer.oserror(t.toString(),t),r(__e(t))}})}_mk(t){return new Promise(async(i,r)=>{try{if(!this.isFolder())throw new Error(__("{0} is not a directory",this.path).__());var n={name:t,parents:[this.info.id],mimeType:"application/vnd.google-apps.folder"};let e=await this.load(gapi.client.drive.files.create({resource:n,fields:"id"}));if(!e||!e.result)throw new Error(__("VFS cannot create : {0}",t).__());a[this.child(t)]={id:e.result.id,mime:"dir"},i(e)}catch(t){e.announcer.oserror(t.toString(),t),r(__e(t))}})}_rm(){return new Promise(async(t,i)=>{try{if(!this.info.id)throw new Error(__("Cannot identify file id of {0}",this.path).__());if(!await this.load(gapi.client.drive.files.delete({fileId:this.info.id})))throw new Error(__("VFS cannot delete : {0}",this.path).__());a[this.path]=null,delete a[this.path],t({result:!0,error:!1})}catch(t){e.announcer.oserror(t.toString(),t),i(__e(t))}})}_mv(t){return new Promise(async(i,a)=>{try{var r=t.asFileHandle().parent().asFileHandle();await r.onready();const e=this.info.parents.join(",");let a=await this.load(gapi.client.drive.files.update({fileId:this.info.id,addParents:r.info.id,removeParents:e,fields:"id"}));if(!a)throw new Error(__("VFS cannot move : {0}",this.path).__());i(a)}catch(t){e.announcer.oserror(t.toString(),t),a(__e(t))}})}_up(){return new Promise(async(t,i)=>{try{if(!this.isFolder())throw new Error(__("Target file should be a folder").__());var a=$("<input>").attr("type","file").css("display","none");a.on("change",async()=>{const e=a[0].files[0],t=this.child(e.name).asFileHandle();return t.cache=e,await this.load(t.write(e.type)),a.remove()}),a.trigger("click")}catch(t){e.announcer.oserror(t.toString(),t),i(__e(t))}})}_down(){return new Promise(async(t,i)=>{try{let i=await this.load(gapi.client.drive.files.get({fileId:this.info.id,alt:"media"}));if(!i.body)throw new Error(__("VFS cannot download file : {0}",this.path).__());let a=[];for(let e=0,t=i.body.length-1,r=0<=t;r?e<=t:e>=t;r?e++:e--)a.push(i.body.charCodeAt(e));let r=new Uint8Array(a);const n=new Blob([r],{type:"octet/stream"});e.API.saveblob(this.basename,n),t(!0)}catch(t){e.announcer.oserror(t.toString(),t),i(__e(t))}})}}i.GoogleDriveHandle=r,r.API_META={CLIENT_ID:"1006507170703-l322pfkrhf9cgta4l4jh2p8ughtc14id.apps.googleusercontent.com",API_KEY:"AIzaSyBZhM5KbARvT10acWC8JQKlRn2WbSsmfLc",apilink:"https://apis.google.com/js/api.js",DISCOVERY_DOCS:["https://www.googleapis.com/discovery/v1/apis/drive/v3/rest"],SCOPES:"https://www.googleapis.com/auth/drive",uploadlink:"https://www.googleapis.com/upload/drive/v3/files/{0}?uploadType=media",logout:"https://www.google.com/accounts/Logout"},i.register("^gdv$",r),t.onsearch("Google Drive",(function(e){const t=[],i=new RegExp(e,"i");for(let e in a){const r=a[e];if(e.match(i)||r&&r.mime.match(i)){const i=e.asFileHandle();i.text=i.basename,i.mime=r.mime,i.iconclass="fa fa-file","dir"===i.mime&&(i.iconclass="fa fa-folder"),i.complex=!0,i.detail=[{text:i.path}],t.push(i)}}return t})),e.onexit("cleanUpGoogleDrive",(function(){return new Promise(async(e,t)=>{try{if(await"home://.gdv_cache".asFileHandle().remove(),a={"gdv://":{id:"root",mime:"dir"}},!Ant.OS.API.libready(Ant.OS.setting.VFS.gdrive.apilink))return e(!0);const t=gapi.auth2.getAuthInstance();if(!t)throw new Error(__("Unable to get OATH instance").__());t.isSignedIn.get()&&$("<iframe/>",{src:r.API_META.logout,frameborder:0,onload:()=>t.disconnect()}),e(!0)}catch(t){console.log(t),e(!0)}})}))}(i=t.VFS||(t.VFS={}))}(t=e.API||(e.API={}))}(OS||(OS={})); |