Skip to content

Commit 3daa41f

Browse files
authored
fix(189pc): address review comments on torrent sidecar handling
- Only append SHA-1 piece hash when n > 0, preventing spurious piece for empty files (BitTorrent v1 requires 0 pieces for 0-byte files) - Explicitly guard torrent generation with size > 0 - Log findFileByName errors in torrentFollowCopy/Move/Rename instead of silently discarding them - Log WaitBatchTask failure for follow-move operation - Rename Cloud189File.ParentId to ParentID to match Go initialism conventions (consistent with Cloud189Folder.ParentID, Cloud189File.ID)
1 parent 1a13cca commit 3daa41f

5 files changed

Lines changed: 14 additions & 14 deletions

File tree

drivers/189pc/driver.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ func (y *Cloud189PC) Move(ctx context.Context, srcObj, dstDir model.Obj) (model.
267267
if !srcObj.IsDir() {
268268
var srcFolderId string
269269
if f, ok := srcObj.(*Cloud189File); ok {
270-
srcFolderId = f.ParentId
270+
srcFolderId = f.ParentID
271271
}
272272
y.torrentFollowMove(srcFolderId, srcObj.GetName(), dstDir)
273273
}
@@ -311,7 +311,7 @@ func (y *Cloud189PC) Rename(ctx context.Context, srcObj model.Obj, newName strin
311311

312312
// 跟随重命名 torrent 文件
313313
if f, ok := srcObj.(*Cloud189File); ok {
314-
y.torrentFollowRename(f.ParentId, srcObj.GetName(), newName)
314+
y.torrentFollowRename(f.ParentID, srcObj.GetName(), newName)
315315
}
316316

317317
switch f := srcObj.(type) {
@@ -343,7 +343,7 @@ func (y *Cloud189PC) Copy(ctx context.Context, srcObj, dstDir model.Obj) error {
343343
if !srcObj.IsDir() {
344344
var srcFolderId string
345345
if f, ok := srcObj.(*Cloud189File); ok {
346-
srcFolderId = f.ParentId
346+
srcFolderId = f.ParentID
347347
}
348348
y.torrentFollowCopy(srcFolderId, srcObj.GetName(), dstDir)
349349
}

drivers/189pc/torrent.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ func (y *Cloud189PC) torrentFollowCopy(srcFolderId string, srcFileName string, d
345345
go func() {
346346
torrentFile, err := y.findFileByName(context.Background(), torrentName, srcFolderId, isFamily)
347347
if err != nil {
348-
// torrent 文件不存在,忽略
348+
utils.Log.Debugf("查找 torrent 文件失败(跟随复制): %v", err)
349349
return
350350
}
351351
// 复制 torrent 文件到目标目录
@@ -383,7 +383,7 @@ func (y *Cloud189PC) torrentFollowMove(srcFolderId string, srcFileName string, d
383383
go func() {
384384
torrentFile, err := y.findFileByName(context.Background(), torrentName, srcFolderId, isFamily)
385385
if err != nil {
386-
// torrent 文件不存在,忽略
386+
utils.Log.Debugf("查找 torrent 文件失败(跟随移动): %v", err)
387387
return
388388
}
389389
// 移动 torrent 文件到目标目录
@@ -398,7 +398,9 @@ func (y *Cloud189PC) torrentFollowMove(srcFolderId string, srcFileName string, d
398398
utils.Log.Warnf("跟随移动 torrent 文件失败: %v", moveErr)
399399
return
400400
}
401-
_ = y.WaitBatchTask("MOVE", resp.TaskID, time.Millisecond*400)
401+
if err = y.WaitBatchTask("MOVE", resp.TaskID, time.Millisecond*400); err != nil {
402+
utils.Log.Warnf("等待跟随移动 torrent 文件失败: %v", err)
403+
}
402404
}()
403405
}
404406

@@ -420,7 +422,7 @@ func (y *Cloud189PC) torrentFollowRename(folderId string, oldFileName string, ne
420422
go func() {
421423
torrentFile, err := y.findFileByName(context.Background(), oldTorrentName, folderId, isFamily)
422424
if err != nil {
423-
// torrent 文件不存在,忽略
425+
utils.Log.Debugf("查找 torrent 文件失败(跟随重命名): %v", err)
424426
return
425427
}
426428

drivers/189pc/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ type Cloud189File struct {
170170
Name string `json:"name"`
171171
Size int64 `json:"size"`
172172
Md5 string `json:"md5"`
173-
ParentId string `json:"-"` // 由 getFiles 设置,不从 JSON 解析
173+
ParentID string `json:"-"` // 由 getFiles 设置,不从 JSON 解析
174174

175175
LastOpTime Time `json:"lastOpTime"`
176176
CreateDate Time `json:"createDate"`

drivers/189pc/utils.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func (y *Cloud189PC) getFiles(ctx context.Context, fileId string, isFamily bool)
210210
res = append(res, &resp.FileListAO.FolderList[i])
211211
}
212212
for i := 0; i < len(resp.FileListAO.FileList); i++ {
213-
resp.FileListAO.FileList[i].ParentId = fileId
213+
resp.FileListAO.FileList[i].ParentID = fileId
214214
res = append(res, &resp.FileListAO.FileList[i])
215215
}
216216
}
@@ -986,8 +986,8 @@ func (y *Cloud189PC) fastUpload(ctx context.Context, dstDir model.Obj, file mode
986986
partInfos = append(partInfos, fmt.Sprint(i, "-", base64.StdEncoding.EncodeToString(md5Byte)))
987987
sliceMd5.Reset()
988988

989-
// 收集 SHA-1 piece hash
990-
if generateTorrent && sha1Writer != nil {
989+
// 收集 SHA-1 piece hash(仅在本次分片实际写入了数据时追加)
990+
if generateTorrent && n > 0 {
991991
pieceSHA1Hashes = append(pieceSHA1Hashes, sha1Writer.Sum(nil)...)
992992
}
993993
}
@@ -1107,7 +1107,7 @@ func (y *Cloud189PC) fastUpload(ctx context.Context, dstDir model.Obj, file mode
11071107
}
11081108

11091109
// 生成 torrent 文件(异步,不影响上传结果)
1110-
if generateTorrent && len(pieceSHA1Hashes) > 0 {
1110+
if generateTorrent && size > 0 && len(pieceSHA1Hashes) > 0 {
11111111
capturedDstDir := dstDir
11121112
capturedIsFamily := isFamily
11131113
capturedFileName := file.GetName()

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ github.com/Masterminds/semver/v3 v3.2.0 h1:3MEsd0SM6jqZojhjLWWeBY+Kcjy9i6MQAeY7Y
2727
github.com/Masterminds/semver/v3 v3.2.0/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
2828
github.com/Max-Sum/base32768 v0.0.0-20230304063302-18e6ce5945fd h1:nzE1YQBdx1bq9IlZinHa+HVffy+NmVRoKr+wHN8fpLE=
2929
github.com/Max-Sum/base32768 v0.0.0-20230304063302-18e6ce5945fd/go.mod h1:C8yoIfvESpM3GD07OCHU7fqI7lhwyZ2Td1rbNbTAhnc=
30-
github.com/OpenListTeam/115-sdk-go v0.2.3 h1:nDNz0GxgliW+nT2Ds486k/rp/GgJj7Ngznc98ZBUwZo=
31-
github.com/OpenListTeam/115-sdk-go v0.2.3/go.mod h1:cfvitk2lwe6036iNi2h+iNxwxWDifKZsSvNtrur5BqU=
3230
github.com/OpenListTeam/115-sdk-go v0.2.4 h1:dVoEz3Pm996n/ZCuRckQvz36w938uNHjPrS7E/SVpBM=
3331
github.com/OpenListTeam/115-sdk-go v0.2.4/go.mod h1:cfvitk2lwe6036iNi2h+iNxwxWDifKZsSvNtrur5BqU=
3432
github.com/OpenListTeam/go-cache v0.1.0 h1:eV2+FCP+rt+E4OCJqLUW7wGccWZNJMV0NNkh+uChbAI=

0 commit comments

Comments
 (0)