Skip to content

Commit bd7b5e6

Browse files
authored
Release attachments duplicated check (#26176)
1 parent 8baa42c commit bd7b5e6

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

models/repo/release.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"code.gitea.io/gitea/models/db"
1616
user_model "code.gitea.io/gitea/models/user"
17+
"code.gitea.io/gitea/modules/container"
1718
"code.gitea.io/gitea/modules/structs"
1819
"code.gitea.io/gitea/modules/timeutil"
1920
"code.gitea.io/gitea/modules/util"
@@ -336,6 +337,17 @@ func (s releaseMetaSearch) Less(i, j int) bool {
336337
return s.ID[i] < s.ID[j]
337338
}
338339

340+
func hasDuplicateName(attaches []*Attachment) bool {
341+
attachSet := container.Set[string]{}
342+
for _, attachment := range attaches {
343+
if attachSet.Contains(attachment.Name) {
344+
return true
345+
}
346+
attachSet.Add(attachment.Name)
347+
}
348+
return false
349+
}
350+
339351
// GetReleaseAttachments retrieves the attachments for releases
340352
func GetReleaseAttachments(ctx context.Context, rels ...*Release) (err error) {
341353
if len(rels) == 0 {
@@ -360,7 +372,7 @@ func GetReleaseAttachments(ctx context.Context, rels ...*Release) (err error) {
360372
err = db.GetEngine(ctx).
361373
Asc("release_id", "name").
362374
In("release_id", sortedRels.ID).
363-
Find(&attachments, Attachment{})
375+
Find(&attachments)
364376
if err != nil {
365377
return err
366378
}
@@ -381,21 +393,8 @@ func GetReleaseAttachments(ctx context.Context, rels ...*Release) (err error) {
381393
continue
382394
}
383395

384-
// Check if there are two or more attachments with the same name
385-
hasDuplicates := false
386-
foundNames := make(map[string]bool)
387-
for _, attachment := range release.Attachments {
388-
_, found := foundNames[attachment.Name]
389-
if found {
390-
hasDuplicates = true
391-
break
392-
} else {
393-
foundNames[attachment.Name] = true
394-
}
395-
}
396-
397396
// If the names unique, use the URL with the Name instead of the UUID
398-
if !hasDuplicates {
397+
if !hasDuplicateName(release.Attachments) {
399398
for _, attachment := range release.Attachments {
400399
attachment.CustomDownloadURL = release.Repo.HTMLURL() + "/releases/download/" + url.PathEscape(release.TagName) + "/" + url.PathEscape(attachment.Name)
401400
}

0 commit comments

Comments
 (0)