feat: onlyoffice callback handler

This commit is contained in:
DrosoCode 2021-05-31 13:37:08 +02:00
parent c53e690afc
commit 75e82522fc
3 changed files with 70 additions and 0 deletions

View File

@ -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"
}

View File

@ -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")

65
http/onlyoffice.go Normal file
View File

@ -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)
})