exa_file_upload_download.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package example
  2. import (
  3. "errors"
  4. "mime/multipart"
  5. "server/dao/example"
  6. "strings"
  7. "server/model/common/request"
  8. "server/utils/upload"
  9. )
  10. //@author: [piexlmax](https://github.com/piexlmax)
  11. //@function: Upload
  12. //@description: 创建文件上传记录
  13. //@param: file model.ExaFileUploadAndDownload
  14. //@return: error
  15. func (e *FileUploadAndDownloadService) Upload(file example.ExaFileUploadAndDownload) error {
  16. return file.CreateFile()
  17. }
  18. //@author: [piexlmax](https://github.com/piexlmax)
  19. //@function: FindFile
  20. //@description: 查询文件记录
  21. //@param: id uint
  22. //@return: model.ExaFileUploadAndDownload, error
  23. func (e *FileUploadAndDownloadService) FindFile(id uint) (example.ExaFileUploadAndDownload, error) {
  24. return example.QueryFileById(id)
  25. }
  26. //@author: [piexlmax](https://github.com/piexlmax)
  27. //@function: DeleteFile
  28. //@description: 删除文件记录
  29. //@param: file model.ExaFileUploadAndDownload
  30. //@return: err error
  31. func (e *FileUploadAndDownloadService) DeleteFile(file example.ExaFileUploadAndDownload) (err error) {
  32. var fileFromDb example.ExaFileUploadAndDownload
  33. fileFromDb, err = e.FindFile(file.ID)
  34. if err != nil {
  35. return
  36. }
  37. oss := upload.NewOss()
  38. if err = oss.DeleteFile(fileFromDb.Key); err != nil {
  39. return errors.New("文件删除失败")
  40. }
  41. err = file.DeleteFile()
  42. return err
  43. }
  44. // EditFileName 编辑文件名或者备注
  45. func (e *FileUploadAndDownloadService) EditFileName(file example.ExaFileUploadAndDownload) (err error) {
  46. _, err = file.EditFileName()
  47. return
  48. }
  49. //@author: [piexlmax](https://github.com/piexlmax)
  50. //@function: GetFileRecordInfoList
  51. //@description: 分页获取数据
  52. //@param: info request.PageInfo
  53. //@return: list interface{}, total int64, err error
  54. func (e *FileUploadAndDownloadService) GetFileRecordInfoList(info request.PageInfo) (list interface{}, total int64, err error) {
  55. limit := info.PageSize
  56. offset := info.PageSize * (info.Page - 1)
  57. keyword := info.Keyword
  58. return example.GetFileRecordInfoList(limit, offset, keyword)
  59. }
  60. //@author: [piexlmax](https://github.com/piexlmax)
  61. //@function: UploadFile
  62. //@description: 根据配置文件判断是文件上传到本地或者七牛云
  63. //@param: header *multipart.FileHeader, noSave string
  64. //@return: file model.ExaFileUploadAndDownload, err error
  65. func (e *FileUploadAndDownloadService) UploadFile(header *multipart.FileHeader, noSave string) (file example.ExaFileUploadAndDownload, err error) {
  66. oss := upload.NewOss()
  67. filePath, key, uploadErr := oss.UploadFile(header)
  68. if uploadErr != nil {
  69. panic(err)
  70. }
  71. if noSave == "0" {
  72. s := strings.Split(header.Filename, ".")
  73. f := example.ExaFileUploadAndDownload{
  74. Url: filePath,
  75. Name: header.Filename,
  76. Tag: s[len(s)-1],
  77. Key: key,
  78. }
  79. return f, e.Upload(f)
  80. }
  81. return
  82. }