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.

97 lines
3.4 KiB

  1. package model
  2. import (
  3. "gin-vue-admin/global"
  4. "github.com/jinzhu/gorm"
  5. )
  6. // file struct, 文件结构体
  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. // file chunk struct, 切片结构体
  17. type ExaFileChunk struct {
  18. gorm.Model
  19. ExaFileId uint
  20. FileChunkNumber int
  21. FileChunkPath string
  22. }
  23. // @title FileCreateComplete
  24. // @description file creation, 文件合成完成
  25. // @auth (2020/04/05 20:22 )
  26. // @param FileMd5 string
  27. // @param FileName string
  28. // @param FilePath string
  29. // @return error
  30. func (f *ExaFile) FileCreateComplete(FileMd5 string, FileName string, FilePath string) error {
  31. var file ExaFile
  32. upDateFile := make(map[string]interface{})
  33. upDateFile["FilePath"] = FilePath
  34. upDateFile["IsFinish"] = true
  35. err := global.GVA_DB.Where("file_md5 = ? AND file_name = ?", FileMd5, FileName).First(&file).Updates(upDateFile).Error
  36. return err
  37. }
  38. // @title FindOrCreateFile
  39. // @description Check your file if it does not exist, or return current slice of the file
  40. // 上传文件时检测当前文件属性,如果没有文件则创建,有则返回文件的当前切片
  41. // @auth (2020/04/05 20:22 )
  42. // @param FileMd5 string
  43. // @param FileName string
  44. // @param ChunkTotal int
  45. // @return err error
  46. // @return file ExaFile
  47. func (f *ExaFile) FindOrCreateFile(FileMd5 string, FileName string, ChunkTotal int) (err error, file ExaFile) {
  48. var cfile ExaFile
  49. cfile.FileMd5 = FileMd5
  50. cfile.FileName = FileName
  51. cfile.ChunkTotal = ChunkTotal
  52. notHaveSameMd5Finish := global.GVA_DB.Where("file_md5 = ? AND is_finish = ?", FileMd5, true).First(&file).RecordNotFound()
  53. if notHaveSameMd5Finish {
  54. err = global.GVA_DB.Where("file_md5 = ? AND file_name = ?", FileMd5, FileName).Preload("ExaFileChunk").FirstOrCreate(&file, cfile).Error
  55. return err, file
  56. } else {
  57. cfile.IsFinish = true
  58. cfile.FilePath = file.FilePath
  59. err = global.GVA_DB.Create(&cfile).Error
  60. return err, cfile
  61. }
  62. }
  63. // @title CreateFileChunk
  64. // @description create a chunk of the file, 创建文件切片记录
  65. // @auth (2020/04/05 20:22 )
  66. // @param FileChunkPath string
  67. // @param FileChunkNumber int
  68. // @return error
  69. func (f *ExaFile) CreateFileChunk(FileChunkPath string, FileChunkNumber int) error {
  70. var chunk ExaFileChunk
  71. chunk.FileChunkPath = FileChunkPath
  72. chunk.ExaFileId = f.ID
  73. chunk.FileChunkNumber = FileChunkNumber
  74. err := global.GVA_DB.Create(&chunk).Error
  75. return err
  76. }
  77. // @title DeleteFileChunk
  78. // @description delete a chuck of the file, 删除文件切片记录
  79. // @auth (2020/04/05 20:22 )
  80. // @param FileMd5 string
  81. // @param FileName string
  82. // @param FilePath string
  83. // @return error
  84. func (f *ExaFile) DeleteFileChunk(fileMd5 string, fileName string, filePath string) error {
  85. var chunks []ExaFileChunk
  86. var file ExaFile
  87. err := global.GVA_DB.Where("file_md5 = ? AND file_name = ?", fileMd5, fileName).First(&file).Update("IsFinish", true).Update("file_path", filePath).Error
  88. err = global.GVA_DB.Where("exa_file_id = ?", file.ID).Delete(&chunks).Unscoped().Error
  89. return err
  90. }