support cache search in google drive api

This commit is contained in:
Xuan Sang LE 2018-03-03 22:27:54 +01:00
parent 3e388d2b80
commit 234131bfa1
3 changed files with 24 additions and 11 deletions

View File

@ -220,7 +220,7 @@ self.OS.API =
return r return r
onsearch: (name, fn) -> onsearch: (name, fn) ->
_API.searchHandler[name] = fn unless _API.searchHandler[name] self.OS.API.searchHandler[name] = fn unless self.OS.API.searchHandler[name]
throwe: (n) -> throwe: (n) ->
err = undefined err = undefined

View File

@ -1,6 +1,6 @@
# GoogleDrive File Handler # GoogleDrive File Handler
G_CACHE = {"gdv:///":"root"} G_CACHE = {"gdv:///":{ id: "root", mime: 'dir' } }
class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler
constructor: (path) -> constructor: (path) ->
@ -17,7 +17,7 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler
fn = (r) -> fn = (r) ->
return f() if r return f() if r
# perform the login # perform the login
G_CACHE = {"gdv:///":"root"} G_CACHE = {"gdv:///":{ id: "root", mime: 'dir' } }
gapi.auth2.getAuthInstance().signIn() gapi.auth2.getAuthInstance().signIn()
if _API.libready @setting.apilink if _API.libready @setting.apilink
@ -52,7 +52,7 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler
me = @ me = @
@oninit () -> @oninit () ->
q = _courrier.getMID() q = _courrier.getMID()
me.gid = G_CACHE[me.path] if G_CACHE[me.path] me.gid = G_CACHE[me.path].id 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" _API.loading q, "GAPI"
@ -75,7 +75,7 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler
file = d.result file = d.result
q1 = _courrier.getMID() q1 = _courrier.getMID()
_API.loading q1, "GAPI" _API.loading q1, "GAPI"
G_CACHE[fp.path] = file.id G_CACHE[fp.path] = { id: file.id, mime: file.mimeType }
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",
fields: "files(#{me.fields()})" fields: "files(#{me.fields()})"
@ -84,7 +84,7 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler
#console.log r #console.log r
_API.loaded q1, "OK" _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] = { id: r.result.files[0].id, mime: r.result.files[0].mimeType }
r.result.files[0].mime = r.result.files[0].mimeType r.result.files[0].mime = r.result.files[0].mimeType
f { result: r.result.files[0] } f { result: r.result.files[0] }
.catch (err) -> .catch (err) ->
@ -150,6 +150,7 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler
file.mime = "dir" file.mime = "dir"
file.type = "dir" file.type = "dir"
file.size = 0 file.size = 0
G_CACHE[file.path] = { id: file.gid, mime: file.mime }
f { result: r.result.files } f { result: r.result.files }
.catch (err) -> .catch (err) ->
_API.loaded q, "FAIL" _API.loaded q, "FAIL"
@ -186,7 +187,7 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler
_API.loaded q, "OK" _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] = { id: r.result.id, mime: "dir" }
f r f r
.catch (err) -> .catch (err) ->
_API.loaded q, "FAIL" _API.loaded q, "FAIL"
@ -195,7 +196,8 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler
return return
when "write" when "write"
gid = G_CACHE[me.path] gid = undefined
gid = G_CACHE[me.path].id if G_CACHE[me.path]
if gid if gid
_API.loaded q, "OK" _API.loaded q, "OK"
@save gid, p, f @save gid, p, f
@ -214,7 +216,7 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler
.then (r) -> .then (r) ->
_API.loaded q, "OK" _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] = { id: r.result.id, mime: p }
me.save r.result.id, p, f me.save r.result.id, p, f
.catch (err) -> .catch (err) ->
_API.loaded q, "FAIL" _API.loaded q, "FAIL"
@ -299,4 +301,15 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler
return _courrier.osfail "VFS unknown action: #{n}", (_API.throwe "OS.VFS"), n return _courrier.osfail "VFS unknown action: #{n}", (_API.throwe "OS.VFS"), n
self.OS.API.VFS.register "^gdv$", GoogleDriveHandler self.OS.API.VFS.register "^gdv$", GoogleDriveHandler
# search the cache for file
self.OS.API.onsearch "Google Drive", (t) ->
arr = []
term = new RegExp t, "i"
for k, v of G_CACHE
if k.match term or (v and v.mime.match term)
file = k.asFileHandler()
file.text = file.basename
file.mime = v.mime
arr.push file
return arr

View File

@ -65,7 +65,7 @@ class SpotlightDialog extends this.OS.GUI.BaseDialog
else else
text = @searchbox.value text = @searchbox.value
($ @scheme).css("height", "45px") ($ @scheme).css("height", "45px")
return unless text.length > 3 return unless text.length >= 3
result = @_api.search text result = @_api.search text
return if result.length is 0 return if result.length is 0
@container.set "items", result @container.set "items", result