From 46d271ad903e363323c2fbf66649566e01c5539e Mon Sep 17 00:00:00 2001 From: lxsang Date: Tue, 9 Feb 2021 19:41:47 +0100 Subject: [PATCH] make it online --- OnlyOffice/api/api.lua | 59 +++++++++++++++ OnlyOffice/assets/scheme.html | 4 +- OnlyOffice/build/debug/api.lua | 59 +++++++++++++++ OnlyOffice/build/debug/main.js | 112 +++++++++++++++++++++++++--- OnlyOffice/build/debug/package.json | 17 ++++- OnlyOffice/build/debug/scheme.html | 4 +- OnlyOffice/coffees/main.coffee | 84 ++++++++++++++++++--- OnlyOffice/package.json | 17 ++++- OnlyOffice/project.json | 2 +- 9 files changed, 327 insertions(+), 31 deletions(-) create mode 100644 OnlyOffice/api/api.lua create mode 100644 OnlyOffice/build/debug/api.lua diff --git a/OnlyOffice/api/api.lua b/OnlyOffice/api/api.lua new file mode 100644 index 0000000..144b491 --- /dev/null +++ b/OnlyOffice/api/api.lua @@ -0,0 +1,59 @@ +local args=... +local web = require("web") +local vfs = require("vfs") + +if not args then + args = REQUEST +end + +local result = function(data) + return { + error = false, + result = data + } +end + +local error = function(data) + return { + error = data, + result = false + } +end + +local handle = {} + +handle.token = function() + return result("sessionid="..SESSION.sessionid) +end + +handle.save = function() + print(JSON.encode(REQUEST)) + if not REQUEST.json then + return error("Invalid request") + end + local data = JSON.decodeString(REQUEST.json) + if not data then + return error("Invalid request") + end + if not REQUEST.file then + return error("No file found") + end + local file = vfs.ospath(REQUEST.file) + if data.status == 2 then + print("download to"..file) + if not web.download(data.url, file) then + print("Unable to download") + return error("Unable to save file") + end + end + + return result("OK") +end + +print(JSON.encode(args)) + +if args.action and handle[args.action] then + return handle[args.action](args.args) +else + return error("Invalid action parameter") +end diff --git a/OnlyOffice/assets/scheme.html b/OnlyOffice/assets/scheme.html index be5ebc5..1f0b62f 100644 --- a/OnlyOffice/assets/scheme.html +++ b/OnlyOffice/assets/scheme.html @@ -1,5 +1,5 @@ - + -
+
\ No newline at end of file diff --git a/OnlyOffice/build/debug/api.lua b/OnlyOffice/build/debug/api.lua new file mode 100644 index 0000000..144b491 --- /dev/null +++ b/OnlyOffice/build/debug/api.lua @@ -0,0 +1,59 @@ +local args=... +local web = require("web") +local vfs = require("vfs") + +if not args then + args = REQUEST +end + +local result = function(data) + return { + error = false, + result = data + } +end + +local error = function(data) + return { + error = data, + result = false + } +end + +local handle = {} + +handle.token = function() + return result("sessionid="..SESSION.sessionid) +end + +handle.save = function() + print(JSON.encode(REQUEST)) + if not REQUEST.json then + return error("Invalid request") + end + local data = JSON.decodeString(REQUEST.json) + if not data then + return error("Invalid request") + end + if not REQUEST.file then + return error("No file found") + end + local file = vfs.ospath(REQUEST.file) + if data.status == 2 then + print("download to"..file) + if not web.download(data.url, file) then + print("Unable to download") + return error("Unable to save file") + end + end + + return result("OK") +end + +print(JSON.encode(args)) + +if args.action and handle[args.action] then + return handle[args.action](args.args) +else + return error("Invalid action parameter") +end diff --git a/OnlyOffice/build/debug/main.js b/OnlyOffice/build/debug/main.js index 4889ec4..249ee6f 100644 --- a/OnlyOffice/build/debug/main.js +++ b/OnlyOffice/build/debug/main.js @@ -4,22 +4,112 @@ OnlyOffice = class OnlyOffice extends this.OS.application.BaseApplication { constructor(args) { super("OnlyOffice", args); + this.eid = `id${Math.random().toString(36).replace(".", "")}`; } main() { var placeholder; + this.currfile = void 0; + if (this.args && this.args.length > 0) { + this.currfile = this.args[0].path.asFileHandle(); + } placeholder = this.find("editor-area"); - return this.editor = new DocsAPI.DocEditor(placeholder, { - "document": { - "fileType": "docx", - "key": "Khirz6zTPdfd7", - "title": "Example Document Title.docx", - "url": "https://file-examples-com.github.io/uploads/2017/02/file-sample_100kB.doc" - }, - "documentType": "word" + placeholder.id = this.eid; + if (this.currfile) { + return this.open(); + } + } + + open() { + if (!this.currfile) { + return; + } + console.log(this.currfile); + return this.exec("token", { + file: this.currfile.path + }).then((d) => { + if (d.error) { + return this.error(d.error); + } + this.access_token = d.result; + return this.currfile.onready().then((meta) => { + if (this.editor) { + //@scheme.apptitle = @currfile.path + this.editor.destroyEditor(); + } + return this.editor = new DocsAPI.DocEditor(this.eid, { + events: { + onRequestCreateNew: () => { + return this.newDocument(); + }, + onRequestSaveAs: (e) => { + return console.log(e); + } + }, + document: { + fileType: this.currfile.ext, + key: meta.mtime.hash().toString(), + title: this.currfile.path, + url: this.currfile.getlink() + "?" + this.access_token + }, + documentType: this.getDocType(this.currfile.ext), + editorConfig: { + user: { + id: this.systemsetting.user.id.toString(), + name: this.systemsetting.user.username + }, + customization: { + compactHeader: false + }, + //autosave: false, + //forcesave: true + callbackUrl: this.uapi("save") + } + }); + }); + }).catch((e) => { + return this.error(e.toString(), e); }); } + getDocType(ext) { + if ("doc,docx,epub,odt".split(",").includes(ext)) { + return "word"; + } + if ("csv,ods,xls,xlsx".split(",").includes(ext)) { + return "cell"; + } + if ("odp,ppt,pptx".split(",").includes(ext)) { + return "slide"; + } + return "none"; + } + + saveAs(e) { + return console.log(e); + } + + newDocument() { + console.log("create document"); + return this.error(__("Unable to create document")); + } + + uapi(action) { + return `${this._api.REST}/system/apigateway?ws=0&path=${this.path()}/api.lua&action=${action}&file=${this.currfile.path}&${this.access_token}`; + } + + exec(action, args) { + var cmd; + cmd = { + path: `${this.path()}/api.lua`, + parameters: { + action: action, + args: args + } + }; + return this.call(cmd); + } + cleanup() { if (this.editor) { this.editor.destroyEditor(); @@ -29,8 +119,12 @@ }; - OnlyOffice.dependencies = ["http://192.168.1.91/web-apps/apps/api/documents/api.js"]; + OnlyOffice.dependencies = ["https://office.iohub.dev/web-apps/apps/api/documents/api.js"]; this.OS.register("OnlyOffice", OnlyOffice); + this.extensionParams = { + url: "https://office.iohub.dev/web-apps/" + }; + }).call(this); diff --git a/OnlyOffice/build/debug/package.json b/OnlyOffice/build/debug/package.json index 38a1ffd..26e9ddc 100644 --- a/OnlyOffice/build/debug/package.json +++ b/OnlyOffice/build/debug/package.json @@ -1,8 +1,8 @@ { "pkgname": "OnlyOffice", "app":"OnlyOffice", - "name":"OnlyOffice", - "description":"OnlyOffice", + "name":"Office Suite", + "description":"Online Office suite based on OnlyOffice", "info":{ "author": "", "email": "" @@ -10,7 +10,18 @@ "version":"0.0.1-a", "category":"Other", "iconclass":"fa fa-adn", - "mimes":["none"], + "mimes":[ + "application/vnd.oasis.opendocument.text", + "application/vnd.oasis.opendocument.spreadsheet", + "application/vnd.oasis.opendocument.presentation", + "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + "application/msword", + "application/vnd.ms-excel", + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + "application/vnd.ms-powerpoint", + "application/vnd.openxmlformats-officedocument.presentationml.presentation", + "application/epub+zip" + ], "dependencies":[], "locale": {} } \ No newline at end of file diff --git a/OnlyOffice/build/debug/scheme.html b/OnlyOffice/build/debug/scheme.html index be5ebc5..1f0b62f 100644 --- a/OnlyOffice/build/debug/scheme.html +++ b/OnlyOffice/build/debug/scheme.html @@ -1,5 +1,5 @@ - + -
+
\ No newline at end of file diff --git a/OnlyOffice/coffees/main.coffee b/OnlyOffice/coffees/main.coffee index ae035de..3fb105b 100644 --- a/OnlyOffice/coffees/main.coffee +++ b/OnlyOffice/coffees/main.coffee @@ -1,25 +1,87 @@ class OnlyOffice extends this.OS.application.BaseApplication constructor: ( args ) -> super "OnlyOffice", args + @eid = "id#{Math.random().toString(36).replace(".","")}" main: () -> + @currfile = undefined + if @args and @args.length > 0 + @currfile = @args[0].path.asFileHandle() placeholder = @find "editor-area" - @editor = new DocsAPI.DocEditor(placeholder, { - "document": { - "fileType": "docx", - "key": "Khirz6zTPdfd7", - "title": "Example Document Title.docx", - "url": "https://file-examples-com.github.io/uploads/2017/02/file-sample_100kB.doc" - }, - "documentType": "word" - }); + placeholder.id = @eid + @open() if @currfile + + open: () -> + return unless @currfile + console.log @currfile + @exec("token", {file: @currfile.path}) + .then (d) => + return @error d.error if d.error + @access_token = d.result + @currfile.onready() + .then (meta) => + #@scheme.apptitle = @currfile.path + @editor.destroyEditor() if @editor + @editor = new DocsAPI.DocEditor(@eid, { + events: { + onRequestCreateNew: () => @newDocument(), + onRequestSaveAs: (e) => console.log e + }, + document: { + fileType: @currfile.ext, + key: meta.mtime.hash().toString(), + title: @currfile.path, + url: @currfile.getlink() + "?" + @access_token + }, + documentType: @getDocType(@currfile.ext), + editorConfig: { + user: { + id: @systemsetting.user.id.toString(), + name: @systemsetting.user.username + }, + customization: { + compactHeader: false, + #autosave: false, + #forcesave: true + }, + callbackUrl: @uapi("save") + } + }); + .catch (e) => + @error e.toString(), e + + getDocType: (ext) -> + return "word" if "doc,docx,epub,odt".split(",").includes(ext) + return "cell" if "csv,ods,xls,xlsx".split(",").includes(ext) + return "slide" if "odp,ppt,pptx".split(",").includes(ext) + return "none" + + saveAs: (e) -> + console.log e + + newDocument: () -> + console.log("create document") + @error __("Unable to create document") + uapi: (action) -> + return "#{@_api.REST}/system/apigateway?ws=0&path=#{@path()}/api.lua&action=#{action}&file=#{@currfile.path}&#{@access_token}" + + exec: (action, args) -> + cmd = + path: "#{@path()}/api.lua", + parameters: + action: action, + args: args + return @call(cmd) cleanup: () -> @editor.destroyEditor() if @editor @editor = undefined OnlyOffice.dependencies = [ - "http://192.168.1.91/web-apps/apps/api/documents/api.js" + "https://office.iohub.dev/web-apps/apps/api/documents/api.js" ] -this.OS.register "OnlyOffice", OnlyOffice \ No newline at end of file +this.OS.register "OnlyOffice", OnlyOffice +this.extensionParams = { + url: "https://office.iohub.dev/web-apps/" +} \ No newline at end of file diff --git a/OnlyOffice/package.json b/OnlyOffice/package.json index 38a1ffd..26e9ddc 100644 --- a/OnlyOffice/package.json +++ b/OnlyOffice/package.json @@ -1,8 +1,8 @@ { "pkgname": "OnlyOffice", "app":"OnlyOffice", - "name":"OnlyOffice", - "description":"OnlyOffice", + "name":"Office Suite", + "description":"Online Office suite based on OnlyOffice", "info":{ "author": "", "email": "" @@ -10,7 +10,18 @@ "version":"0.0.1-a", "category":"Other", "iconclass":"fa fa-adn", - "mimes":["none"], + "mimes":[ + "application/vnd.oasis.opendocument.text", + "application/vnd.oasis.opendocument.spreadsheet", + "application/vnd.oasis.opendocument.presentation", + "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + "application/msword", + "application/vnd.ms-excel", + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + "application/vnd.ms-powerpoint", + "application/vnd.openxmlformats-officedocument.presentationml.presentation", + "application/epub+zip" + ], "dependencies":[], "locale": {} } \ No newline at end of file diff --git a/OnlyOffice/project.json b/OnlyOffice/project.json index 1ce0928..942253d 100644 --- a/OnlyOffice/project.json +++ b/OnlyOffice/project.json @@ -3,5 +3,5 @@ "css": [], "javascripts": [], "coffees": ["coffees/main.coffee"], - "copies": ["assets/scheme.html", "package.json", "README.md"] + "copies": ["api/api.lua", "assets/scheme.html", "package.json", "README.md"] } \ No newline at end of file