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.

97 lines
2.7 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package example
  2. import (
  3. "errors"
  4. "fmt"
  5. "gin-vue-admin/global"
  6. "gin-vue-admin/model/example"
  7. "gorm.io/gorm"
  8. "io/ioutil"
  9. "os"
  10. "strconv"
  11. )
  12. type SimpleUploaderService struct {
  13. }
  14. //@author: [piexlmax](https://github.com/piexlmax)
  15. //@function: SaveChunk
  16. //@description: 保存文件切片路径
  17. //@param: uploader model.ExaSimpleUploader
  18. //@return: err error
  19. func (exa *SimpleUploaderService) SaveChunk(uploader example.ExaSimpleUploader) (err error) {
  20. return global.GVA_DB.Create(uploader).Error
  21. }
  22. //@author: [piexlmax](https://github.com/piexlmax)
  23. //@function: CheckFileMd5
  24. //@description: 检查文件是否已经上传过
  25. //@param: md5 string
  26. //@return: err error, uploads []model.ExaSimpleUploader, isDone bool
  27. func (exa *SimpleUploaderService) CheckFileMd5(md5 string) (err error, uploads []example.ExaSimpleUploader, isDone bool) {
  28. err = global.GVA_DB.Find(&uploads, "identifier = ? AND is_done = ?", md5, false).Error
  29. isDone = errors.Is(global.GVA_DB.First(&example.ExaSimpleUploader{}, "identifier = ? AND is_done = ?", md5, true).Error, gorm.ErrRecordNotFound)
  30. return err, uploads, !isDone
  31. }
  32. //@author: [piexlmax](https://github.com/piexlmax)
  33. //@function: MergeFileMd5
  34. //@description: 合并文件
  35. //@param: md5 string, fileName string
  36. //@return: err error
  37. func (exa *SimpleUploaderService) MergeFileMd5(md5 string, fileName string) (err error) {
  38. finishDir := "./finish/"
  39. dir := "./chunk/" + md5
  40. // 如果文件上传成功 不做后续操作 通知成功即可
  41. if !errors.Is(global.GVA_DB.First(&example.ExaSimpleUploader{}, "identifier = ? AND is_done = ?", md5, true).Error, gorm.ErrRecordNotFound) {
  42. return nil
  43. }
  44. // 打开切片文件夹
  45. rd, err := ioutil.ReadDir(dir)
  46. _ = os.MkdirAll(finishDir, os.ModePerm)
  47. // 创建目标文件
  48. fd, err := os.OpenFile(finishDir+fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
  49. if err != nil {
  50. return
  51. }
  52. // 关闭文件
  53. defer fd.Close()
  54. // 将切片文件按照顺序写入
  55. for k := range rd {
  56. content, _ := ioutil.ReadFile(dir + "/" + fileName + strconv.Itoa(k+1))
  57. _, err = fd.Write(content)
  58. if err != nil {
  59. _ = os.Remove(finishDir + fileName)
  60. }
  61. }
  62. if err != nil {
  63. return err
  64. }
  65. err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
  66. // 删除切片信息
  67. if err = tx.Delete(&example.ExaSimpleUploader{}, "identifier = ? AND is_done = ?", md5, false).Error; err != nil {
  68. fmt.Println(err)
  69. return err
  70. }
  71. data := example.ExaSimpleUploader{
  72. Identifier: md5,
  73. IsDone: true,
  74. FilePath: finishDir + fileName,
  75. Filename: fileName,
  76. }
  77. // 添加文件信息
  78. if err = tx.Create(&data).Error; err != nil {
  79. fmt.Println(err)
  80. return err
  81. }
  82. return nil
  83. })
  84. err = os.RemoveAll(dir) // 清除切片
  85. return err
  86. }