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.

72 lines
2.2 KiB

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