From a70f52f965b91e20deae4fdda6c3fe434f8da329 Mon Sep 17 00:00:00 2001 From: Xuan Sang LE Date: Tue, 23 Jan 2018 18:16:41 +0100 Subject: [PATCH] VFS --- Makefile | 3 +- src/core/BaseApplication.coffee | 1 - src/core/api.coffee | 2 +- src/core/gui.coffee | 5 -- src/core/handlers/RemoteHandler.coffee | 6 ++ src/core/vfs.coffee | 115 ++++++++++++++++++++++--- 6 files changed, 111 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index ae7cc75..77863ed 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,8 @@ NC=\033[0m coffees= src/core/core.coffee\ src/core/api.coffee\ src/core/handlers/RemoteHandler.coffee\ - src/core/gui.coffee\ + src/core/vfs.coffee\ + src/core/gui.coffee\ src/core/BaseModel.coffee\ src/core/BaseApplication.coffee\ src/core/BaseService.coffee\ diff --git a/src/core/BaseApplication.coffee b/src/core/BaseApplication.coffee index ff54a23..d4ba971 100644 --- a/src/core/BaseApplication.coffee +++ b/src/core/BaseApplication.coffee @@ -1,4 +1,3 @@ -self = this class BaseApplication extends this.OS.GUI.BaseModel constructor: (name, args) -> super name, args diff --git a/src/core/api.coffee b/src/core/api.coffee index 9b5f6a2..c6df68a 100644 --- a/src/core/api.coffee +++ b/src/core/api.coffee @@ -2,7 +2,7 @@ self.OS.API = # the handler object could be a any remote or local handle to # fetch user data, used by the API to make requests # handlers are defined in /src/handlers - handler: {} + handler: { } #request a user data post: (p, d, c, f) -> q = _courrier.getMID() diff --git a/src/core/gui.coffee b/src/core/gui.coffee index a624c9e..333996d 100644 --- a/src/core/gui.coffee +++ b/src/core/gui.coffee @@ -1,10 +1,5 @@ self.OS.GUI = dialog: new Object() - init: () -> - query = - path: 'VFS/get' - data: "#{_GUI.tagPath}/tags.json" - self.OS.API.request query, ()-> htmlToScheme: (html, app, parent) -> scheme = $.parseHTML html ($ parent).append scheme diff --git a/src/core/handlers/RemoteHandler.coffee b/src/core/handlers/RemoteHandler.coffee index 635dc3a..b70613c 100644 --- a/src/core/handlers/RemoteHandler.coffee +++ b/src/core/handlers/RemoteHandler.coffee @@ -3,6 +3,12 @@ self.OS.API.handler = path = "lua-api/fs/scandir" _API.post path, { path: p }, c, (e, s) -> _courrier.osfail "Fail to scan directory: #{path}", e, s + + fileinfo: (p, c) -> + path = "lua-api/fs/fileinfo" + _API.post path, { path: p }, c, (e, s) -> + _courrier.osfail "Fail to get file metadata: #{path}", e, s + scanapp: (p, c ) -> path = "lua-api/system/application" auth: (c) -> diff --git a/src/core/vfs.coffee b/src/core/vfs.coffee index 7d66333..bc12973 100644 --- a/src/core/vfs.coffee +++ b/src/core/vfs.coffee @@ -1,15 +1,104 @@ -self.OS.API.VFS = - readdir: (p, c, er) -> - h = _API.VFS.pathHandler p - h p, - (d ) -> c d - , (e, s) -> er e, s +String.prototype.hash = () -> + hash = 5381 + i = this.length + hash = (hash * 33) ^ this.charCodeAt(--i) while i + hash >>> 0 - pathHandler: (p) -> - list = p.split "///" - switch list[0] - when "app:" - return _API.handler.scanapp - else - return _API.handler.scandir +String.prototype.asFileHandler = () -> + list = this.split ":///" + switch list[0] + when "app" + return new ApplicationHandler(this) + else + return new RemoteFileHandler(this) + +this.OS.API.VFS = {} + +class BasicFileHandler + constructor: (@path) -> + list = @path.split ":///" + @protocol = list[0] + return unless list.length > 1 + re = list[1].replace(/^\/+|\/+$/g, '') + return if re is "" + @genealogy = re.split("/") + @basename = @genealogy[@genealogy.length - 1] unless @isRoot() + @ext = @basename.split( "." ).pop() unless @basename.lastIndexOf(".") is 0 or @basename.indexOf( "." ) is -1 + @ready = false + + + isRoot: () -> (not @genealogy) or (@genealogy.size is 0) + isHidden: () -> + return false if not @basename + @basename[0] is "." + + hash: () -> @path.hash() + + parent: () -> + return @ if @isRoot() + (@protocol + ":///" + (@genealogy.slice 0 , @genealogy.length - 1).join "/").asFileHandler() + + onready: (f, e) -> + # read meta data + return f() if @ready + me = @ + me.meta (d) -> + return e d.error if d.error + me.meta = d.result + me.ready = true + f() + + #public interface for all action on file + do: (a, f, e) -> + return e "Action #{a} not found" if not @[a] + me = @ + @onready (() -> me[a] f), e + + + # methods implemented by subclasses used as private methods + meta: (f) -> + + read: (f) -> + + write: (f) -> + + remove: (f) -> + + execute: (f) -> + + ls: (f) -> + + mk: (f) -> + +# now export the class +self.OS.API.VFS.BasicFileHandler = BasicFileHandler + +# Remote file handle +class RemoteFileHandler extends self.OS.API.VFS.BasicFileHandler + constructor: (path) -> + super path + + meta: (f) -> + _API.handler.fileinfo @path, f + + ls: (f) -> + return f(@) if @meta.type is "file" + _API.handler.scandir @path, f + +self.OS.API.VFS.RemoteFileHandler = RemoteFileHandler + +# Application Handler +class ApplicationHandler extends self.OS.API.VFS.BasicFileHandler + constructor: (path) -> + super path + +self.OS.API.VFS.ApplicationHandler = ApplicationHandler + + +# GoogleDrive File Handler +class GoogleDriveHandler extends self.OS.API.VFS.BasicFileHandler + constructor: (path) -> + super path + +self.OS.API.VFS.GoogleDriveHandler = GoogleDriveHandler \ No newline at end of file