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

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