SysAttachmentController.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package partial
  2. import (
  3. "crypto/sha1"
  4. "fmt"
  5. "git.qianqiusoft.com/qianqiusoft/light-apiengine/entitys"
  6. syslogs "git.qianqiusoft.com/qianqiusoft/light-apiengine/logs"
  7. sysmodel "git.qianqiusoft.com/qianqiusoft/light-apiengine/models"
  8. sysutils "git.qianqiusoft.com/qianqiusoft/light-apiengine/utils"
  9. "io"
  10. "io/ioutil"
  11. "os"
  12. "path"
  13. "time"
  14. )
  15. // _Upload
  16. // @Title _Upload
  17. // @Description 上传文件
  18. // @Success 200 {object} Account
  19. // @Failure 403 :id is empty
  20. func SysAttachment_Upload(c *entitys.CtrlContext) {
  21. attach, err := doUpload2(c)
  22. if err == nil {
  23. c.Ctx.JSON(200, sysmodel.SysReturn{200, "", attach.Id})
  24. syslogs.Debug("上传附件成功:", attach.Name, attach.Size)
  25. } else {
  26. c.Ctx.JSON(500, sysmodel.SysReturn{500, err.Error(), nil})
  27. }
  28. }
  29. // _Download
  30. // @Title _Download
  31. // @Description 下载文件
  32. // @Param id string false "文件ID"
  33. // @Success 200 {object} Account
  34. // @Failure 403 :id is empty
  35. func SysAttachment_Download(c *entitys.CtrlContext) {
  36. attrId := c.Ctx.Query("id")
  37. filePath := fmt.Sprintf("files/%s", attrId)
  38. c.Ctx.Header("Content-Type", "image/png")
  39. c.Ctx.Header("Content-Disposition", fmt.Sprintf("inline; filename=\"%s\"", attrId))
  40. file, err := ioutil.ReadFile(filePath)
  41. if err != nil {
  42. c.Ctx.JSON(500, sysmodel.SysReturn{500, err.Error(), nil})
  43. return
  44. }
  45. c.Ctx.Writer.Write(file)
  46. ////gt := c.Ctx.Query("get_thumb")
  47. //var attach sysmodel.SysAttachment
  48. //exist, err := c.Db.SqlMapClient("selectone_sys_attachment", &map[string]interface{}{"id": attrId}).Get(&attach)
  49. //if !exist && err == nil {
  50. // err = errors.New("record does not exist")
  51. //}
  52. //if err != nil {
  53. // syslogs.Error("获取附件出错了:", err)
  54. // c.Ctx.JSON(500, sysmodel.SysReturn{500, err.Error(), nil})
  55. // return
  56. //}
  57. //
  58. ////domain := c.Ctx.GetString("domain")
  59. ////filePath := fmt.Sprintf("files/%s/%s", domain, attrId)
  60. //filePath := fmt.Sprintf("files/%s", attrId)
  61. //file, err := os.Open(filePath)
  62. //if err != nil {
  63. // c.Ctx.JSON(500, sysmodel.SysReturn{500, err.Error(), nil})
  64. // return
  65. //}
  66. //defer file.Close()
  67. //
  68. ///*durl := attach.Url
  69. //if gt != "" {
  70. // durl += "?getthumb=" + gt
  71. //}
  72. //resultBody, err := sysutils.GetFile(durl)
  73. //if err != nil {
  74. // syslogs.Error("下载附件出错了:", err)
  75. // c.Ctx.JSON(500, sysmodel.SysReturn{500, err.Error(), nil})
  76. // return
  77. //}
  78. //defer resultBody.Close()*/
  79. //
  80. //bbuf := bytes.NewBuffer([]byte{})
  81. //
  82. //c.Ctx.Header("Content-Disposition", "attachment;filename=\""+sysutils.FormatForBrowse(c.Ctx.Request.UserAgent(), attach.Name)+"\"")
  83. ////c.Ctx.Header("Content-Type", strings.Replace(mime.TypeByExtension(attach.Ext), "charset=utf-8", "", -1))
  84. //c.Ctx.Header("Content-Type", "image/jpeg")
  85. //
  86. //buff := make([]byte, 1024)
  87. //var ssize int64 = 0
  88. //
  89. //for {
  90. // n, err := io.ReadFull(file, buff)
  91. // if err != nil && err != io.ErrUnexpectedEOF && err != io.EOF {
  92. // syslogs.Error("下载附件出错了:", err)
  93. // c.Ctx.JSON(500, sysmodel.SysReturn{500, err.Error(), nil})
  94. // return
  95. // }
  96. // if n <= 0 {
  97. // break
  98. // }
  99. // bbuf.Write(buff[:n])
  100. // ssize += int64(n)
  101. //}
  102. //
  103. //c.Ctx.Header("Content-Length", strconv.FormatInt(ssize, 10))
  104. //_, err = c.Ctx.Writer.Write(bbuf.Bytes())
  105. //if err != nil {
  106. // syslogs.Error("输出流断开:", attach.Name, attach.Size)
  107. // c.Ctx.JSON(500, sysmodel.SysReturn{500, err.Error(), nil})
  108. // return
  109. //}
  110. //
  111. //syslogs.Debug("下载附件成功:", attach.Name, attach.Size)
  112. //c.Ctx.JSON(200, sysmodel.SysReturn{200, "", attach.Id})
  113. }
  114. // _Delete
  115. // @Title _Delete
  116. // @Description 删除文件
  117. // @Param id string false "文件ID"
  118. // @Success 200 {object} Account
  119. // @Failure 403 :id is empty
  120. func SysAttachment_Delete(c *entitys.CtrlContext) {
  121. attrId := c.Ctx.Query("id")
  122. attach := &sysmodel.SysAttachment{}
  123. attach.Id = attrId
  124. _, err := c.Db.Id(attrId).Delete(attach)
  125. if err != nil {
  126. syslogs.Error("删除附件出错了:", err)
  127. c.Ctx.JSON(500, sysmodel.SysReturn{500, err.Error(), nil})
  128. return
  129. }
  130. syslogs.Debug("删除附件:", attrId)
  131. c.Ctx.JSON(200, sysmodel.SysReturn{200, "", attrId})
  132. }
  133. func doUpload(c *entitys.CtrlContext) (*sysmodel.SysAttachment, error) {
  134. file, fInfo, err := c.Ctx.Request.FormFile("file")
  135. if err != nil {
  136. syslogs.Error("上传出错了:", err)
  137. return nil, err
  138. }
  139. download, hash, length, err := sysutils.PostFile(file)
  140. if err != nil {
  141. return nil, err
  142. }
  143. userId := c.Ctx.GetString("user_id")
  144. attach := &sysmodel.SysAttachment{}
  145. attach.Id = sysutils.NewUUID()
  146. attach.Name = fInfo.Filename
  147. attach.Size = length
  148. attach.Url = download
  149. attach.CreateBy = userId
  150. attach.CreateTime = time.Now()
  151. attach.Ext = path.Ext(attach.Name)
  152. attach.Hash = hash
  153. _, err = c.Db.InsertOne(attach)
  154. if err != nil {
  155. syslogs.Error("保存附件出错了:", err)
  156. return nil, err
  157. }
  158. return attach, nil
  159. }
  160. func doUpload2(c *entitys.CtrlContext) (*sysmodel.SysAttachment, error) {
  161. file, fInfo, err := c.Ctx.Request.FormFile("file")
  162. if err != nil {
  163. syslogs.Error("上传出错了:", err)
  164. return nil, err
  165. }
  166. defer file.Close()
  167. //domain := c.Ctx.GetString("domain")
  168. //fileDir := fmt.Sprintf("files/%s/", domain)
  169. fileDir := fmt.Sprintf("files/")
  170. if err = mkdir(fileDir); err != nil {
  171. syslogs.Info("文件夹创建失败")
  172. return nil, err
  173. }
  174. attachId := sysutils.NewUUID()
  175. filePath := fileDir + attachId
  176. fW, err := os.Create(filePath)
  177. if err != nil {
  178. syslogs.Info("文件创建失败")
  179. return nil, err
  180. }
  181. defer fW.Close()
  182. length, err := io.Copy(fW, file)
  183. if err != nil {
  184. syslogs.Info("文件保存失败")
  185. return nil, err
  186. }
  187. userId := c.Ctx.GetString("user_id")
  188. attach := &sysmodel.SysAttachment{}
  189. attach.Id = attachId
  190. attach.Name = fInfo.Filename
  191. attach.Size = int32(length)
  192. attach.Url = "/api/v1/sys_attachment/download?id=" + attach.Id
  193. attach.CreateBy = userId
  194. attach.CreateTime = time.Now()
  195. attach.Ext = path.Ext(attach.Name)
  196. attach.Hash = FileHasH(filePath)
  197. //_, err = c.Db.InsertOne(attach)
  198. _, err = c.PlatformDbEngine.InsertOne(attach)
  199. if err != nil {
  200. syslogs.Error("保存附件出错了:", err)
  201. return nil, err
  202. }
  203. return attach, nil
  204. }
  205. func mkdir(dir string) error {
  206. _, err := os.Stat(dir)
  207. if err != nil {
  208. err = os.MkdirAll(dir, os.ModePerm)
  209. }
  210. return err
  211. }
  212. func FileHasH(filePath string) string {
  213. bytes, err := ioutil.ReadFile(filePath)
  214. if err == nil && len(bytes) > 0 {
  215. return fmt.Sprintf("%x", sha1.Sum(bytes))
  216. }
  217. return ""
  218. }
  219. func __none_func_sys_attachment__(params ...interface{}) bool {
  220. return true
  221. }