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.

65 lines
2.3 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package example
  2. import (
  3. "errors"
  4. "github.com/flipped-aurora/gin-vue-admin/server/global"
  5. "github.com/flipped-aurora/gin-vue-admin/server/model/example"
  6. "gorm.io/gorm"
  7. )
  8. type FileUploadAndDownloadService struct{}
  9. //@author: [piexlmax](https://github.com/piexlmax)
  10. //@function: FindOrCreateFile
  11. //@description: 上传文件时检测当前文件属性,如果没有文件则创建,有则返回文件的当前切片
  12. //@param: fileMd5 string, fileName string, chunkTotal int
  13. //@return: err error, file model.ExaFile
  14. func (e *FileUploadAndDownloadService) FindOrCreateFile(fileMd5 string, fileName string, chunkTotal int) (err error, file example.ExaFile) {
  15. var cfile example.ExaFile
  16. cfile.FileMd5 = fileMd5
  17. cfile.FileName = fileName
  18. cfile.ChunkTotal = chunkTotal
  19. if errors.Is(global.GVA_DB.Where("file_md5 = ? AND is_finish = ?", fileMd5, true).First(&file).Error, gorm.ErrRecordNotFound) {
  20. err = global.GVA_DB.Where("file_md5 = ? AND file_name = ?", fileMd5, fileName).Preload("ExaFileChunk").FirstOrCreate(&file, cfile).Error
  21. return err, file
  22. }
  23. cfile.IsFinish = true
  24. cfile.FilePath = file.FilePath
  25. err = global.GVA_DB.Create(&cfile).Error
  26. return err, cfile
  27. }
  28. //@author: [piexlmax](https://github.com/piexlmax)
  29. //@function: CreateFileChunk
  30. //@description: 创建文件切片记录
  31. //@param: id uint, fileChunkPath string, fileChunkNumber int
  32. //@return: error
  33. func (e *FileUploadAndDownloadService) CreateFileChunk(id uint, fileChunkPath string, fileChunkNumber int) error {
  34. var chunk example.ExaFileChunk
  35. chunk.FileChunkPath = fileChunkPath
  36. chunk.ExaFileID = id
  37. chunk.FileChunkNumber = fileChunkNumber
  38. err := global.GVA_DB.Create(&chunk).Error
  39. return err
  40. }
  41. //@author: [piexlmax](https://github.com/piexlmax)
  42. //@function: DeleteFileChunk
  43. //@description: 删除文件切片记录
  44. //@param: fileMd5 string, fileName string, filePath string
  45. //@return: error
  46. func (e *FileUploadAndDownloadService) DeleteFileChunk(fileMd5 string, fileName string, filePath string) error {
  47. var chunks []example.ExaFileChunk
  48. var file example.ExaFile
  49. err := global.GVA_DB.Where("file_md5 = ? AND file_name = ?", fileMd5, fileName).First(&file).Update("IsFinish", true).Update("file_path", filePath).Error
  50. if err != nil {
  51. return err
  52. }
  53. err = global.GVA_DB.Where("exa_file_id = ?", file.ID).Delete(&chunks).Unscoped().Error
  54. return err
  55. }