1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- package logic
- import (
- "context"
- "fmt"
- "io/ioutil"
- "net/http"
- "strings"
- "app.yhyue.com/moapp/jybase/common"
- fpb "bp.jydev.jianyu360.cn/BaseService/fileCenter/rpc/pb"
- cm "bp.jydev.jianyu360.cn/CRM/application/api/common"
- "bp.jydev.jianyu360.cn/CRM/application/api/internal/svc"
- "bp.jydev.jianyu360.cn/CRM/application/api/internal/types"
- "github.com/zeromicro/go-zero/core/logx"
- )
- type FileUploadLogic struct {
- logx.Logger
- ctx context.Context
- svcCtx *svc.ServiceContext
- r *http.Request
- }
- func NewFileUploadLogic(ctx context.Context, svcCtx *svc.ServiceContext, r *http.Request) *FileUploadLogic {
- return &FileUploadLogic{
- Logger: logx.WithContext(ctx),
- ctx: ctx,
- svcCtx: svcCtx,
- r: r,
- }
- }
- func (l *FileUploadLogic) FileUpload(req *types.FileUploadReq) (resp *types.Reply, err error) {
- resp = &types.Reply{}
- file, header, _ := l.r.FormFile("file")
- defer file.Close()
- bt, _ := ioutil.ReadAll(file)
- fmt.Println(cm.C.OssBucketName, cm.C.OssUrl, header.Filename)
- up, err := cm.FileCenterRpc.Upload(l.ctx, &fpb.UploadReq{
- File: bt,
- OssBucketName: cm.C.OssBucketName,
- OssUrl: cm.C.OssUrl,
- Name: header.Filename,
- })
- if up == nil || up.Url == "" {
- resp.Error_code = -1
- resp.Error_msg = "上传失败"
- return
- }
- key := up.Key
- size := fmt.Sprintf("%.4f", common.Float64All(header.Size)/1024)
- for {
- if strings.HasSuffix(size, "0") {
- size = strings.TrimRight(size, "0")
- continue
- }
- if strings.HasSuffix(size, ".") {
- size = strings.TrimRight(size, ".")
- break
- }
- break
- }
- fileSize := size + "KB"
- var data map[string]interface{}
- fileTypeTmp := strings.Split(header.Filename, ".")
- var ftype string
- if len(fileTypeTmp) > 0 && len(fileTypeTmp) == 2 {
- ftype = strings.Split(header.Filename, ".")[1]
- }
- data = map[string]interface{}{
- "filename": header.Filename,
- "ftype": ftype,
- "fid": key,
- "size": fileSize,
- "ossurl": cm.C.OssUrl,
- "url": up.Url,
- }
- resp.Data = data
- return
- }
|