exa_breakpoint_continue.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package dbModel
  2. import (
  3. "gin-vue-admin/init/qmsql"
  4. "github.com/jinzhu/gorm"
  5. )
  6. type ExaFile struct {
  7. gorm.Model
  8. FileName string
  9. FileMd5 string
  10. FilePath string
  11. ExaFileChunk []ExaFileChunk
  12. ChunkTotal int
  13. IsFinish bool
  14. }
  15. type ExaFileChunk struct {
  16. gorm.Model
  17. ExaFileId uint
  18. FileChunkNumber int
  19. FileChunkPath string
  20. }
  21. func (f *ExaFile) FileCreateComplete(FileMd5 string, FileName string, FilePath string) error {
  22. var file ExaFile
  23. upDateFile := make(map[string]interface{})
  24. upDateFile["FilePath"] = FilePath
  25. upDateFile["IsFinish"] = true
  26. err := qmsql.DEFAULTDB.Where("file_md5 = ? AND file_name = ?", FileMd5, FileName).First(&file).Updates(upDateFile).Error
  27. return err
  28. }
  29. func (f *ExaFile) FindOrCreateFile(FileMd5 string, FileName string, ChunkTotal int) (err error, file ExaFile) {
  30. var cfile ExaFile
  31. cfile.FileMd5 = FileMd5
  32. cfile.FileName = FileName
  33. cfile.ChunkTotal = ChunkTotal
  34. notHaveSameMd5Finish := qmsql.DEFAULTDB.Where("file_md5 = ? AND is_finish = ?", FileMd5, true).First(&file).RecordNotFound()
  35. if notHaveSameMd5Finish {
  36. err = qmsql.DEFAULTDB.Where("file_md5 = ? AND file_name = ?", FileMd5, FileName).Preload("ExaFileChunk").FirstOrCreate(&file, cfile).Error
  37. return err, file
  38. } else {
  39. cfile.IsFinish = true
  40. cfile.FilePath = file.FilePath
  41. err = qmsql.DEFAULTDB.Create(&cfile).Error
  42. return err, cfile
  43. }
  44. }
  45. // 创建文件切片记录
  46. func (f *ExaFile) CreateFileChunk(FileChunkPath string, FileChunkNumber int) error {
  47. var chunk ExaFileChunk
  48. chunk.FileChunkPath = FileChunkPath
  49. chunk.ExaFileId = f.ID
  50. chunk.FileChunkNumber = FileChunkNumber
  51. err := qmsql.DEFAULTDB.Create(&chunk).Error
  52. return err
  53. }
  54. // 删除文件切片记录
  55. func (f *ExaFile) DeleteFileChunk(fileMd5 string, fileName string, filePath string) error {
  56. var chunks []ExaFileChunk
  57. var file ExaFile
  58. err := qmsql.DEFAULTDB.Where("file_md5 = ? AND file_name = ?", fileMd5, fileName).First(&file).Update("IsFinish", true).Update("file_path", filePath).Error
  59. err = qmsql.DEFAULTDB.Where("exa_file_id = ?", file.ID).Delete(&chunks).Unscoped().Error
  60. return err
  61. }