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.

multipart_upload.go 3.8 kB

10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package local
  2. import (
  3. "context"
  4. "crypto/sha256"
  5. "fmt"
  6. "hash"
  7. "io"
  8. "os"
  9. "path/filepath"
  10. "gitlink.org.cn/cloudream/common/utils/io2"
  11. "gitlink.org.cn/cloudream/common/utils/os2"
  12. "gitlink.org.cn/cloudream/common/utils/sort2"
  13. stgtypes "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/storage/types"
  14. jcstypes "gitlink.org.cn/cloudream/jcs-pub/common/types"
  15. )
  16. type Multiparter struct {
  17. detail *jcstypes.UserSpaceDetail
  18. localStg *jcstypes.LocalCred
  19. feat *jcstypes.MultipartUploadFeature
  20. }
  21. func (*Multiparter) MinPartSize() int64 {
  22. return 1 * 1024 * 1024 // 1MB
  23. }
  24. func (*Multiparter) MaxPartSize() int64 {
  25. return 5 * 1024 * 1024 * 1024 // 5GB
  26. }
  27. func (m *Multiparter) Initiate(ctx context.Context) (stgtypes.MultipartTask, error) {
  28. tempDir := filepath.Join(m.localStg.RootDir, m.detail.UserSpace.WorkingDir.JoinOSPath(), stgtypes.TempWorkingDir)
  29. absTempDir, err := filepath.Abs(tempDir)
  30. if err != nil {
  31. return nil, fmt.Errorf("get abs temp dir %v: %v", tempDir, err)
  32. }
  33. tempFileName := os2.GenerateRandomFileName(10)
  34. tempPartsDir := filepath.Join(absTempDir, tempFileName)
  35. absJoinedFilePath := filepath.Join(absTempDir, tempFileName+".joined")
  36. err = os.MkdirAll(tempPartsDir, 0777)
  37. if err != nil {
  38. return nil, err
  39. }
  40. return &MultipartTask{
  41. absTempDir: absTempDir,
  42. tempFileName: tempFileName,
  43. tempPartsDir: tempPartsDir,
  44. joinedFileJPath: m.detail.UserSpace.WorkingDir.ConcatCompsNew(stgtypes.TempWorkingDir, tempFileName+".joined"),
  45. absJoinedFilePath: absJoinedFilePath,
  46. uploadID: tempPartsDir,
  47. }, nil
  48. }
  49. func (m *Multiparter) UploadPart(ctx context.Context, init stgtypes.MultipartInitState, partSize int64, partNumber int, stream io.Reader) (stgtypes.UploadedPartInfo, error) {
  50. partFilePath := filepath.Join(init.UploadID, fmt.Sprintf("%v", partNumber))
  51. partFile, err := os.Create(partFilePath)
  52. if err != nil {
  53. return stgtypes.UploadedPartInfo{}, err
  54. }
  55. defer partFile.Close()
  56. _, err = io.Copy(partFile, stream)
  57. if err != nil {
  58. return stgtypes.UploadedPartInfo{}, err
  59. }
  60. return stgtypes.UploadedPartInfo{
  61. ETag: partFilePath,
  62. PartNumber: partNumber,
  63. }, nil
  64. }
  65. type MultipartTask struct {
  66. absTempDir string // 应该要是绝对路径
  67. tempFileName string
  68. tempPartsDir string
  69. joinedFileJPath jcstypes.JPath
  70. absJoinedFilePath string
  71. uploadID string
  72. }
  73. func (i *MultipartTask) InitState() stgtypes.MultipartInitState {
  74. return stgtypes.MultipartInitState{
  75. UploadID: i.uploadID,
  76. }
  77. }
  78. func (i *MultipartTask) JoinParts(ctx context.Context, parts []stgtypes.UploadedPartInfo) (stgtypes.FileInfo, error) {
  79. parts = sort2.Sort(parts, func(l, r stgtypes.UploadedPartInfo) int {
  80. return l.PartNumber - r.PartNumber
  81. })
  82. joined, err := os.Create(i.absJoinedFilePath)
  83. if err != nil {
  84. return stgtypes.FileInfo{}, err
  85. }
  86. defer joined.Close()
  87. size := int64(0)
  88. hasher := sha256.New()
  89. for _, part := range parts {
  90. partSize, err := i.writePart(part, joined, hasher)
  91. if err != nil {
  92. return stgtypes.FileInfo{}, err
  93. }
  94. size += partSize
  95. }
  96. h := hasher.Sum(nil)
  97. return stgtypes.FileInfo{
  98. Path: i.joinedFileJPath,
  99. Size: size,
  100. Hash: jcstypes.NewFullHash(h),
  101. }, nil
  102. }
  103. func (i *MultipartTask) writePart(partInfo stgtypes.UploadedPartInfo, joined *os.File, hasher hash.Hash) (int64, error) {
  104. part, err := os.Open(partInfo.ETag)
  105. if err != nil {
  106. return 0, err
  107. }
  108. defer part.Close()
  109. buf := make([]byte, 32*1024)
  110. size := int64(0)
  111. for {
  112. n, err := part.Read(buf)
  113. if n > 0 {
  114. size += int64(n)
  115. io2.WriteAll(hasher, buf[:n])
  116. err := io2.WriteAll(joined, buf[:n])
  117. if err != nil {
  118. return 0, err
  119. }
  120. }
  121. if err == io.EOF {
  122. break
  123. }
  124. if err != nil {
  125. return 0, err
  126. }
  127. }
  128. return size, nil
  129. }
  130. func (i *MultipartTask) Close() {
  131. os.RemoveAll(i.tempPartsDir)
  132. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。