2
0

exa_file_upload_download.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/example"
  8. "github.com/flipped-aurora/gin-vue-admin/server/model/example/request"
  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: model.ExaFileUploadAndDownload, error
  24. func (e *FileUploadAndDownloadService) FindFile(id uint) (example.ExaFileUploadAndDownload, error) {
  25. var file example.ExaFileUploadAndDownload
  26. err := global.GVA_DB.Where("id = ?", id).First(&file).Error
  27. return file, err
  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. fileFromDb, err = e.FindFile(file.ID)
  37. if err != nil {
  38. return
  39. }
  40. oss := upload.NewOss()
  41. if err = oss.DeleteFile(fileFromDb.Key); err != nil {
  42. return errors.New("文件删除失败")
  43. }
  44. err = global.GVA_DB.Where("id = ?", file.ID).Unscoped().Delete(&file).Error
  45. return err
  46. }
  47. // EditFileName 编辑文件名或者备注
  48. func (e *FileUploadAndDownloadService) EditFileName(file example.ExaFileUploadAndDownload) (err error) {
  49. var fileFromDb example.ExaFileUploadAndDownload
  50. return global.GVA_DB.Where("id = ?", file.ID).First(&fileFromDb).Update("name", file.Name).Error
  51. }
  52. //@author: [piexlmax](https://github.com/piexlmax)
  53. //@function: GetFileRecordInfoList
  54. //@description: 分页获取数据
  55. //@param: info request.ExaAttachmentCategorySearch
  56. //@return: list interface{}, total int64, err error
  57. func (e *FileUploadAndDownloadService) GetFileRecordInfoList(info request.ExaAttachmentCategorySearch) (list []example.ExaFileUploadAndDownload, total int64, err error) {
  58. limit := info.PageSize
  59. offset := info.PageSize * (info.Page - 1)
  60. db := global.GVA_DB.Model(&example.ExaFileUploadAndDownload{})
  61. if len(info.Keyword) > 0 {
  62. db = db.Where("name LIKE ?", "%"+info.Keyword+"%")
  63. }
  64. if info.ClassId > 0 {
  65. db = db.Where("class_id = ?", info.ClassId)
  66. }
  67. err = db.Count(&total).Error
  68. if err != nil {
  69. return
  70. }
  71. err = db.Limit(limit).Offset(offset).Order("id desc").Find(&list).Error
  72. return list, total, err
  73. }
  74. //@author: [piexlmax](https://github.com/piexlmax)
  75. //@function: UploadFile
  76. //@description: 根据配置文件判断是文件上传到本地或者七牛云
  77. //@param: header *multipart.FileHeader, noSave string
  78. //@return: file model.ExaFileUploadAndDownload, err error
  79. func (e *FileUploadAndDownloadService) UploadFile(header *multipart.FileHeader, noSave string, classId int) (file example.ExaFileUploadAndDownload, err error) {
  80. oss := upload.NewOss()
  81. filePath, key, uploadErr := oss.UploadFile(header)
  82. if uploadErr != nil {
  83. return file, uploadErr
  84. }
  85. s := strings.Split(header.Filename, ".")
  86. f := example.ExaFileUploadAndDownload{
  87. Url: filePath,
  88. Name: header.Filename,
  89. ClassId: classId,
  90. Tag: s[len(s)-1],
  91. Key: key,
  92. }
  93. if noSave == "0" {
  94. return f, e.Upload(f)
  95. }
  96. return f, nil
  97. }
  98. //@author: [piexlmax](https://github.com/piexlmax)
  99. //@function: ImportURL
  100. //@description: 导入URL
  101. //@param: file model.ExaFileUploadAndDownload
  102. //@return: error
  103. func (e *FileUploadAndDownloadService) ImportURL(file *[]example.ExaFileUploadAndDownload) error {
  104. return global.GVA_DB.Create(&file).Error
  105. }