system_file_upload_logic.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package system
  2. import (
  3. "context"
  4. "fmt"
  5. "git.i2edu.net/i2/i2-bill-api/internal/svc"
  6. "git.i2edu.net/i2/i2-bill-api/internal/types"
  7. "io"
  8. "net/http"
  9. "os"
  10. "path/filepath"
  11. "git.i2edu.net/i2/go-zero/core/logx"
  12. )
  13. type SystemFileUploadLogic struct {
  14. logx.Logger
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. }
  18. func NewSystemFileUploadLogic(ctx context.Context, svcCtx *svc.ServiceContext) SystemFileUploadLogic {
  19. return SystemFileUploadLogic{
  20. Logger: logx.WithContext(ctx),
  21. ctx: ctx,
  22. svcCtx: svcCtx,
  23. }
  24. }
  25. func (l *SystemFileUploadLogic) SystemFileUpload(r *http.Request) (*types.Response, error) {
  26. // todo: add your logic here and delete this line
  27. userId := l.svcCtx.GetUserIdByJwt(l.ctx)
  28. file, fileHeader, err := r.FormFile("file")
  29. if err != nil {
  30. logx.Error(err.Error())
  31. return &types.Response{500, err.Error(), nil}, nil
  32. }
  33. dir := "asserts/avatar"
  34. _, err = os.Stat(dir)
  35. if err != nil && !os.IsExist(err) {
  36. err := os.MkdirAll(dir, os.ModePerm)
  37. if err != nil {
  38. logx.Error(err.Error())
  39. return &types.Response{500, err.Error(), nil}, nil
  40. }
  41. }
  42. fileName := fmt.Sprintf("%s/%d%s", dir, userId, filepath.Ext(fileHeader.Filename))
  43. newFile, err := os.OpenFile(fileName, os.O_TRUNC|os.O_CREATE|os.O_RDWR, 0777)
  44. if err != nil {
  45. logx.Error(err.Error())
  46. return &types.Response{500, err.Error(), nil}, nil
  47. }
  48. _, err = io.Copy(newFile, file)
  49. if err != nil {
  50. logx.Error(err.Error())
  51. return &types.Response{500, err.Error(), nil}, nil
  52. }
  53. return &types.Response{200, "", fileName}, nil
  54. //
  55. //file, fInfo, err := r.FormFile("file")
  56. //if err != nil {
  57. // logx.Error(err.Error())
  58. // return &types.Response{500, err.Error(), nil}, nil
  59. //}
  60. //defer file.Close()
  61. //
  62. //var attach *sysmodel.SysAttachment = nil
  63. //if _attachementUpDownloader != nil{
  64. // attach, err = _attachementUpDownloader.Upload(file, fInfo)
  65. // if err !=nil{
  66. // return nil, err
  67. // }
  68. //}else{
  69. // return nil, errors.New("附件上传下载接口为nil")
  70. //}
  71. //
  72. //userId := c.Ctx.GetString("user_id")
  73. //
  74. //if attach.Id == ""{
  75. // attach.Id = sysutils.NewUUID()
  76. //}
  77. //if attach.Size <= 0{
  78. // attach.Size = int32((fInfo.Size))
  79. //}
  80. //attach.Name = fInfo.Filename
  81. //attach.CreateBy = userId
  82. //attach.CreateTime = time.Now()
  83. //attach.Ext = path.Ext(attach.Name)
  84. //
  85. //_, err = c.PlatformDbEngine.InsertOne(attach)
  86. //if err != nil {
  87. // syslogs.Error("保存附件出错了:", err)
  88. // return nil, err
  89. //}
  90. //return attach, nil
  91. }
  92. /**
  93. * @brief: 默认
  94. */
  95. type defaultAttachUpDownloader struct {
  96. }
  97. /**
  98. * @brief: 附件上传接口
  99. * @param1 file: post 多媒体file
  100. * @param1 header: 文件相头信息
  101. * @return1: 附件信息,主要包括id,url, hash和大小。其他字段会自动设置
  102. * @return2: 错误信息
  103. */
  104. //func(d *defaultAttachUpDownloader)Upload(file multipart.File, header *multipart.FileHeader)(*sysmodel.SysAttachment, error){
  105. // fileDir := fmt.Sprintf("files/")
  106. // if err := mkdir(fileDir); err != nil {
  107. // syslogs.Info("文件夹创建失败")
  108. // return nil, err
  109. // }
  110. // attachId := sysutils.NewUUID()
  111. // filePath := fileDir + attachId
  112. // fW, err := os.Create(filePath)
  113. // if err != nil {
  114. // syslogs.Info("文件创建失败")
  115. // return nil, err
  116. // }
  117. // defer fW.Close()
  118. // length, err := io.Copy(fW, file)
  119. // if err != nil {
  120. // syslogs.Info("文件保存失败")
  121. // return nil, err
  122. // }
  123. //
  124. // attach := &sysmodel.SysAttachment{}
  125. // attach.Id = attachId
  126. // attach.Url = "/api/v1/sys_attachment/download?id=" + attach.Id
  127. // attach.Hash = ""
  128. // attach.Size = int32(length)
  129. //
  130. // return attach, nil
  131. //}
  132. //
  133. ///**
  134. // * @brief: 附件下载
  135. // * @param1 ctx: 上下文
  136. // * @param2 id: 附件记录的id
  137. // */
  138. //func(d *defaultAttachUpDownloader)Download(c *entitys.CtrlContext, attach *sysmodel.SysAttachment){
  139. // filePath := "files/" + attach.Id
  140. //
  141. // c.Ctx.Writer.Header().Add("Content-Disposition", "attachment;filename=\""+sysutils.FormatForBrowse(c.Ctx.Request.UserAgent(), attach.Name)+"\"")
  142. // c.Ctx.Writer.Header().Add("Content-Type", mime.TypeByExtension(attach.Ext))
  143. // c.Ctx.Writer.Header().Add("Content-Length", strconv.FormatInt(int64(attach.Size), 10))
  144. // c.Ctx.Writer.Header().Add("Accept-Ranges", "bytes")
  145. //
  146. // if !sysutils.Exists(filePath) {
  147. // c.Ctx.Writer.WriteHeader(400)
  148. // return
  149. // }
  150. // fmt.Println("---->", c.Ctx.Writer.Header().Get("Content-Disposition"))
  151. // fmt.Println("---->", c.Ctx.Writer.Header().Get("Content-Type"))
  152. // fmt.Println("---->", c.Ctx.Writer.Header().Get("Content-Length"))
  153. // fmt.Println("---->", c.Ctx.Writer.Header().Get("Accept-Ranges"))
  154. //
  155. // c.Ctx.File(filePath)
  156. //}
  157. //