exa_breakpoint_continue.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package example
  2. import (
  3. "errors"
  4. "gorm.io/gorm"
  5. "server/global"
  6. "server/model/example"
  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: file model.ExaFile, err error
  14. func (e *FileUploadAndDownloadService) FindOrCreateFile(fileMd5 string, fileName string, chunkTotal int) (file example.ExaFile, err error) {
  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 file, err
  22. }
  23. cfile.IsFinish = true
  24. cfile.FilePath = file.FilePath
  25. err = global.GVA_DB.Create(&cfile).Error
  26. return cfile, err
  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, filePath string) error {
  47. var chunks []example.ExaFileChunk
  48. var file example.ExaFile
  49. err := global.GVA_DB.Where("file_md5 = ? ", fileMd5).First(&file).
  50. Updates(map[string]interface{}{
  51. "IsFinish": true,
  52. "file_path": filePath,
  53. }).Error
  54. if err != nil {
  55. return err
  56. }
  57. err = global.GVA_DB.Where("exa_file_id = ?", file.ID).Delete(&chunks).Unscoped().Error
  58. return err
  59. }