| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- package system
- import (
- "context"
- "fmt"
- "git.i2edu.net/i2/i2-bill-api/internal/svc"
- "git.i2edu.net/i2/i2-bill-api/internal/types"
- "io"
- "net/http"
- "os"
- "path/filepath"
- "git.i2edu.net/i2/go-zero/core/logx"
- )
- type SystemFileUploadLogic struct {
- logx.Logger
- ctx context.Context
- svcCtx *svc.ServiceContext
- }
- func NewSystemFileUploadLogic(ctx context.Context, svcCtx *svc.ServiceContext) SystemFileUploadLogic {
- return SystemFileUploadLogic{
- Logger: logx.WithContext(ctx),
- ctx: ctx,
- svcCtx: svcCtx,
- }
- }
- func (l *SystemFileUploadLogic) SystemFileUpload(r *http.Request) (*types.Response, error) {
- // todo: add your logic here and delete this line
- userId := l.svcCtx.GetUserIdByJwt(l.ctx)
- file, fileHeader, err := r.FormFile("file")
- if err != nil {
- logx.Error(err.Error())
- return &types.Response{500, err.Error(), nil}, nil
- }
- dir := "asserts/avatar"
- _, err = os.Stat(dir)
- if err != nil && !os.IsExist(err) {
- err := os.MkdirAll(dir, os.ModePerm)
- if err != nil {
- logx.Error(err.Error())
- return &types.Response{500, err.Error(), nil}, nil
- }
- }
- fileName := fmt.Sprintf("%s/%d%s", dir, userId, filepath.Ext(fileHeader.Filename))
- newFile, err := os.OpenFile(fileName, os.O_TRUNC|os.O_CREATE|os.O_RDWR, 0777)
- if err != nil {
- logx.Error(err.Error())
- return &types.Response{500, err.Error(), nil}, nil
- }
- _, err = io.Copy(newFile, file)
- if err != nil {
- logx.Error(err.Error())
- 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)
- //}
- //
|