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.

108 lines
2.9 KiB

3 years ago
3 years ago
  1. package utils
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "strconv"
  6. )
  7. // 前端传来文件片与当前片为什么文件的第几片
  8. // 后端拿到以后比较次分片是否上传 或者是否为不完全片
  9. // 前端发送每片多大
  10. // 前端告知是否为最后一片且是否完成
  11. const (
  12. breakpointDir = "./breakpointDir/"
  13. finishDir = "./fileDir/"
  14. )
  15. //@author: [piexlmax](https://github.com/piexlmax)
  16. //@function: BreakPointContinue
  17. //@description: 断点续传
  18. //@param: content []byte, fileName string, contentNumber int, contentTotal int, fileMd5 string
  19. //@return: error, string
  20. func BreakPointContinue(content []byte, fileName string, contentNumber int, contentTotal int, fileMd5 string) (error, string) {
  21. path := breakpointDir + fileMd5 + "/"
  22. err := os.MkdirAll(path, os.ModePerm)
  23. if err != nil {
  24. return err, path
  25. }
  26. err, pathc := makeFileContent(content, fileName, path, contentNumber)
  27. return err, pathc
  28. }
  29. //@author: [piexlmax](https://github.com/piexlmax)
  30. //@function: CheckMd5
  31. //@description: 检查Md5
  32. //@param: content []byte, chunkMd5 string
  33. //@return: CanUpload bool
  34. func CheckMd5(content []byte, chunkMd5 string) (CanUpload bool) {
  35. fileMd5 := MD5V(content)
  36. if fileMd5 == chunkMd5 {
  37. return true // 可以继续上传
  38. } else {
  39. return false // 切片不完整,废弃
  40. }
  41. }
  42. //@author: [piexlmax](https://github.com/piexlmax)
  43. //@function: makeFileContent
  44. //@description: 创建切片内容
  45. //@param: content []byte, fileName string, FileDir string, contentNumber int
  46. //@return: error, string
  47. func makeFileContent(content []byte, fileName string, FileDir string, contentNumber int) (error, string) {
  48. path := FileDir + fileName + "_" + strconv.Itoa(contentNumber)
  49. f, err := os.Create(path)
  50. if err != nil {
  51. return err, path
  52. } else {
  53. _, err = f.Write(content)
  54. if err != nil {
  55. return err, path
  56. }
  57. }
  58. defer f.Close()
  59. return nil, path
  60. }
  61. //@author: [piexlmax](https://github.com/piexlmax)
  62. //@function: makeFileContent
  63. //@description: 创建切片文件
  64. //@param: fileName string, FileMd5 string
  65. //@return: error, string
  66. func MakeFile(fileName string, FileMd5 string) (error, string) {
  67. rd, err := ioutil.ReadDir(breakpointDir + FileMd5)
  68. if err != nil {
  69. return err, finishDir + fileName
  70. }
  71. _ = os.MkdirAll(finishDir, os.ModePerm)
  72. fd, err := os.OpenFile(finishDir+fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0o644)
  73. if err != nil {
  74. return err, finishDir + fileName
  75. }
  76. defer fd.Close()
  77. for k := range rd {
  78. content, _ := ioutil.ReadFile(breakpointDir + FileMd5 + "/" + fileName + "_" + strconv.Itoa(k))
  79. _, err = fd.Write(content)
  80. if err != nil {
  81. _ = os.Remove(finishDir + fileName)
  82. return err, finishDir + fileName
  83. }
  84. }
  85. return nil, finishDir + fileName
  86. }
  87. //@author: [piexlmax](https://github.com/piexlmax)
  88. //@function: RemoveChunk
  89. //@description: 移除切片
  90. //@param: FileMd5 string
  91. //@return: error
  92. func RemoveChunk(FileMd5 string) error {
  93. err := os.RemoveAll(breakpointDir + FileMd5)
  94. return err
  95. }