Skip to content

Commit f350ccd

Browse files
authored
fix(189pc): sliceSize must not be equal to fileSize (#1169)
* fix(189pc): sliceSize not equal to fileSize Signed-off-by: MadDogOwner <[email protected]> * Update comment for sliceSize parameter Signed-off-by: MadDogOwner <[email protected]> --------- Signed-off-by: MadDogOwner <[email protected]>
1 parent 4f2de93 commit f350ccd

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

drivers/189pc/utils.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -472,14 +472,16 @@ func (y *Cloud189PC) refreshSession() (err error) {
472472
// 普通上传
473473
// 无法上传大小为0的文件
474474
func (y *Cloud189PC) StreamUpload(ctx context.Context, dstDir model.Obj, file model.FileStreamer, up driver.UpdateProgress, isFamily bool, overwrite bool) (model.Obj, error) {
475-
size := file.GetSize()
476-
sliceSize := min(size, partSize(size))
475+
// 文件大小
476+
fileSize := file.GetSize()
477+
// 分片大小,不得为文件大小
478+
sliceSize := partSize(fileSize)
477479

478480
params := Params{
479481
"parentFolderId": dstDir.GetID(),
480482
"fileName": url.QueryEscape(file.GetName()),
481-
"fileSize": fmt.Sprint(file.GetSize()),
482-
"sliceSize": fmt.Sprint(sliceSize),
483+
"fileSize": fmt.Sprint(fileSize),
484+
"sliceSize": fmt.Sprint(sliceSize), // 必须为特定分片大小
483485
"lazyCheck": "1",
484486
}
485487

@@ -512,10 +514,10 @@ func (y *Cloud189PC) StreamUpload(ctx context.Context, dstDir model.Obj, file mo
512514
retry.DelayType(retry.BackOffDelay))
513515

514516
count := 1
515-
if size > sliceSize {
516-
count = int((size + sliceSize - 1) / sliceSize)
517+
if fileSize > sliceSize {
518+
count = int((fileSize + sliceSize - 1) / sliceSize)
517519
}
518-
lastPartSize := size % sliceSize
520+
lastPartSize := fileSize % sliceSize
519521
if lastPartSize == 0 {
520522
lastPartSize = sliceSize
521523
}
@@ -535,9 +537,9 @@ func (y *Cloud189PC) StreamUpload(ctx context.Context, dstDir model.Obj, file mo
535537
break
536538
}
537539
offset := int64((i)-1) * sliceSize
538-
size := sliceSize
540+
partSize := sliceSize
539541
if i == count {
540-
size = lastPartSize
542+
partSize = lastPartSize
541543
}
542544
partInfo := ""
543545
var reader *stream.SectionReader
@@ -546,14 +548,14 @@ func (y *Cloud189PC) StreamUpload(ctx context.Context, dstDir model.Obj, file mo
546548
Before: func(ctx context.Context) error {
547549
if reader == nil {
548550
var err error
549-
reader, err = ss.GetSectionReader(offset, size)
551+
reader, err = ss.GetSectionReader(offset, partSize)
550552
if err != nil {
551553
return err
552554
}
553555
silceMd5.Reset()
554556
w, err := utils.CopyWithBuffer(writers, reader)
555-
if w != size {
556-
return fmt.Errorf("failed to read all data: (expect =%d, actual =%d) %w", size, w, err)
557+
if w != partSize {
558+
return fmt.Errorf("failed to read all data: (expect =%d, actual =%d) %w", partSize, w, err)
557559
}
558560
// 计算块md5并进行hex和base64编码
559561
md5Bytes := silceMd5.Sum(nil)
@@ -595,7 +597,7 @@ func (y *Cloud189PC) StreamUpload(ctx context.Context, dstDir model.Obj, file mo
595597
fileMd5Hex = strings.ToUpper(hex.EncodeToString(fileMd5.Sum(nil)))
596598
}
597599
sliceMd5Hex := fileMd5Hex
598-
if file.GetSize() > sliceSize {
600+
if fileSize > sliceSize {
599601
sliceMd5Hex = strings.ToUpper(utils.GetMD5EncodeStr(strings.Join(silceMd5Hexs, "\n")))
600602
}
601603

0 commit comments

Comments
 (0)