exa_file_upload_download.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package example
  2. import (
  3. "errors"
  4. "mime/multipart"
  5. "server/global"
  6. "server/model/common/request"
  7. "server/model/example"
  8. "server/utils/upload"
  9. "strings"
  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.PageInfo
  56. //@return: list interface{}, total int64, err error
  57. func (e *FileUploadAndDownloadService) GetFileRecordInfoList(info request.PageInfo) (list interface{}, total int64, err error) {
  58. limit := info.PageSize
  59. offset := info.PageSize * (info.Page - 1)
  60. keyword := info.Keyword
  61. db := global.GVA_DB.Model(&example.ExaFileUploadAndDownload{})
  62. var fileLists []example.ExaFileUploadAndDownload
  63. if len(keyword) > 0 {
  64. db = db.Where("name LIKE ?", "%"+keyword+"%")
  65. }
  66. err = db.Count(&total).Error
  67. if err != nil {
  68. return
  69. }
  70. err = db.Limit(limit).Offset(offset).Order("updated_at desc").Find(&fileLists).Error
  71. return fileLists, total, err
  72. }
  73. //@author: [piexlmax](https://github.com/piexlmax)
  74. //@function: UploadFile
  75. //@description: 根据配置文件判断是文件上传到本地或者七牛云
  76. //@param: header *multipart.FileHeader, noSave string
  77. //@return: file model.ExaFileUploadAndDownload, err error
  78. func (e *FileUploadAndDownloadService) UploadFile(header *multipart.FileHeader, noSave string) (file example.ExaFileUploadAndDownload, err error) {
  79. oss := upload.NewOss()
  80. filePath, key, uploadErr := oss.UploadFile(header)
  81. if uploadErr != nil {
  82. panic(err)
  83. }
  84. if noSave == "0" {
  85. s := strings.Split(header.Filename, ".")
  86. f := example.ExaFileUploadAndDownload{
  87. Url: filePath,
  88. Name: header.Filename,
  89. Tag: s[len(s)-1],
  90. Key: key,
  91. }
  92. return f, e.Upload(f)
  93. }
  94. return
  95. }