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.

59 lines
2.0 KiB

  1. package service
  2. import (
  3. "errors"
  4. "gin-vue-admin/global"
  5. "gin-vue-admin/model"
  6. "gorm.io/gorm"
  7. )
  8. //@author: [piexlmax](https://github.com/piexlmax)
  9. //@function: FindOrCreateFile
  10. //@description: 上传文件时检测当前文件属性,如果没有文件则创建,有则返回文件的当前切片
  11. //@param: fileMd5 string, fileName string, chunkTotal int
  12. //@return: err error, file model.ExaFile
  13. func FindOrCreateFile(fileMd5 string, fileName string, chunkTotal int) (err error, file model.ExaFile) {
  14. var cfile model.ExaFile
  15. cfile.FileMd5 = fileMd5
  16. cfile.FileName = fileName
  17. cfile.ChunkTotal = chunkTotal
  18. if errors.Is(global.GVA_DB.Where("file_md5 = ? AND is_finish = ?", fileMd5, true).First(&file).Error, gorm.ErrRecordNotFound) {
  19. err = global.GVA_DB.Where("file_md5 = ? AND file_name = ?", fileMd5, fileName).Preload("ExaFileChunk").FirstOrCreate(&file, cfile).Error
  20. return err, file
  21. }
  22. cfile.IsFinish = true
  23. cfile.FilePath = file.FilePath
  24. err = global.GVA_DB.Create(&cfile).Error
  25. return err, cfile
  26. }
  27. //@author: [piexlmax](https://github.com/piexlmax)
  28. //@function: CreateFileChunk
  29. //@description: 创建文件切片记录
  30. //@param: id uint, fileChunkPath string, fileChunkNumber int
  31. //@return: error
  32. func CreateFileChunk(id uint, fileChunkPath string, fileChunkNumber int) error {
  33. var chunk model.ExaFileChunk
  34. chunk.FileChunkPath = fileChunkPath
  35. chunk.ExaFileID = id
  36. chunk.FileChunkNumber = fileChunkNumber
  37. err := global.GVA_DB.Create(&chunk).Error
  38. return err
  39. }
  40. //@author: [piexlmax](https://github.com/piexlmax)
  41. //@function: DeleteFileChunk
  42. //@description: 删除文件切片记录
  43. //@param: fileMd5 string, fileName string, filePath string
  44. //@return: error
  45. func DeleteFileChunk(fileMd5 string, fileName string, filePath string) error {
  46. var chunks []model.ExaFileChunk
  47. var file model.ExaFile
  48. err := global.GVA_DB.Where("file_md5 = ? AND file_name = ?", fileMd5, fileName).First(&file).Update("IsFinish", true).Update("file_path", filePath).Error
  49. err = global.GVA_DB.Where("exa_file_id = ?", file.ID).Delete(&chunks).Unscoped().Error
  50. return err
  51. }