Skip to content

Commit 128224b

Browse files
fuxiaoheisilverwind
authored andcommitted
Fix merging artifact chunks error when minio storage basepath is set (go-gitea#28555)
Related to go-gitea#28279 When merging artifact chunks, it lists chunks from storage. When storage is minio, chunk's path contains `MINIO_BASE_PATH` that makes merging break. <del>So trim the `MINIO_BASE_PATH` when handle chunks.</del> Update the chunk file's basename to retain necessary information. It ensures that the directory in the chunk's path remains unaffected.
1 parent 3137d5e commit 128224b

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

routers/api/actions/artifacts_chunks.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ func saveUploadChunk(st storage.ObjectStorage, ctx *ArtifactContext,
2626
contentRange := ctx.Req.Header.Get("Content-Range")
2727
start, end, length := int64(0), int64(0), int64(0)
2828
if _, err := fmt.Sscanf(contentRange, "bytes %d-%d/%d", &start, &end, &length); err != nil {
29+
log.Warn("parse content range error: %v, content-range: %s", err, contentRange)
2930
return -1, fmt.Errorf("parse content range error: %v", err)
3031
}
3132
// build chunk store path
32-
storagePath := fmt.Sprintf("tmp%d/%d-%d-%d.chunk", runID, artifact.ID, start, end)
33+
storagePath := fmt.Sprintf("tmp%d/%d-%d-%d-%d.chunk", runID, runID, artifact.ID, start, end)
3334
// use io.TeeReader to avoid reading all body to md5 sum.
3435
// it writes data to hasher after reading end
3536
// if hash is not matched, delete the read-end result
@@ -58,6 +59,7 @@ func saveUploadChunk(st storage.ObjectStorage, ctx *ArtifactContext,
5859
}
5960

6061
type chunkFileItem struct {
62+
RunID int64
6163
ArtifactID int64
6264
Start int64
6365
End int64
@@ -67,9 +69,12 @@ type chunkFileItem struct {
6769
func listChunksByRunID(st storage.ObjectStorage, runID int64) (map[int64][]*chunkFileItem, error) {
6870
storageDir := fmt.Sprintf("tmp%d", runID)
6971
var chunks []*chunkFileItem
70-
if err := st.IterateObjects(storageDir, func(path string, obj storage.Object) error {
71-
item := chunkFileItem{Path: path}
72-
if _, err := fmt.Sscanf(path, filepath.Join(storageDir, "%d-%d-%d.chunk"), &item.ArtifactID, &item.Start, &item.End); err != nil {
72+
if err := st.IterateObjects(storageDir, func(fpath string, obj storage.Object) error {
73+
baseName := filepath.Base(fpath)
74+
// when read chunks from storage, it only contains storage dir and basename,
75+
// no matter the subdirectory setting in storage config
76+
item := chunkFileItem{Path: storageDir + "/" + baseName}
77+
if _, err := fmt.Sscanf(baseName, "%d-%d-%d-%d.chunk", &item.RunID, &item.ArtifactID, &item.Start, &item.End); err != nil {
7378
return fmt.Errorf("parse content range error: %v", err)
7479
}
7580
chunks = append(chunks, &item)

0 commit comments

Comments
 (0)