From 234131bfa1a76cb99213ef75971a7bb7db7e4591 Mon Sep 17 00:00:00 2001 From: Xuan Sang LE Date: Sat, 3 Mar 2018 22:27:54 +0100 Subject: [PATCH] support cache search in google drive api --- src/core/api.coffee | 2 +- src/core/vfs/GoogleDriveHandler.coffee | 31 +++++++++++++++------- src/packages/CoreServices/Spotlight.coffee | 2 +- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/core/api.coffee b/src/core/api.coffee index d885a80..bba3788 100644 --- a/src/core/api.coffee +++ b/src/core/api.coffee @@ -220,7 +220,7 @@ self.OS.API = return r 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) -> err = undefined diff --git a/src/core/vfs/GoogleDriveHandler.coffee b/src/core/vfs/GoogleDriveHandler.coffee index 60fd220..7c2127d 100644 --- a/src/core/vfs/GoogleDriveHandler.coffee +++ b/src/core/vfs/GoogleDriveHandler.coffee @@ -1,6 +1,6 @@ # GoogleDrive File Handler -G_CACHE = {"gdv:///":"root"} +G_CACHE = {"gdv:///":{ id: "root", mime: 'dir' } } class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler constructor: (path) -> @@ -17,7 +17,7 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler fn = (r) -> return f() if r # perform the login - G_CACHE = {"gdv:///":"root"} + G_CACHE = {"gdv:///":{ id: "root", mime: 'dir' } } gapi.auth2.getAuthInstance().signIn() if _API.libready @setting.apilink @@ -52,7 +52,7 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler me = @ @oninit () -> 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 #console.log "Gid exists ", me.gid _API.loading q, "GAPI" @@ -75,7 +75,7 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler file = d.result q1 = _courrier.getMID() _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 { q: "name = '#{me.basename}' and '#{file.id}' in parents and trashed = false", fields: "files(#{me.fields()})" @@ -84,7 +84,7 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler #console.log r _API.loaded q1, "OK" 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 f { result: r.result.files[0] } .catch (err) -> @@ -150,6 +150,7 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler file.mime = "dir" file.type = "dir" file.size = 0 + G_CACHE[file.path] = { id: file.gid, mime: file.mime } f { result: r.result.files } .catch (err) -> _API.loaded q, "FAIL" @@ -186,7 +187,7 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler _API.loaded q, "OK" #console.log r 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 .catch (err) -> _API.loaded q, "FAIL" @@ -195,7 +196,8 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler return when "write" - gid = G_CACHE[me.path] + gid = undefined + gid = G_CACHE[me.path].id if G_CACHE[me.path] if gid _API.loaded q, "OK" @save gid, p, f @@ -214,7 +216,7 @@ class GoogleDriveHandler extends this.OS.API.VFS.BaseFileHandler .then (r) -> _API.loaded q, "OK" 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 .catch (err) -> _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 -self.OS.API.VFS.register "^gdv$", GoogleDriveHandler \ No newline at end of file +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 \ No newline at end of file diff --git a/src/packages/CoreServices/Spotlight.coffee b/src/packages/CoreServices/Spotlight.coffee index cf9b0ee..a17ca71 100644 --- a/src/packages/CoreServices/Spotlight.coffee +++ b/src/packages/CoreServices/Spotlight.coffee @@ -65,7 +65,7 @@ class SpotlightDialog extends this.OS.GUI.BaseDialog else text = @searchbox.value ($ @scheme).css("height", "45px") - return unless text.length > 3 + return unless text.length >= 3 result = @_api.search text return if result.length is 0 @container.set "items", result