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.

62 lines
2.2 KiB

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