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.

67 lines
2.4 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. if errors.Is(global.GVA_DB.Where("file_md5 = ? AND is_finish = ?", fileMd5, true).First(&file).Error, gorm.ErrRecordNotFound) {
  23. err = global.GVA_DB.Where("file_md5 = ? AND file_name = ?", fileMd5, fileName).Preload("ExaFileChunk").FirstOrCreate(&file, cfile).Error
  24. return err, file
  25. }
  26. cfile.IsFinish = true
  27. cfile.FilePath = file.FilePath
  28. err = global.GVA_DB.Create(&cfile).Error
  29. return err, cfile
  30. }
  31. // @title CreateFileChunk
  32. // @description create a chunk of the file, 创建文件切片记录
  33. // @auth (2020/04/05 20:22)
  34. // @param id unit
  35. // @param fileChunkPath string
  36. // @param fileChunkNumber int
  37. // @return error
  38. func CreateFileChunk(id uint, fileChunkPath string, fileChunkNumber int) error {
  39. var chunk model.ExaFileChunk
  40. chunk.FileChunkPath = fileChunkPath
  41. chunk.ExaFileID = id
  42. chunk.FileChunkNumber = fileChunkNumber
  43. err := global.GVA_DB.Create(&chunk).Error
  44. return err
  45. }
  46. // @title DeleteFileChunk
  47. // @description delete a chuck of the file, 删除文件切片记录
  48. // @auth (2020/04/05 20:22)
  49. // @param FileMd5 string
  50. // @param FileName string
  51. // @param FilePath string
  52. // @return error
  53. func DeleteFileChunk(fileMd5 string, fileName string, filePath string) error {
  54. var chunks []model.ExaFileChunk
  55. var file model.ExaFile
  56. err := global.GVA_DB.Where("file_md5 = ? AND file_name = ?", fileMd5, fileName).First(&file).Update("IsFinish", true).Update("file_path", filePath).Error
  57. err = global.GVA_DB.Where("exa_file_id = ?", file.ID).Delete(&chunks).Unscoped().Error
  58. return err
  59. }