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.

85 lines
3.1 KiB

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