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.

79 lines
3.0 KiB

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