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.

91 lines
2.6 KiB

  1. package service
  2. import (
  3. "errors"
  4. "gin-vue-admin/global"
  5. "gin-vue-admin/model"
  6. "gin-vue-admin/model/request"
  7. "gin-vue-admin/utils/upload"
  8. "mime/multipart"
  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 Upload(file model.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 FindFile(id uint) (error, model.ExaFileUploadAndDownload) {
  25. var file model.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 DeleteFile(file model.ExaFileUploadAndDownload) (err error) {
  35. var fileFromDb model.ExaFileUploadAndDownload
  36. err, fileFromDb = 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 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 []model.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. //@author: [piexlmax](https://github.com/piexlmax)
  59. //@function: UploadFile
  60. //@description: 根据配置文件判断是文件上传到本地或者七牛云
  61. //@param: header *multipart.FileHeader, noSave string
  62. //@return: err error, file model.ExaFileUploadAndDownload
  63. func UploadFile(header *multipart.FileHeader, noSave string) (err error, file model.ExaFileUploadAndDownload) {
  64. oss := upload.NewOss()
  65. filePath, key, uploadErr := oss.UploadFile(header)
  66. if uploadErr != nil {
  67. panic(err)
  68. }
  69. if noSave == "0" {
  70. s := strings.Split(header.Filename, ".")
  71. f := model.ExaFileUploadAndDownload{
  72. Url: filePath,
  73. Name: header.Filename,
  74. Tag: s[len(s)-1],
  75. Key: key,
  76. }
  77. return Upload(f), f
  78. }
  79. return
  80. }