134 lines
3.3 KiB
Go
134 lines
3.3 KiB
Go
package advice
|
|
|
|
import (
|
|
"ai_scheduler/internal/biz"
|
|
"ai_scheduler/internal/config"
|
|
errorcode "ai_scheduler/internal/data/error"
|
|
"ai_scheduler/internal/entitys"
|
|
"ai_scheduler/internal/pkg"
|
|
"ai_scheduler/internal/pkg/file_download"
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"net/url"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// FileService 文件处理
|
|
type FileService struct {
|
|
adviceBiz *biz.AdviceFileBiz
|
|
cfg *config.Config
|
|
adviceProjectBiz *biz.AdviceProjectBiz
|
|
}
|
|
|
|
// NewFileService
|
|
func NewFileService(
|
|
adviceBiz *biz.AdviceFileBiz,
|
|
cfg *config.Config,
|
|
adviceProjectBiz *biz.AdviceProjectBiz,
|
|
) *FileService {
|
|
return &FileService{
|
|
adviceBiz: adviceBiz,
|
|
cfg: cfg,
|
|
adviceProjectBiz: adviceProjectBiz,
|
|
}
|
|
}
|
|
|
|
func (a *FileService) WordAna(c *fiber.Ctx) error {
|
|
req := &entitys.WordAnaReq{}
|
|
if err := c.BodyParser(req); err != nil {
|
|
return err
|
|
}
|
|
if req.ProjectId == 0 {
|
|
return errorcode.ParamErr("ProjectId is empty")
|
|
}
|
|
//项目信息
|
|
projectInfo, err := a.adviceProjectBiz.Info(c.UserContext(), &entitys.AdvicerProjectInfoReq{
|
|
ProjectId: req.ProjectId,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// URL 解码
|
|
fileURL, err := url.PathUnescape(req.WordFileUrl)
|
|
if err != nil {
|
|
return errors.New("URL 解码失败")
|
|
}
|
|
result, _, err := file_download.GetWordTextFromURL(fileURL, file_download.IsWordFile)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ana, err := a.adviceBiz.WordAna(context.Background(), result, projectInfo)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = c.JSON(ana)
|
|
return err
|
|
}
|
|
|
|
func (a *FileService) WordAnat(path string) ([]byte, error) {
|
|
|
|
// URL 解码
|
|
projectInfo, err := a.adviceProjectBiz.Info(context.Background(), &entitys.AdvicerProjectInfoReq{
|
|
ProjectId: 127,
|
|
})
|
|
fileURL, err := url.PathUnescape(path)
|
|
if err != nil {
|
|
return nil, errors.New("URL 解码失败")
|
|
}
|
|
result, _, err := file_download.GetWordTextFromURL(fileURL, file_download.IsWordFile)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ana, err := a.adviceBiz.WordAna(context.Background(), result, projectInfo)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return pkg.JsonByteIgonErr(ana), err
|
|
}
|
|
|
|
// Upload 本地上传文件(聊天记录等),返回可直接访问的 URL
|
|
func (a *FileService) Upload(c *fiber.Ctx) error {
|
|
file, err := c.FormFile("file")
|
|
if err != nil {
|
|
return errorcode.ParamErr("file 不能为空")
|
|
}
|
|
if file.Size > 32*1024*1024 {
|
|
return errorcode.ParamErr("文件过大,最大支持 32MB")
|
|
}
|
|
ext := strings.ToLower(filepath.Ext(file.Filename))
|
|
allowed := map[string]bool{
|
|
".doc": true, ".docx": true, ".pdf": true, ".txt": true, ".md": true,
|
|
".xls": true, ".xlsx": true, ".csv": true, ".json": true,
|
|
}
|
|
if !allowed[ext] {
|
|
return errorcode.ParamErr("不支持的文件类型: " + ext)
|
|
}
|
|
// 按日期分目录存储
|
|
day := time.Now().Format("20060102")
|
|
dir := filepath.Join("upload", day)
|
|
if err = os.MkdirAll(dir, 0755); err != nil {
|
|
return err
|
|
}
|
|
saveName := uuid.NewString() + ext
|
|
dst := filepath.Join(dir, saveName)
|
|
if err = c.SaveFile(file, dst); err != nil {
|
|
return err
|
|
}
|
|
relPath := "/" + filepath.ToSlash(dst)
|
|
res := &entitys.FileUploadRes{
|
|
Path: relPath,
|
|
Url: c.Protocol() + "://" + c.Hostname() + relPath,
|
|
Name: file.Filename,
|
|
Size: file.Size,
|
|
}
|
|
return pkg.HandleResponse(c, res, nil)
|
|
}
|