From 53fae4906b0e0131ebeb94480cb155b6c689ed9c Mon Sep 17 00:00:00 2001 From: han2015 <1019850453@qq.com> Date: Thu, 26 Jun 2025 11:21:37 +0800 Subject: [PATCH] v0.3.6 add onlyoffice callbackEdit --- code/rest/matter_controller.go | 73 +++++++++++++++++++++++++++++++++- code/rest/matter_model.go | 2 + code/tool/result/web_result.go | 7 ++-- 3 files changed, 77 insertions(+), 5 deletions(-) diff --git a/code/rest/matter_controller.go b/code/rest/matter_controller.go index 27f7b84..2d84924 100644 --- a/code/rest/matter_controller.go +++ b/code/rest/matter_controller.go @@ -1,14 +1,21 @@ package rest import ( + "encoding/json" + "fmt" + "io" "net/http" + "os" + "os/exec" "strconv" "strings" + "time" "github.com/eyebluecn/tank/code/core" "github.com/eyebluecn/tank/code/tool/builder" "github.com/eyebluecn/tank/code/tool/i18n" "github.com/eyebluecn/tank/code/tool/result" + "github.com/eyebluecn/tank/code/tool/util" ) type MatterController struct { @@ -84,7 +91,8 @@ func (this *MatterController) RegisterRoutes() map[string]func(writer http.Respo routeMap["/api/matter/change/privacy"] = this.Wrap(this.ChangePrivacy, USER_ROLE_USER) routeMap["/api/matter/move"] = this.Wrap(this.Move, USER_ROLE_USER) // wait less routeMap["/api/matter/detail"] = this.Wrap(this.Detail, USER_ROLE_USER) - routeMap["/api/matter/page"] = this.Wrap(this.Page, USER_ROLE_GUEST) //done + routeMap["/api/matter/page"] = this.Wrap(this.Page, USER_ROLE_GUEST) //done + routeMap["/api/matter/save"] = this.Wrap(this.FileSave, USER_ROLE_GUEST) //done //mirror local files. routeMap["/api/matter/mirror"] = this.Wrap(this.Mirror, USER_ROLE_USER) @@ -112,7 +120,6 @@ func (this *MatterController) Detail(writer http.ResponseWriter, request *http.R } func (this *MatterController) Page(writer http.ResponseWriter, request *http.Request) *result.WebResult { - pageStr := request.FormValue("page") pageSizeStr := request.FormValue("pageSize") orderCreateTime := request.FormValue("orderCreateTime") @@ -229,6 +236,68 @@ func (this *MatterController) CreateDirectory(writer http.ResponseWriter, reques return this.Success(matter) } +type OfficeBack struct { + Status int + Key string + Url string + FileType string +} + +func (this *MatterController) FileSave(writer http.ResponseWriter, request *http.Request) *result.WebResult { + body, err := io.ReadAll(request.Body) + if err != nil { + panic(err) + } + + filePath := request.URL.Query().Get("info") + //如果filePath不是文件路径,panic + if !FILE_PATTERN.MatchString(filePath) || strings.Contains(filePath, "..") { + panic("不合规的文件路径:" + filePath) + } + var back OfficeBack + if err := json.Unmarshal(body, &back); err != nil { + panic(err) + } + + if back.Status != 2 { + writer.Write([]byte(`{"error":0}`)) + writer.WriteHeader(http.StatusOK) + return nil + } + + // 发起 GET 请求 + resp, err := http.Get(back.Url) + if err != nil { + panic(err) + } + defer resp.Body.Close() + + filePath = util.GetHomePath() + "/matter/" + filePath //在matterpath中操作 + //windows os should be "copy" command + cmd := exec.Command("cp", filePath, filePath+fmt.Sprint(time.Now().UnixMilli())) + if err = cmd.Run(); err != nil { + panic(err) + } + + // 创建本地文件, 如果已经存在,将清空文件,所以前面要做一个拷贝备份 + file, err := os.Create(filePath) + if err != nil { + panic(err) + } + defer file.Close() + + // 将响应体中的数据写入本地文件 + buffer := make([]byte, 1024*1024) // 1MB 缓冲区 + _, err = io.CopyBuffer(file, resp.Body, buffer) + if err != nil { + panic(back.Key + ": 文件流写入失败!!") + } + + writer.Write([]byte(`{"error":0}`)) + writer.WriteHeader(http.StatusOK) + return nil +} + func (this *MatterController) Upload(writer http.ResponseWriter, request *http.Request) *result.WebResult { puuid := request.FormValue("puuid") diff --git a/code/rest/matter_model.go b/code/rest/matter_model.go index 98f0164..2c49f87 100644 --- a/code/rest/matter_model.go +++ b/code/rest/matter_model.go @@ -27,6 +27,8 @@ const ( MATTER_NAME_PATTERN = `[\\/:*?"<>|]` ) +var FILE_PATTERN = regexp.MustCompile(`\.[a-z]+$`) + /** * file is too common. so we use matter as file. */ diff --git a/code/tool/result/web_result.go b/code/tool/result/web_result.go index 5dda0f1..0b59523 100644 --- a/code/tool/result/web_result.go +++ b/code/tool/result/web_result.go @@ -15,9 +15,10 @@ type WebResult struct { } type WebResultEx struct { - Code int `json:"code"` - Msg string `json:"msg"` - Data interface{} `json:"data"` + Code int `json:"code"` + Msg string `json:"msg"` + Data interface{} `json:"data"` + Error int `json:"error"` } func (this *WebResult) Error() string {