forked from NYANDEV/forgejo
Release attachments duplicated check (#26176)
This commit is contained in:
parent
8baa42c8d7
commit
bd7b5e61aa
1 changed files with 14 additions and 15 deletions
|
@ -14,6 +14,7 @@ import (
|
||||||
|
|
||||||
"code.gitea.io/gitea/models/db"
|
"code.gitea.io/gitea/models/db"
|
||||||
user_model "code.gitea.io/gitea/models/user"
|
user_model "code.gitea.io/gitea/models/user"
|
||||||
|
"code.gitea.io/gitea/modules/container"
|
||||||
"code.gitea.io/gitea/modules/structs"
|
"code.gitea.io/gitea/modules/structs"
|
||||||
"code.gitea.io/gitea/modules/timeutil"
|
"code.gitea.io/gitea/modules/timeutil"
|
||||||
"code.gitea.io/gitea/modules/util"
|
"code.gitea.io/gitea/modules/util"
|
||||||
|
@ -336,6 +337,17 @@ func (s releaseMetaSearch) Less(i, j int) bool {
|
||||||
return s.ID[i] < s.ID[j]
|
return s.ID[i] < s.ID[j]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func hasDuplicateName(attaches []*Attachment) bool {
|
||||||
|
attachSet := container.Set[string]{}
|
||||||
|
for _, attachment := range attaches {
|
||||||
|
if attachSet.Contains(attachment.Name) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
attachSet.Add(attachment.Name)
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// GetReleaseAttachments retrieves the attachments for releases
|
// GetReleaseAttachments retrieves the attachments for releases
|
||||||
func GetReleaseAttachments(ctx context.Context, rels ...*Release) (err error) {
|
func GetReleaseAttachments(ctx context.Context, rels ...*Release) (err error) {
|
||||||
if len(rels) == 0 {
|
if len(rels) == 0 {
|
||||||
|
@ -360,7 +372,7 @@ func GetReleaseAttachments(ctx context.Context, rels ...*Release) (err error) {
|
||||||
err = db.GetEngine(ctx).
|
err = db.GetEngine(ctx).
|
||||||
Asc("release_id", "name").
|
Asc("release_id", "name").
|
||||||
In("release_id", sortedRels.ID).
|
In("release_id", sortedRels.ID).
|
||||||
Find(&attachments, Attachment{})
|
Find(&attachments)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -381,21 +393,8 @@ func GetReleaseAttachments(ctx context.Context, rels ...*Release) (err error) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if there are two or more attachments with the same name
|
|
||||||
hasDuplicates := false
|
|
||||||
foundNames := make(map[string]bool)
|
|
||||||
for _, attachment := range release.Attachments {
|
|
||||||
_, found := foundNames[attachment.Name]
|
|
||||||
if found {
|
|
||||||
hasDuplicates = true
|
|
||||||
break
|
|
||||||
} else {
|
|
||||||
foundNames[attachment.Name] = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the names unique, use the URL with the Name instead of the UUID
|
// If the names unique, use the URL with the Name instead of the UUID
|
||||||
if !hasDuplicates {
|
if !hasDuplicateName(release.Attachments) {
|
||||||
for _, attachment := range release.Attachments {
|
for _, attachment := range release.Attachments {
|
||||||
attachment.CustomDownloadURL = release.Repo.HTMLURL() + "/releases/download/" + url.PathEscape(release.TagName) + "/" + url.PathEscape(attachment.Name)
|
attachment.CustomDownloadURL = release.Repo.HTMLURL() + "/releases/download/" + url.PathEscape(release.TagName) + "/" + url.PathEscape(attachment.Name)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue