From c53e690afc3cfc2dedb28b6a218ddfdb358bd73a Mon Sep 17 00:00:00 2001 From: DrosoCode Date: Mon, 31 May 2021 13:36:58 +0200 Subject: [PATCH 1/7] feat: onlyoffice editor --- frontend/src/views/Files.vue | 5 + frontend/src/views/files/OnlyOfficeEditor.vue | 154 ++++++++++++++++++ 2 files changed, 159 insertions(+) create mode 100644 frontend/src/views/files/OnlyOfficeEditor.vue diff --git a/frontend/src/views/Files.vue b/frontend/src/views/Files.vue index 8f50426d..35f0630e 100644 --- a/frontend/src/views/Files.vue +++ b/frontend/src/views/Files.vue @@ -42,6 +42,7 @@ export default { Preview, Listing, Editor: () => import("@/views/files/Editor"), + OnlyOfficeEditor: () => import("@/views/files/OnlyOfficeEditor"), }, data: function () { return { @@ -63,6 +64,10 @@ export default { this.req.type === "textImmutable" ) { return "editor"; + } else if ( + this.req.type === "officedocument" + ) { + return "OnlyOfficeEditor"; } else { return "preview"; } diff --git a/frontend/src/views/files/OnlyOfficeEditor.vue b/frontend/src/views/files/OnlyOfficeEditor.vue new file mode 100644 index 00000000..6d4e9979 --- /dev/null +++ b/frontend/src/views/files/OnlyOfficeEditor.vue @@ -0,0 +1,154 @@ + + + + + From 75e82522fcafd04a6d2da605fc45a95e4f51be15 Mon Sep 17 00:00:00 2001 From: DrosoCode Date: Mon, 31 May 2021 13:37:08 +0200 Subject: [PATCH 2/7] feat: onlyoffice callback handler --- files/file.go | 3 +++ http/http.go | 2 ++ http/onlyoffice.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 http/onlyoffice.go diff --git a/files/file.go b/files/file.go index ad78ad06..ee9db86b 100644 --- a/files/file.go +++ b/files/file.go @@ -188,6 +188,9 @@ func (i *FileInfo) detectType(modify, saveContent, readHeader bool) error { i.Content = string(content) } return nil + case strings.HasPrefix(mimetype, "application/vnd.openxmlformats-officedocument"): + i.Type = "officedocument" + return nil default: i.Type = "blob" } diff --git a/http/http.go b/http/http.go index bc798561..082db1d7 100644 --- a/http/http.go +++ b/http/http.go @@ -53,6 +53,8 @@ func NewHandler( users.Handle("/{id:[0-9]+}", monkey(userGetHandler, "")).Methods("GET") users.Handle("/{id:[0-9]+}", monkey(userDeleteHandler, "")).Methods("DELETE") + api.PathPrefix("/onlyoffice").Handler(monkey(onlyofficeCallbackHandler, "/api/onlyoffice/callback")).Methods("POST") + api.PathPrefix("/resources").Handler(monkey(resourceGetHandler, "/api/resources")).Methods("GET") api.PathPrefix("/resources").Handler(monkey(resourceDeleteHandler(fileCache), "/api/resources")).Methods("DELETE") api.PathPrefix("/resources").Handler(monkey(resourcePostHandler(fileCache), "/api/resources")).Methods("POST") diff --git a/http/onlyoffice.go b/http/onlyoffice.go new file mode 100644 index 00000000..c8bc67da --- /dev/null +++ b/http/onlyoffice.go @@ -0,0 +1,65 @@ +package http + +import ( + "io/ioutil" + "net/http" + "errors" + "encoding/json" +) + +type OnlyOfficeCallback struct { + ChangesUrl string `json:"changesurl,omitempty"` + Key string `json:"key"` + Status int `json:"status"` + Url string `json:"url,omitempty"` + Users []string `json:"users,omitempty"` + UserData string `json:"userdata,omitempty"` +} + +var onlyofficeCallbackHandler = withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) { + body, e1 := ioutil.ReadAll(r.Body) + if e1 != nil { + return http.StatusInternalServerError, e1 + } + + var data OnlyOfficeCallback + err1 := json.Unmarshal(body, &data) + if err1 != nil { + return http.StatusInternalServerError, err1 + } + + if (data.Status == 2 || data.Status == 6) { + docPath := r.URL.Query().Get("save") + if docPath == "" { + return http.StatusInternalServerError, errors.New("Unable to get file save path") + } + + if !d.user.Perm.Modify || !d.Check(docPath) { + return http.StatusForbidden, nil + } + + doc, err2 := http.Get(data.Url) + if err2 != nil { + return http.StatusInternalServerError, err2 + } + defer doc.Body.Close() + + err := d.RunHook(func() error { + _, writeErr := writeFile(d.user.Fs, docPath, doc.Body) + if writeErr != nil { + return writeErr + } + return nil + }, "save", docPath, "", d.user) + + if err != nil { + _ = d.user.Fs.RemoveAll(docPath) + return http.StatusInternalServerError, err + } + } + + resp := map[string]int{ + "error": 0, + } + return renderJSON(w, r, resp) +}) From f140fe1ddfc967686ac3016907066cbc7505567a Mon Sep 17 00:00:00 2001 From: DrosoCode Date: Mon, 31 May 2021 13:56:58 +0200 Subject: [PATCH 3/7] feat: onlyoffice url config in settings --- cmd/config_init.go | 1 + cmd/config_set.go | 2 ++ frontend/src/utils/constants.js | 2 ++ frontend/src/views/Files.vue | 3 ++- frontend/src/views/files/OnlyOfficeEditor.vue | 4 ++-- frontend/src/views/settings/Global.vue | 14 ++++++++++++++ http/settings.go | 3 +++ http/static.go | 1 + settings/settings.go | 1 + 9 files changed, 28 insertions(+), 3 deletions(-) diff --git a/cmd/config_init.go b/cmd/config_init.go index 12b11688..1dc0fd52 100644 --- a/cmd/config_init.go +++ b/cmd/config_init.go @@ -39,6 +39,7 @@ override the options.`, DisableExternal: mustGetBool(flags, "branding.disableExternal"), Files: mustGetString(flags, "branding.files"), }, + OnlyOffice: "", } ser := &settings.Server{ diff --git a/cmd/config_set.go b/cmd/config_set.go index e959bc97..31b83e29 100644 --- a/cmd/config_set.go +++ b/cmd/config_set.go @@ -55,6 +55,8 @@ you want to change. Other options will remain unchanged.`, set.Branding.DisableExternal = mustGetBool(flags, flag.Name) case "branding.files": set.Branding.Files = mustGetString(flags, flag.Name) + case "onlyoffice": + set.OnlyOffice = mustGetString(flags, flag.Name) } }) diff --git a/frontend/src/utils/constants.js b/frontend/src/utils/constants.js index 9761339b..a0d4dbd0 100644 --- a/frontend/src/utils/constants.js +++ b/frontend/src/utils/constants.js @@ -14,6 +14,7 @@ const theme = window.FileBrowser.Theme; const enableThumbs = window.FileBrowser.EnableThumbs; const resizePreview = window.FileBrowser.ResizePreview; const enableExec = window.FileBrowser.EnableExec; +const onlyOffice = window.FileBrowser.OnlyOffice; export { name, @@ -31,4 +32,5 @@ export { enableThumbs, resizePreview, enableExec, + onlyOffice, }; diff --git a/frontend/src/views/Files.vue b/frontend/src/views/Files.vue index 35f0630e..8a547313 100644 --- a/frontend/src/views/Files.vue +++ b/frontend/src/views/Files.vue @@ -22,6 +22,7 @@