exa_file_upload_download.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package example
  2. import (
  3. "errors"
  4. "mime/multipart"
  5. "strings"
  6. "github.com/flipped-aurora/gin-vue-admin/server/global"
  7. "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
  8. "github.com/flipped-aurora/gin-vue-admin/server/model/example"
  9. "github.com/flipped-aurora/gin-vue-admin/server/utils/upload"
  10. )
  11. //@author: [piexlmax](https://github.com/piexlmax)
  12. //@function: Upload
  13. //@description: 创建文件上传记录
  14. //@param: file model.ExaFileUploadAndDownload
  15. //@return: error
  16. func (e *FileUploadAndDownloadService) Upload(file example.ExaFileUploadAndDownload) error {
  17. return global.GVA_DB.Create(&file).Error
  18. }
  19. //@author: [piexlmax](https://github.com/piexlmax)
  20. //@function: FindFile
  21. //@description: 删除文件切片记录
  22. //@param: id uint
  23. //@return: error, model.ExaFileUploadAndDownload
  24. func (e *FileUploadAndDownloadService) FindFile(id uint) (error, example.ExaFileUploadAndDownload) {
  25. var file example.ExaFileUploadAndDownload
  26. err := global.GVA_DB.Where("id = ?", id).First(&file).Error
  27. return err, file
  28. }
  29. //@author: [piexlmax](https://github.com/piexlmax)
  30. //@function: DeleteFile
  31. //@description: 删除文件记录
  32. //@param: file model.ExaFileUploadAndDownload
  33. //@return: err error
  34. func (e *FileUploadAndDownloadService) DeleteFile(file example.ExaFileUploadAndDownload) (err error) {
  35. var fileFromDb example.ExaFileUploadAndDownload
  36. err, fileFromDb = e.FindFile(file.ID)
  37. oss := upload.NewOss()
  38. if err = oss.DeleteFile(fileFromDb.Key); err != nil {
  39. return errors.New("文件删除失败")
  40. }
  41. err = global.GVA_DB.Where("id = ?", file.ID).Unscoped().Delete(&file).Error
  42. return err
  43. }
  44. //@author: [piexlmax](https://github.com/piexlmax)
  45. //@function: GetFileRecordInfoList
  46. //@description: 分页获取数据
  47. //@param: info request.PageInfo
  48. //@return: err error, list interface{}, total int64
  49. func (e *FileUploadAndDownloadService) GetFileRecordInfoList(info request.PageInfo) (err error, list interface{}, total int64) {
  50. limit := info.PageSize
  51. offset := info.PageSize * (info.Page - 1)
  52. db := global.GVA_DB
  53. var fileLists []example.ExaFileUploadAndDownload
  54. err = db.Find(&fileLists).Count(&total).Error
  55. err = db.Limit(limit).Offset(offset).Order("updated_at desc").Find(&fileLists).Error
  56. return err, fileLists, total
  57. }
  58. func (e *FileUploadAndDownloadService) GetFileRecordInfoByList(assignmentId uint) (err error, list []example.ExaFileUploadAndDownload) {
  59. db := global.GVA_DB
  60. var fileLists []example.ExaFileUploadAndDownload
  61. err = db.Where("assignment_id=?", assignmentId).Order("updated_at desc").Find(&fileLists).Error
  62. return err, fileLists
  63. }
  64. //@author: [piexlmax](https://github.com/piexlmax)
  65. //@function: UploadFile
  66. //@description: 根据配置文件判断是文件上传到本地或者七牛云
  67. //@param: header *multipart.FileHeader, noSave string
  68. //@return: err error, file model.ExaFileUploadAndDownload
  69. func (e *FileUploadAndDownloadService) UploadFile(header *multipart.FileHeader, noSave string, assignmentId string) (err error, file example.ExaFileUploadAndDownload) {
  70. oss := upload.NewOss()
  71. filePath, key, uploadErr := oss.UploadFile(header)
  72. if uploadErr != nil {
  73. panic(err)
  74. }
  75. if noSave == "0" {
  76. s := strings.Split(header.Filename, ".")
  77. var f example.ExaFileUploadAndDownload
  78. if assignmentId != "" {
  79. f = example.ExaFileUploadAndDownload{
  80. Url: filePath,
  81. Name: header.Filename,
  82. Tag: s[len(s)-1],
  83. Key: key,
  84. AssignmentId: assignmentId,
  85. }
  86. } else {
  87. f = example.ExaFileUploadAndDownload{
  88. Url: filePath,
  89. Name: header.Filename,
  90. Tag: s[len(s)-1],
  91. Key: key,
  92. }
  93. }
  94. return e.Upload(f), f
  95. }
  96. return
  97. }