You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

uploadhandler.go 2.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package image
  2. import (
  3. "bufio"
  4. "fmt"
  5. "github.com/zeromicro/go-zero/core/logx"
  6. result2 "gitlink.org.cn/jcce-pcm/utils/result"
  7. "gitlink.org.cn/jcce-pcm/utils/tool"
  8. "io"
  9. "net/http"
  10. "os"
  11. "path/filepath"
  12. "strconv"
  13. "syscall"
  14. "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/svc"
  15. )
  16. type LoadBody struct {
  17. Stream string `json:"stream"`
  18. }
  19. var uploadTempPath = filepath.Join(uploadPath, "temp")
  20. func UploadHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
  21. return func(w http.ResponseWriter, r *http.Request) {
  22. file, fileHeader, err := r.FormFile("file")
  23. if err != nil {
  24. logx.Error(err)
  25. result2.HttpResult(r, w, nil, err)
  26. return
  27. }
  28. index := r.PostFormValue("index")
  29. hash := r.PostFormValue("hash")
  30. defer file.Close()
  31. // 合并路径
  32. chunksPath := filepath.Join(uploadTempPath, hash)
  33. // 文件路径
  34. filePath := filepath.Join(chunksPath, hash+"-"+index)
  35. // 检查临时文件夹是否存在 不存在则创建文件夹
  36. isPathExists, err := tool.PathExists(chunksPath)
  37. if !isPathExists {
  38. err = os.MkdirAll(chunksPath, os.ModePerm)
  39. if err != nil {
  40. logx.Error(err)
  41. result2.HttpResult(r, w, nil, err)
  42. return
  43. }
  44. }
  45. // 检查文件是否存在
  46. exists, err := tool.PathExists(filePath)
  47. if err != nil {
  48. logx.Error(err)
  49. result2.HttpResult(r, w, nil, err)
  50. return
  51. }
  52. // 文件存在 进行断点续传
  53. if exists {
  54. fileInfo, _ := os.Stat(filePath)
  55. if fileInfo.Size() == fileHeader.Size {
  56. result2.HttpResult(r, w, nil, err)
  57. return
  58. }
  59. start := strconv.Itoa(int(fileInfo.Size()))
  60. oldFile, _ := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY, os.ModePerm)
  61. defer oldFile.Close()
  62. count, _ := strconv.ParseInt(start, 10, 64)
  63. fmt.Println("已上传:", count)
  64. // 设置读,写的偏移量
  65. file.Seek(count, 0)
  66. oldFile.Seek(count, 0)
  67. data := make([]byte, 1024, 1024)
  68. for {
  69. total, err := file.Read(data)
  70. if err == io.EOF {
  71. fmt.Println("文件复制完毕")
  72. break
  73. }
  74. oldFile.Write(data[:total])
  75. }
  76. // 文件不存在 直接上传
  77. } else {
  78. destFile, _ := os.OpenFile(filepath.Join(chunksPath, hash+"-"+index), syscall.O_CREAT|syscall.O_WRONLY, 0777)
  79. reader := bufio.NewReader(file)
  80. writer := bufio.NewWriter(destFile)
  81. buf := make([]byte, 1024*1024) // 1M buf
  82. for {
  83. n, err := reader.Read(buf)
  84. if err == io.EOF {
  85. writer.Flush()
  86. break
  87. } else if err != nil {
  88. return
  89. } else {
  90. writer.Write(buf[:n])
  91. }
  92. }
  93. defer file.Close()
  94. defer destFile.Close()
  95. }
  96. result2.HttpResult(r, w, nil, err)
  97. }
  98. }

PCM is positioned as Software stack over Cloud, aiming to build the standards and ecology of heterogeneous cloud collaboration for JCC in a non intrusive and autonomous peer-to-peer manner.