exa_breakpoint_continue.go 2.4 KB

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