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.8 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package example
  2. import (
  3. "errors"
  4. "gin-vue-admin/global"
  5. "gin-vue-admin/model/common/request"
  6. "gin-vue-admin/model/example"
  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 (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. //@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 (e *FileUploadAndDownloadService) UploadFile(header *multipart.FileHeader, noSave string) (err error, file example.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 := example.ExaFileUploadAndDownload{
  72. Url: filePath,
  73. Name: header.Filename,
  74. Tag: s[len(s)-1],
  75. Key: key,
  76. }
  77. return e.Upload(f), f
  78. }
  79. return
  80. }