Browse Source

v0.3.6 add onlyoffice callbackEdit

master
han2015 5 months ago
parent
commit
53fae4906b
  1. 71
      code/rest/matter_controller.go
  2. 2
      code/rest/matter_model.go
  3. 1
      code/tool/result/web_result.go

71
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 {
@ -85,6 +92,7 @@ func (this *MatterController) RegisterRoutes() map[string]func(writer http.Respo
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/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")

2
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.
*/

1
code/tool/result/web_result.go

@ -18,6 +18,7 @@ type WebResultEx struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
Error int `json:"error"`
}
func (this *WebResult) Error() string {

Loading…
Cancel
Save