SysAttachmentController.go 7.1 KB

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