Skip to content

Commit f2b702a

Browse files
committed
fix: trim minio basepath when merging chunks
1 parent 4f04ab2 commit f2b702a

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

routers/api/actions/artifacts_chunks.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import (
1010
"io"
1111
"path/filepath"
1212
"sort"
13+
"strings"
1314
"time"
1415

1516
"code.gitea.io/gitea/models/actions"
1617
"code.gitea.io/gitea/models/db"
1718
"code.gitea.io/gitea/modules/log"
19+
"code.gitea.io/gitea/modules/setting"
1820
"code.gitea.io/gitea/modules/storage"
1921
)
2022

@@ -26,6 +28,7 @@ func saveUploadChunk(st storage.ObjectStorage, ctx *ArtifactContext,
2628
contentRange := ctx.Req.Header.Get("Content-Range")
2729
start, end, length := int64(0), int64(0), int64(0)
2830
if _, err := fmt.Sscanf(contentRange, "bytes %d-%d/%d", &start, &end, &length); err != nil {
31+
log.Warn("parse content range error: %v, content-range: %s", err, contentRange)
2932
return -1, fmt.Errorf("parse content range error: %v", err)
3033
}
3134
// build chunk store path
@@ -64,10 +67,16 @@ type chunkFileItem struct {
6467
Path string
6568
}
6669

67-
func listChunksByRunID(st storage.ObjectStorage, runID int64) (map[int64][]*chunkFileItem, error) {
70+
func listChunksByRunID(st storage.ObjectStorage, runID int64, basepath string) (map[int64][]*chunkFileItem, error) {
6871
storageDir := fmt.Sprintf("tmp%d", runID)
6972
var chunks []*chunkFileItem
7073
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+
}
7180
item := chunkFileItem{Path: path}
7281
if _, err := fmt.Sscanf(path, filepath.Join(storageDir, "%d-%d-%d.chunk"), &item.ArtifactID, &item.Start, &item.End); err != nil {
7382
return fmt.Errorf("parse content range error: %v", err)
@@ -94,8 +103,11 @@ func mergeChunksForRun(ctx *ArtifactContext, st storage.ObjectStorage, runID int
94103
if err != nil {
95104
return err
96105
}
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
97109
// read all uploading chunks from storage
98-
chunksMap, err := listChunksByRunID(st, runID)
110+
chunksMap, err := listChunksByRunID(st, runID, basepath)
99111
if err != nil {
100112
return err
101113
}

0 commit comments

Comments
 (0)