fix loading googledrive

This commit is contained in:
Xuan Sang LE 2018-03-01 12:00:56 +01:00
parent 8589c2be9a
commit 0590e284a0
3 changed files with 43 additions and 7 deletions

View File

@ -52,7 +52,7 @@ class BaseFileHandler
sendB64: (m, f) -> sendB64: (m, f) ->
me = @ me = @
return unless @cache return f "" unless @cache
if typeof @cache is "string" if typeof @cache is "string"
b64 = @cache.asBase64() b64 = @cache.asBase64()
b64 = "data:#{m};base64,#{b64}" b64 = "data:#{m};base64,#{b64}"

View File

@ -27,7 +27,9 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler
else else
_API.require @setting.apilink, () -> _API.require @setting.apilink, () ->
# avoid popup block # avoid popup block
q = _courrier.getMID()
gapi.load "client:auth2", () -> gapi.load "client:auth2", () ->
_API.loading q, "GAPI"
gapi.client.init { gapi.client.init {
apiKey: me.setting.API_KEY, apiKey: me.setting.API_KEY,
clientId: me.setting.CLIENT_ID, clientId: me.setting.CLIENT_ID,
@ -35,6 +37,7 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler
scope: me.setting.SCOPES scope: me.setting.SCOPES
} }
.then () -> .then () ->
_API.loaded q, "OK"
gapi.auth2.getAuthInstance().isSignedIn.listen (r) -> gapi.auth2.getAuthInstance().isSignedIn.listen (r) ->
fn(r) fn(r)
_GUI.openDialog "YesNoDialog", (d) -> _GUI.openDialog "YesNoDialog", (d) ->
@ -42,28 +45,35 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler
fn(gapi.auth2.getAuthInstance().isSignedIn.get()) fn(gapi.auth2.getAuthInstance().isSignedIn.get())
, "Authentification", { text: "Do you want to login to Google Drive ?" } , "Authentification", { text: "Do you want to login to Google Drive ?" }
.catch (err) -> .catch (err) ->
_API.loaded q, "FAIL"
_courrier.oserror "VFS cannot init GAPI: #{err.error}", (_API.throwe "OS.VFS"), err _courrier.oserror "VFS cannot init GAPI: #{err.error}", (_API.throwe "OS.VFS"), err
meta: (f) -> meta: (f) ->
me = @ me = @
@oninit () -> @oninit () ->
q = _courrier.getMID()
me.gid = G_CACHE[me.path] if G_CACHE[me.path] me.gid = G_CACHE[me.path] if G_CACHE[me.path]
if me.gid if me.gid
#console.log "Gid exists ", me.gid #console.log "Gid exists ", me.gid
_API.loading q, "GAPI"
gapi.client.drive.files.get { gapi.client.drive.files.get {
fileId: me.gid, fileId: me.gid,
fields: me.fields() fields: me.fields()
} }
.then (r) -> .then (r) ->
_API.loaded q, "OK"
return unless r.result return unless r.result
f(r) f(r)
.catch (err) -> .catch (err) ->
_API.loaded q, "FAIL"
_courrier.oserror "VFS cannot get meta #{me.gid}", (_API.throwe "OS.VFS"), err _courrier.oserror "VFS cannot get meta #{me.gid}", (_API.throwe "OS.VFS"), err
else else
#console.log "Find file in ", me.parent() #console.log "Find file in ", me.parent()
fp = me.parent().asFileHandler() fp = me.parent().asFileHandler()
fp.meta (d) -> fp.meta (d) ->
file = d.result file = d.result
q1 = _courrier.getMID()
_API.loading q1, "GAPI"
G_CACHE[fp.path] = file.id G_CACHE[fp.path] = file.id
gapi.client.drive.files.list { gapi.client.drive.files.list {
q: "name = '#{me.basename}' and '#{file.id}' in parents and trashed = false", q: "name = '#{me.basename}' and '#{file.id}' in parents and trashed = false",
@ -71,10 +81,12 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler
} }
.then (r) -> .then (r) ->
#console.log r #console.log r
_API.loaded q1, "OK"
return unless r.result.files and r.result.files.length > 0 return unless r.result.files and r.result.files.length > 0
G_CACHE[me.path] = r.result.files[0].id G_CACHE[me.path] = r.result.files[0].id
f { result: r.result.files[0] } f { result: r.result.files[0] }
.catch (err) -> .catch (err) ->
_API.loaded q1, "FAIL"
_courrier.oserror "VFS cannot get meta #{me.path}", (_API.throwe "OS.VFS"), err _courrier.oserror "VFS cannot get meta #{me.path}", (_API.throwe "OS.VFS"), err
fields: () -> fields: () ->
@ -86,7 +98,7 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler
me = @ me = @
user = gapi.auth2.getAuthInstance().currentUser.get() user = gapi.auth2.getAuthInstance().currentUser.get()
oauthToken = user.getAuthResponse().access_token oauthToken = user.getAuthResponse().access_token
q = _courrier.getMID()
xhr = new XMLHttpRequest() xhr = new XMLHttpRequest()
url = 'https://www.googleapis.com/upload/drive/v3/files/' + id + '?uploadType=media' url = 'https://www.googleapis.com/upload/drive/v3/files/' + id + '?uploadType=media'
xhr.open('PATCH', url) xhr.open('PATCH', url)
@ -94,11 +106,14 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler
xhr.setRequestHeader 'Content-Type', m xhr.setRequestHeader 'Content-Type', m
xhr.setRequestHeader 'Content-Encoding', 'base64' xhr.setRequestHeader 'Content-Encoding', 'base64'
xhr.setRequestHeader 'Content-Transfer-Encoding', 'base64' xhr.setRequestHeader 'Content-Transfer-Encoding', 'base64'
_API.loading q, "GAPI"
error = (e, s) -> error = (e, s) ->
_API.loaded q, "FAIL"
_courrier.oserror "VFS cannot save : #{me.path}", e, s _courrier.oserror "VFS cannot save : #{me.path}", e, s
xhr.onreadystatechange = () -> xhr.onreadystatechange = () ->
if ( xhr.readyState == 4 ) if ( xhr.readyState == 4 )
if ( xhr.status == 200 ) if ( xhr.status == 200 )
_API.loaded q, "OK"
f { result: JSON.parse(xhr.responseText) } f { result: JSON.parse(xhr.responseText) }
else else
error xhr, xhr.status error xhr, xhr.status
@ -110,6 +125,8 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler
action: (n, p, f) -> action: (n, p, f) ->
me = @ me = @
q = _courrier.getMID()
_API.loading q, "GAPI"
switch n switch n
when "read" when "read"
return unless @info.id return unless @info.id
@ -119,6 +136,7 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler
fields: "files(#{me.fields()})" fields: "files(#{me.fields()})"
} }
.then (r) -> .then (r) ->
_API.loaded q, "OK"
return unless r.result.files return unless r.result.files
for file in r.result.files for file in r.result.files
file.path = me.child file.name file.path = me.child file.name
@ -132,6 +150,7 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler
file.size = 0 file.size = 0
f { result: r.result.files } f { result: r.result.files }
.catch (err) -> .catch (err) ->
_API.loaded q, "FAIL"
_courrier.oserror "VFS cannot read #{me.path}", (_API.throwe "OS.VFS"), err _courrier.oserror "VFS cannot read #{me.path}", (_API.throwe "OS.VFS"), err
else else
gapi.client.drive.files.get { gapi.client.drive.files.get {
@ -139,8 +158,10 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler
alt: 'media' alt: 'media'
} }
.then (r) -> .then (r) ->
_API.loaded q, "OK"
f r.body f r.body
.catch(err) -> .catch (err) ->
_API.loaded q, "FAIL"
_courrier.oserror "VFS cannot get read #{me.path}", (_API.throwe "OS.VFS"), err _courrier.oserror "VFS cannot get read #{me.path}", (_API.throwe "OS.VFS"), err
when "mk" when "mk"
@ -155,11 +176,13 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler
fields: 'id' fields: 'id'
} }
.then (r) -> .then (r) ->
_API.loaded q, "OK"
#console.log r #console.log r
return _courrier.oserror "VFS cannot create : #{p}", (_API.throwe "OS.VFS"), r unless r and r.result return _courrier.oserror "VFS cannot create : #{p}", (_API.throwe "OS.VFS"), r unless r and r.result
G_CACHE[me.child p] = r.result.id G_CACHE[me.child p] = r.result.id
f r f r
.catch (err) -> .catch (err) ->
_API.loaded q, "FAIL"
_courrier.oserror "VFS cannot create #{p}", (_API.throwe "OS.VFS"), err _courrier.oserror "VFS cannot create #{p}", (_API.throwe "OS.VFS"), err
return return
@ -167,6 +190,7 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler
when "write" when "write"
gid = G_CACHE[me.path] gid = G_CACHE[me.path]
if gid if gid
_API.loaded q, "OK"
@save gid, p, f @save gid, p, f
else else
dir = @parent().asFileHandler() dir = @parent().asFileHandler()
@ -181,17 +205,19 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler
fields: 'id' fields: 'id'
} }
.then (r) -> .then (r) ->
_API.loaded q, "OK"
return _courrier.oserror "VFS cannot write : #{me.path}", (_API.throwe "OS.VFS"), r unless r and r.result return _courrier.oserror "VFS cannot write : #{me.path}", (_API.throwe "OS.VFS"), r unless r and r.result
G_CACHE[me.path] = r.result.id G_CACHE[me.path] = r.result.id
me.save r.result.id, p, f me.save r.result.id, p, f
.catch (err) -> .catch (err) ->
_API.loaded q, "FAIL"
_courrier.oserror "VFS cannot write #{me.path}", (_API.throwe "OS.VFS"), err _courrier.oserror "VFS cannot write #{me.path}", (_API.throwe "OS.VFS"), err
when "upload" when "upload"
return unless @isFolder() return unless @isFolder()
q = _courrier.getMID()
#insert a temporal file selector #insert a temporal file selector
o = ($ '<input>').attr('type', 'file').css("display", "none") o = ($ '<input>').attr('type', 'file').css("display", "none")
_API.loaded q, "OK"
o.change () -> o.change () ->
#_API.loading q, p #_API.loading q, p
fo = o[0].files[0] fo = o[0].files[0]
@ -212,13 +238,16 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler
} }
.then (r) -> .then (r) ->
#console.log r #console.log r
_API.loaded q, "OK"
return _courrier.oserror "VFS cannot delete : #{me.path}", (_API.throwe "OS.VFS"), r unless r return _courrier.oserror "VFS cannot delete : #{me.path}", (_API.throwe "OS.VFS"), r unless r
G_CACHE[me.path] = null G_CACHE[me.path] = null
f { result: true } f { result: true }
.catch(err) -> .catch (err) ->
_API.loaded q, "FAIL"
_courrier.oserror "VFS cannot delete #{me.path}", (_API.throwe "OS.VFS"), err _courrier.oserror "VFS cannot delete #{me.path}", (_API.throwe "OS.VFS"), err
when "publish" when "publish"
_API.loaded q, "OK"
return return
when "download" when "download"
@ -227,10 +256,12 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler
alt: 'media' alt: 'media'
} }
.then (r) -> .then (r) ->
_API.loaded q, "OK"
return _courrier.oserror "VFS cannot get file : #{me.path}", (_API.throwe "OS.VFS"), r unless r.body return _courrier.oserror "VFS cannot get file : #{me.path}", (_API.throwe "OS.VFS"), r unless r.body
blob = new Blob [r.body], { type: "octet/stream" } blob = new Blob [r.body], { type: "octet/stream" }
_API.saveblob me.basename, blob _API.saveblob me.basename, blob
.catch(err) -> .catch (err) ->
_API.loaded q, "FAIL"
_courrier.oserror "VFS cannot fetch #{me.path}", (_API.throwe "OS.VFS"), err _courrier.oserror "VFS cannot fetch #{me.path}", (_API.throwe "OS.VFS"), err
when "move" when "move"
@ -244,11 +275,16 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler
fields: "id" fields: "id"
} }
.then (r) -> .then (r) ->
_API.loaded q, "OK"
return _courrier.oserror "VFS cannot move : #{me.path}", (_API.throwe "OS.VFS"), r unless r return _courrier.oserror "VFS cannot move : #{me.path}", (_API.throwe "OS.VFS"), r unless r
f r f r
.catch (err) -> .catch (err) ->
_API.loaded q, "FAIL"
_courrier.oserror "VFS cannot move #{me.gid}", (_API.throwe "OS.VFS"), err _courrier.oserror "VFS cannot move #{me.gid}", (_API.throwe "OS.VFS"), err
, (err) ->
_API.loaded q, "FAIL"
else else
_API.loaded q, "FAIL"
return _courrier.osfail "VFS unknown action: #{n}", (_API.throwe "OS.VFS"), n return _courrier.osfail "VFS unknown action: #{n}", (_API.throwe "OS.VFS"), n

View File

@ -88,7 +88,7 @@ class Files extends this.OS.GUI.BaseApplication
chdir: (p) -> chdir: (p) ->
me = @ me = @
console.log "ch" #console.log "ch"
dir = if p then p.asFileHandler() else me.currdir dir = if p then p.asFileHandler() else me.currdir
dir.read (d) -> dir.read (d) ->
if(d.error) if(d.error)