Skip to content

Commit b70a3cc

Browse files
committed
fix: update artifact chunk basename to ignore directory value in storage settings
1 parent f2b702a commit b70a3cc

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

routers/api/actions/artifacts_chunks.go

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ import (
88
"encoding/base64"
99
"fmt"
1010
"io"
11+
"path"
1112
"path/filepath"
1213
"sort"
13-
"strings"
1414
"time"
1515

1616
"code.gitea.io/gitea/models/actions"
1717
"code.gitea.io/gitea/models/db"
1818
"code.gitea.io/gitea/modules/log"
19-
"code.gitea.io/gitea/modules/setting"
2019
"code.gitea.io/gitea/modules/storage"
2120
)
2221

@@ -32,7 +31,7 @@ func saveUploadChunk(st storage.ObjectStorage, ctx *ArtifactContext,
3231
return -1, fmt.Errorf("parse content range error: %v", err)
3332
}
3433
// build chunk store path
35-
storagePath := fmt.Sprintf("tmp%d/%d-%d-%d.chunk", runID, artifact.ID, start, end)
34+
storagePath := fmt.Sprintf("tmp%d/%d-%d-%d-%d.chunk", runID, runID, artifact.ID, start, end)
3635
// use io.TeeReader to avoid reading all body to md5 sum.
3736
// it writes data to hasher after reading end
3837
// if hash is not matched, delete the read-end result
@@ -61,24 +60,22 @@ func saveUploadChunk(st storage.ObjectStorage, ctx *ArtifactContext,
6160
}
6261

6362
type chunkFileItem struct {
63+
RunID int64
6464
ArtifactID int64
6565
Start int64
6666
End int64
6767
Path string
6868
}
6969

70-
func listChunksByRunID(st storage.ObjectStorage, runID int64, basepath string) (map[int64][]*chunkFileItem, error) {
70+
func listChunksByRunID(st storage.ObjectStorage, runID int64) (map[int64][]*chunkFileItem, error) {
7171
storageDir := fmt.Sprintf("tmp%d", runID)
7272
var chunks []*chunkFileItem
73-
if err := st.IterateObjects(storageDir, func(path string, obj storage.Object) error {
74-
// if basepath is settings.Actions.ArtifactStorage.MinioConfig.BasePath, it should trim it
75-
if basepath != "" {
76-
log.Debug("listChunksByRunID, trim basepath: %s, path: %s", basepath, path)
77-
basepath = strings.TrimPrefix(basepath, "/")
78-
path = strings.TrimPrefix(path, basepath+"/")
79-
}
80-
item := chunkFileItem{Path: path}
81-
if _, err := fmt.Sscanf(path, filepath.Join(storageDir, "%d-%d-%d.chunk"), &item.ArtifactID, &item.Start, &item.End); err != nil {
73+
if err := st.IterateObjects(storageDir, func(fpath string, obj storage.Object) error {
74+
baseName := filepath.Base(fpath)
75+
// when read chunks from storage, it only contains storage dir and basename,
76+
// no matter the subdirectory setting in storage config
77+
item := chunkFileItem{Path: path.Join(storageDir, baseName)}
78+
if _, err := fmt.Sscanf(baseName, "%d-%d-%d-%d.chunk", &item.RunID, &item.ArtifactID, &item.Start, &item.End); err != nil {
8279
return fmt.Errorf("parse content range error: %v", err)
8380
}
8481
chunks = append(chunks, &item)
@@ -103,11 +100,8 @@ func mergeChunksForRun(ctx *ArtifactContext, st storage.ObjectStorage, runID int
103100
if err != nil {
104101
return err
105102
}
106-
// if use minio, it should trim basepath when iterate objects for chunks
107-
// if use local, basepath is empty
108-
basepath := setting.Actions.ArtifactStorage.MinioConfig.BasePath
109103
// read all uploading chunks from storage
110-
chunksMap, err := listChunksByRunID(st, runID, basepath)
104+
chunksMap, err := listChunksByRunID(st, runID)
111105
if err != nil {
112106
return err
113107
}

0 commit comments

Comments
 (0)