You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.0 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  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. 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. //@author: [piexlmax](https://github.com/piexlmax)
  48. //@function: GetFileRecordInfoList
  49. //@description: 分页获取数据
  50. //@param: info request.PageInfo
  51. //@return: err error, list interface{}, total int64
  52. func (e *FileUploadAndDownloadService) GetFileRecordInfoList(info request.PageInfo) (err error, list interface{}, total int64) {
  53. limit := info.PageSize
  54. offset := info.PageSize * (info.Page - 1)
  55. db := global.GVA_DB.Model(&example.ExaFileUploadAndDownload{})
  56. var fileLists []example.ExaFileUploadAndDownload
  57. err = db.Count(&total).Error
  58. if err != nil {
  59. return
  60. }
  61. err = db.Limit(limit).Offset(offset).Order("updated_at desc").Find(&fileLists).Error
  62. return err, fileLists, total
  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) (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. f := example.ExaFileUploadAndDownload{
  78. Url: filePath,
  79. Name: header.Filename,
  80. Tag: s[len(s)-1],
  81. Key: key,
  82. }
  83. return e.Upload(f), f
  84. }
  85. return
  86. }