feat(go): refresh file config

This commit is contained in:
sshakndr
2024-12-10 16:29:29 +07:00
parent 3fe3a49edb
commit bf9c1dc36c
6 changed files with 99 additions and 0 deletions

View File

@ -33,6 +33,7 @@ type FileOperationsEndpointsHandler interface {
}
type InfoEndpointsHandler interface {
Config(w http.ResponseWriter, r *http.Request)
Files(w http.ResponseWriter, r *http.Request)
Formats(w http.ResponseWriter, r *http.Request)
History(w http.ResponseWriter, r *http.Request)

View File

@ -0,0 +1,83 @@
/**
*
* (c) Copyright Ascensio System SIA 2024
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package dapi
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/ONLYOFFICE/document-server-integration/server/managers"
"github.com/ONLYOFFICE/document-server-integration/server/models"
"github.com/ONLYOFFICE/document-server-integration/server/shared"
)
func (srv *DefaultServerEndpointsHandler) Config(w http.ResponseWriter, r *http.Request) {
filename := r.URL.Query().Get("fileName")
var permissions models.Permissions
err := json.Unmarshal([]byte(r.URL.Query().Get("permissions")), &permissions)
if err != nil {
shared.SendDocumentServerRespose(w, true)
return
}
path, err := srv.GenerateFilePath(filename)
if err != nil || filename == "" || !srv.PathExists(path) {
shared.SendCustomErrorResponse(w, "File not found")
return
}
remoteAddr := generateUrl(r)
docKey, err := srv.StorageManager.GenerateFileHash(filename)
if err != nil {
shared.SendDocumentServerRespose(w, true)
return
}
config := models.Config{
Document: models.Document{
Title: filename,
Url: srv.StorageManager.GeneratePublicFileUri(
filename, remoteAddr, managers.FileMeta{}),
Key: docKey,
Permissions: permissions,
ReferenceData: models.ReferenceData{
FileKey: fmt.Sprintf("{\"fileName\":\"%s\"}", filename),
InstanceId: remoteAddr,
},
},
EditorConfig: models.EditorConfig{
CallbackUrl: fmt.Sprintf(
"%s/callback?filename=%s&user_address=%s",
remoteAddr,
filename,
remoteAddr,
),
Mode: "edit",
},
}
secret := strings.TrimSpace(srv.config.JwtSecret)
if secret != "" && srv.config.JwtEnabled {
token, _ := srv.JwtManager.JwtSign(config, []byte(secret))
config.Token = token
}
shared.SendResponse(w, config)
}

View File

@ -129,6 +129,7 @@ var descriptionUser0 []string = []string{
"Can't protect file",
"View file without collaboration",
"Can't submit forms",
"Can't refresh outdated file",
}
var descriptionUser1 []string = []string{
"File author by default",

View File

@ -103,6 +103,7 @@ func (srv *Server) configureRouter() *mux.Router {
r.HandleFunc("/historyObj", srv.ServerAPI.HistoryObj).Methods(http.MethodPost)
r.HandleFunc("/restore", srv.ServerAPI.Restore).Methods(http.MethodPut)
r.HandleFunc("/formats", srv.ServerAPI.Formats).Methods(http.MethodGet)
r.HandleFunc("/config", srv.ServerAPI.Config).Methods(http.MethodGet)
r.HandleFunc("/forgotten", srv.ServerAPI.Forgotten).Methods(http.MethodGet, http.MethodDelete)
return r

View File

@ -362,6 +362,17 @@
}
};
var onRequestRefreshFile = function(event) {
let xhr = new XMLHttpRequest();
xhr.open("GET", "config?fileName=" + encodeURIComponent(config.document.title) +
"&permissions=" + encodeURIComponent(JSON.stringify(config.document.permissions)));
xhr.send();
xhr.onload = function () {
innerAlert(xhr.responseText);
docEditor.refreshFile(JSON.parse(xhr.responseText));
};
};
var events = {
"onAppReady": onAppReady,
"onDocumentStateChange": onDocumentStateChange,
@ -378,6 +389,7 @@
var config = {{ .config }};
if (config.editorConfig.user.id !== "uid-0") {
events["onRequestRefreshFile"] = onRequestRefreshFile;
events["onRequestClose"] = onRequestClose;
events["onRequestSendNotify"] = onRequestSendNotify;
events["onRequestEditRights"] = onRequestEditRights;