|
|
@@ -2,13 +2,16 @@ package system
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "encoding/json"
|
|
|
"fmt"
|
|
|
+ "git.i2edu.net/i2/i2-bill-api/internal/global"
|
|
|
"git.i2edu.net/i2/i2-bill-api/internal/svc"
|
|
|
"git.i2edu.net/i2/i2-bill-api/internal/types"
|
|
|
"io"
|
|
|
"net/http"
|
|
|
"os"
|
|
|
"path/filepath"
|
|
|
+ "strings"
|
|
|
|
|
|
"git.i2edu.net/i2/go-zero/core/logx"
|
|
|
)
|
|
|
@@ -29,6 +32,31 @@ func NewSystemFileUploadLogic(ctx context.Context, svcCtx *svc.ServiceContext) S
|
|
|
|
|
|
func (l *SystemFileUploadLogic) SystemFileUpload(r *http.Request) (*types.Response, error) {
|
|
|
// todo: add your logic here and delete this line
|
|
|
+ file, fileHeader, err := r.FormFile("file")
|
|
|
+ if err != nil {
|
|
|
+ logx.Error(err.Error())
|
|
|
+ return &types.Response{500, err.Error(), nil}, nil
|
|
|
+ }
|
|
|
+ res, err := global.NewAliYunOssUpDownloader().Upload(file, fileHeader)
|
|
|
+ if err != nil {
|
|
|
+ logx.Error(err.Error())
|
|
|
+ return &types.Response{500, err.Error(), nil}, nil
|
|
|
+ }
|
|
|
+ resp := make(map[string]interface{})
|
|
|
+ resByte, err := json.Marshal(res)
|
|
|
+ if err != nil {
|
|
|
+ logx.Error(err.Error())
|
|
|
+ return &types.Response{500, err.Error(), nil}, nil
|
|
|
+ }
|
|
|
+ if err := json.Unmarshal(resByte, &resp); err != nil {
|
|
|
+ logx.Error(err.Error())
|
|
|
+ return &types.Response{500, err.Error(), nil}, nil
|
|
|
+ }
|
|
|
+ resp["full_url"] = strings.TrimLeft(l.svcCtx.Config.AliYunOss.FileUrl, ",") + "/" + strings.TrimRight(res.Url, "/")
|
|
|
+ return &types.Response{200, "", resp}, nil
|
|
|
+}
|
|
|
+
|
|
|
+func localFileUpload(l SystemFileUploadLogic, r *http.Request) (*types.Response, error) {
|
|
|
userId := l.svcCtx.GetUserIdByJwt(l.ctx)
|
|
|
file, fileHeader, err := r.FormFile("file")
|
|
|
if err != nil {
|
|
|
@@ -56,112 +84,4 @@ func (l *SystemFileUploadLogic) SystemFileUpload(r *http.Request) (*types.Respon
|
|
|
return &types.Response{500, err.Error(), nil}, nil
|
|
|
}
|
|
|
return &types.Response{200, "", fileName}, nil
|
|
|
-
|
|
|
- //
|
|
|
- //file, fInfo, err := r.FormFile("file")
|
|
|
- //if err != nil {
|
|
|
- // logx.Error(err.Error())
|
|
|
- // return &types.Response{500, err.Error(), nil}, nil
|
|
|
- //}
|
|
|
- //defer file.Close()
|
|
|
- //
|
|
|
- //var attach *sysmodel.SysAttachment = nil
|
|
|
- //if _attachementUpDownloader != nil{
|
|
|
- // attach, err = _attachementUpDownloader.Upload(file, fInfo)
|
|
|
- // if err !=nil{
|
|
|
- // return nil, err
|
|
|
- // }
|
|
|
- //}else{
|
|
|
- // return nil, errors.New("附件上传下载接口为nil")
|
|
|
- //}
|
|
|
- //
|
|
|
- //userId := c.Ctx.GetString("user_id")
|
|
|
- //
|
|
|
- //if attach.Id == ""{
|
|
|
- // attach.Id = sysutils.NewUUID()
|
|
|
- //}
|
|
|
- //if attach.Size <= 0{
|
|
|
- // attach.Size = int32((fInfo.Size))
|
|
|
- //}
|
|
|
- //attach.Name = fInfo.Filename
|
|
|
- //attach.CreateBy = userId
|
|
|
- //attach.CreateTime = time.Now()
|
|
|
- //attach.Ext = path.Ext(attach.Name)
|
|
|
- //
|
|
|
- //_, err = c.PlatformDbEngine.InsertOne(attach)
|
|
|
- //if err != nil {
|
|
|
- // syslogs.Error("保存附件出错了:", err)
|
|
|
- // return nil, err
|
|
|
- //}
|
|
|
-
|
|
|
- //return attach, nil
|
|
|
-
|
|
|
}
|
|
|
-
|
|
|
-/**
|
|
|
- * @brief: 默认
|
|
|
- */
|
|
|
-type defaultAttachUpDownloader struct {
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * @brief: 附件上传接口
|
|
|
- * @param1 file: post 多媒体file
|
|
|
- * @param1 header: 文件相头信息
|
|
|
- * @return1: 附件信息,主要包括id,url, hash和大小。其他字段会自动设置
|
|
|
- * @return2: 错误信息
|
|
|
- */
|
|
|
-//func(d *defaultAttachUpDownloader)Upload(file multipart.File, header *multipart.FileHeader)(*sysmodel.SysAttachment, error){
|
|
|
-// fileDir := fmt.Sprintf("files/")
|
|
|
-// if err := mkdir(fileDir); err != nil {
|
|
|
-// syslogs.Info("文件夹创建失败")
|
|
|
-// return nil, err
|
|
|
-// }
|
|
|
-// attachId := sysutils.NewUUID()
|
|
|
-// filePath := fileDir + attachId
|
|
|
-// fW, err := os.Create(filePath)
|
|
|
-// if err != nil {
|
|
|
-// syslogs.Info("文件创建失败")
|
|
|
-// return nil, err
|
|
|
-// }
|
|
|
-// defer fW.Close()
|
|
|
-// length, err := io.Copy(fW, file)
|
|
|
-// if err != nil {
|
|
|
-// syslogs.Info("文件保存失败")
|
|
|
-// return nil, err
|
|
|
-// }
|
|
|
-//
|
|
|
-// attach := &sysmodel.SysAttachment{}
|
|
|
-// attach.Id = attachId
|
|
|
-// attach.Url = "/api/v1/sys_attachment/download?id=" + attach.Id
|
|
|
-// attach.Hash = ""
|
|
|
-// attach.Size = int32(length)
|
|
|
-//
|
|
|
-// return attach, nil
|
|
|
-//}
|
|
|
-//
|
|
|
-///**
|
|
|
-// * @brief: 附件下载
|
|
|
-// * @param1 ctx: 上下文
|
|
|
-// * @param2 id: 附件记录的id
|
|
|
-// */
|
|
|
-//func(d *defaultAttachUpDownloader)Download(c *entitys.CtrlContext, attach *sysmodel.SysAttachment){
|
|
|
-// filePath := "files/" + attach.Id
|
|
|
-//
|
|
|
-// c.Ctx.Writer.Header().Add("Content-Disposition", "attachment;filename=\""+sysutils.FormatForBrowse(c.Ctx.Request.UserAgent(), attach.Name)+"\"")
|
|
|
-// c.Ctx.Writer.Header().Add("Content-Type", mime.TypeByExtension(attach.Ext))
|
|
|
-// c.Ctx.Writer.Header().Add("Content-Length", strconv.FormatInt(int64(attach.Size), 10))
|
|
|
-// c.Ctx.Writer.Header().Add("Accept-Ranges", "bytes")
|
|
|
-//
|
|
|
-// if !sysutils.Exists(filePath) {
|
|
|
-// c.Ctx.Writer.WriteHeader(400)
|
|
|
-// return
|
|
|
-// }
|
|
|
-// fmt.Println("---->", c.Ctx.Writer.Header().Get("Content-Disposition"))
|
|
|
-// fmt.Println("---->", c.Ctx.Writer.Header().Get("Content-Type"))
|
|
|
-// fmt.Println("---->", c.Ctx.Writer.Header().Get("Content-Length"))
|
|
|
-// fmt.Println("---->", c.Ctx.Writer.Header().Get("Accept-Ranges"))
|
|
|
-//
|
|
|
-// c.Ctx.File(filePath)
|
|
|
-//}
|
|
|
-//
|