初始
This commit is contained in:
194
internal/app/system/controller/document.go
Normal file
194
internal/app/system/controller/document.go
Normal file
@ -0,0 +1,194 @@
|
||||
// ==========================================================================
|
||||
// GFast自动生成controller操作代码。
|
||||
// 生成日期:2023-09-16 10:32:04
|
||||
// 生成路径: internal/app/system/controller/document.go
|
||||
// 生成人:gfast
|
||||
// desc:母板
|
||||
// company:云南奇讯科技有限公司
|
||||
// ==========================================================================
|
||||
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/tiger1103/gfast/v3/api/v1/common/coryCommon"
|
||||
"github.com/tiger1103/gfast/v3/api/v1/system"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/service"
|
||||
"github.com/tiger1103/gfast/v3/library/liberr"
|
||||
"github.com/tiger1103/gfast/v3/third/officeWeb365"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type documentController struct {
|
||||
BaseController
|
||||
}
|
||||
|
||||
type Action struct {
|
||||
Type int `json:"type"`
|
||||
UserID string `json:"userid"`
|
||||
}
|
||||
|
||||
type MyStruct struct {
|
||||
Actions []Action `json:"actions"`
|
||||
ForceSaveType string `json:"forcesavetype"`
|
||||
Key string `json:"key"`
|
||||
Status int `json:"status"`
|
||||
URL string `json:"url"`
|
||||
Userdata string `json:"userdata"`
|
||||
Users []string `json:"users"`
|
||||
}
|
||||
|
||||
var Document = new(documentController)
|
||||
|
||||
func (c *documentController) SendFile(ctx context.Context, req *system.SendFileReq) (res *system.SendFileRes, err error) {
|
||||
res = new(system.SendFileRes)
|
||||
|
||||
err, path := officeWeb365.SendFile199(ctx, req.FilePath, req.FileType)
|
||||
res.FilePath = path
|
||||
return
|
||||
}
|
||||
|
||||
// List 列表
|
||||
func (c *documentController) List(ctx context.Context, req *system.DocumentSearchReq) (res *system.DocumentSearchRes, err error) {
|
||||
res, err = service.Document().List(ctx, req)
|
||||
return
|
||||
}
|
||||
|
||||
// AllList 列表
|
||||
func (c *documentController) AllList(ctx context.Context, req *system.AllDocumentSearchReq) (res *system.AllDocumentSearchRes, err error) {
|
||||
res, err = service.Document().AllList(ctx, req)
|
||||
return
|
||||
}
|
||||
|
||||
// Get 获取母板(根据id)
|
||||
func (c *documentController) Get(ctx context.Context, req *system.DocumentGetReq) (res *system.DocumentGetRes, err error) {
|
||||
res = new(system.DocumentGetRes)
|
||||
res.DocumentInfoRes, err = service.Document().GetById(ctx, req.Id)
|
||||
return
|
||||
}
|
||||
|
||||
// Add 本地导入(文件/压缩文件夹)
|
||||
func (c *documentController) Add(ctx context.Context, req *system.DocumentAddReq) (res *system.DocumentAddRes, err error) {
|
||||
err = service.Document().Add(ctx, req)
|
||||
return
|
||||
}
|
||||
|
||||
// NewFolderFunc 新建文件夹
|
||||
func (c *documentController) NewFolderFunc(ctx context.Context, req *system.NewFolderReq) (res *system.NewFolderRes, err error) {
|
||||
err = service.Document().NewFolderFunc(ctx, req)
|
||||
return
|
||||
}
|
||||
|
||||
// Edit 修改母板
|
||||
func (c *documentController) Edit(ctx context.Context, req *system.DocumentEditReq) (res *system.DocumentEditRes, err error) {
|
||||
err = service.Document().Edit(ctx, req)
|
||||
return
|
||||
}
|
||||
|
||||
// Delete 删除母板
|
||||
func (c *documentController) Delete(ctx context.Context, req *system.DocumentDeleteReq) (res *system.DocumentDeleteRes, err error) {
|
||||
err = service.Document().Delete(ctx, req.Ids)
|
||||
return
|
||||
}
|
||||
|
||||
// TreeStructureFunc 树形结构
|
||||
func (c *documentController) TreeStructureFunc(ctx context.Context, req *system.TreeStructureReq) (res *system.TreeStructureRes, err error) {
|
||||
res, err = service.Document().TreeStructureFunc(ctx, req)
|
||||
return
|
||||
}
|
||||
|
||||
// CompressedDownload 根据文件目录进行下载压缩包
|
||||
func (c *documentController) CompressedDownload(ctx context.Context, req *system.CompressedDownloadReq) (res *system.CompressedDownloadRes, err error) {
|
||||
res = new(system.CompressedDownloadRes)
|
||||
download, err := ZIPDownload(ctx, req.RelativePath)
|
||||
if err != nil {
|
||||
return
|
||||
} else {
|
||||
res = download
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// ZIPDownload 生成压缩包
|
||||
func ZIPDownload(ctx context.Context, relativePath string) (res *system.CompressedDownloadRes, err error) {
|
||||
res = new(system.CompressedDownloadRes)
|
||||
sourceDir := strings.ReplaceAll(relativePath, "/file", coryCommon.GetCWD()+"/resource/public") // 指定要压缩的文件夹路径
|
||||
zipFile := coryCommon.GetCWD() + coryCommon.Temporary + "/" + coryCommon.FileName("") + ".zip"
|
||||
err = coryCommon.CreateZipFile(sourceDir, zipFile)
|
||||
if err != nil {
|
||||
err = errors.New("下载失败!")
|
||||
return
|
||||
} else {
|
||||
res.RelativePath = strings.Replace(zipFile, coryCommon.GetCWD()+"/resource/public", "/file", 1)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// UniFileDownload 单文件下载
|
||||
func (c *documentController) UniFileDownload(ctx context.Context, req *system.UniFileDownloadReq) (res *system.UniFileDownloadRes, err error) {
|
||||
res = new(system.UniFileDownloadRes)
|
||||
rpath := coryCommon.FileToFunc(req.RelativePath, 2)
|
||||
g.RequestFromCtx(ctx).Response.ServeFileDownload(rpath)
|
||||
return
|
||||
}
|
||||
|
||||
// TemplateRecycleBinFunc 模板回收站
|
||||
func (c *documentDataController) TemplateRecycleBinFunc(ctx context.Context, req *system.TemplateRecycleBinReq) (res *system.TemplateRecycleBinRes, err error) {
|
||||
err = service.Document().TemplateRecycleBinFunc(ctx, req)
|
||||
return
|
||||
}
|
||||
func (c *busConstructionUserController) ExposeSingleFileUploads(ctx context.Context, req *system.WriteFreelyReq) (res *system.WriteFreelyRes, err error) {
|
||||
res = new(system.WriteFreelyRes)
|
||||
res, err = SingleFileUploads(ctx, req.Path)
|
||||
return
|
||||
}
|
||||
|
||||
// SingleFileUploads 畅写回调地址
|
||||
func SingleFileUploads(ctx context.Context, path string) (res *system.WriteFreelyRes, err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
var my MyStruct
|
||||
r := ghttp.RequestFromCtx(ctx)
|
||||
writer := r.Response.Writer
|
||||
//正文
|
||||
body := r.GetBody()
|
||||
err = json.Unmarshal(body, &my)
|
||||
res = new(system.WriteFreelyRes)
|
||||
status := my.Status
|
||||
if status == 0 {
|
||||
err = errors.New("Invalid status format")
|
||||
} else if status == 2 || status == 6 {
|
||||
downloadURL := my.URL
|
||||
if downloadURL == "" {
|
||||
err = errors.New("Invalid URL format")
|
||||
}
|
||||
resp, err := http.Get(downloadURL)
|
||||
if err != nil {
|
||||
err = errors.New("Error downloading file")
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
fileContent, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
err = errors.New("Error reading file content")
|
||||
}
|
||||
pathForSave := coryCommon.FileToFunc(path, 2)
|
||||
err = os.WriteFile(pathForSave, fileContent, 0644)
|
||||
if err != nil {
|
||||
err = errors.New("Error saving file")
|
||||
}
|
||||
} else if status == 3 || status == 7 {
|
||||
writer.Write([]byte("{\"error\":-1}"))
|
||||
return
|
||||
}
|
||||
writer.Write([]byte("{\"error\":0}"))
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
})
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user