antos-frontend/src/core/vfs/GoogleDriveHandler.coffee

108 lines
3.8 KiB
CoffeeScript
Raw Normal View History

2018-02-27 16:40:36 +01:00
2018-02-13 20:27:41 +01:00
# GoogleDrive File Handler
2018-02-27 19:39:50 +01:00
G_CACHE = {"gdv:///":"root"}
2018-02-13 20:27:41 +01:00
class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler
constructor: (path) ->
super path
2018-02-27 16:40:36 +01:00
me = @
@setting = _OS.setting.VFS.gdrive
return _courrier.oserror "Unknown API setting for google drive VFS", (_API.throwe "OS.VFS"), null unless @setting
@gid = 'root' if @isRoot()
oninit: (f) ->
me = @
return unless @setting
if _API.libready @setting.apilink
f()
else
_API.require @setting.apilink, () ->
gapi.load "client:auth2", () ->
gapi.client.init {
apiKey: me.setting.API_KEY,
clientId: me.setting.CLIENT_ID,
discoveryDocs: me.setting.DISCOVERY_DOCS,
scope: me.setting.SCOPES
}
.then () ->
fn = (r) ->
return f() if r
# perform the login
gapi.auth2.getAuthInstance().signIn()
gapi.auth2.getAuthInstance().isSignedIn.listen (r) ->
fn(r)
fn(gapi.auth2.getAuthInstance().isSignedIn.get())
meta: (f) ->
2018-02-27 19:39:50 +01:00
me = @
2018-02-27 16:40:36 +01:00
@oninit () ->
2018-02-27 19:39:50 +01:00
me.gid = G_CACHE[me.path] if G_CACHE[me.path]
if me.gid
#console.log "Gid exists ", me.gid
gapi.client.drive.files.get {
fileId: me.gid,
fields: me.fields()
}
.then (r) ->
return unless r.result
f(r)
else
#console.log "Find file in ", me.parent()
fp = me.parent().asFileHandler()
fp.meta (d) ->
file = d.result
G_CACHE[fp.path] = file.id
gapi.client.drive.files.list {
q: "name = '#{me.basename}' and '#{file.id}' in parents and trashed = false",
fields: "files(#{me.fields()})"
}
.then (r) ->
#console.log r
return unless r.result.files and r.result.files.length > 0
G_CACHE[me.path] = r.result.files[0].id
f { result: r.result.files[0] }
2018-02-27 16:40:36 +01:00
2018-02-27 19:39:50 +01:00
fields: () ->
return "webContentLink, id, name,mimeType,description, kind, parents, properties, iconLink, createdTime, modifiedTime, owners, permissions, fullFileExtension, fileExtension, size"
2018-02-27 16:40:36 +01:00
action: (n, p, f) ->
me = @
switch n
when "read"
2018-02-27 19:39:50 +01:00
return unless @info.id
console.log @info.webContentLink
accessToken = gapi.auth.getToken().access_token
xhr = new XMLHttpRequest()
xhr.open 'GET', @info.webContentLink
xhr.setRequestHeader 'Authorization', 'Bearer ' + accessToken
xhr.setRequestHeader 'Access-Control-Allow-Origin', '*'
xhr.onload = () ->
f xhr.responseText
xhr.onerror = () ->
console.log "eror download"
xhr.send()
2018-02-27 16:40:36 +01:00
return
when "mk"
return
when "write"
return
when "upload"
return
when "remove"
return
when "publish"
return
when "download"
return
when "move"
return
else
return _courrier.osfail "VFS unknown action: #{n}", (_API.throwe "OS.VFS"), n
2018-02-13 20:27:41 +01:00
self.OS.API.VFS.register "^gdv$", GoogleDriveHandler