exa_file_upload_download.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. 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. }