1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-22 18:28:37 +00:00

For API attachments, use API URL (#25639)

Fix #25257

---------

Co-authored-by: Giteabot <teabot@gitea.io>
This commit is contained in:
Lunny Xiao
2023-07-10 17:31:19 +08:00
committed by GitHub
parent 5489962aac
commit 0fd1672ae4
19 changed files with 108 additions and 67 deletions

View File

@@ -4,12 +4,38 @@
package convert
import (
"strconv"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
)
// ToAttachment converts models.Attachment to api.Attachment
func ToAttachment(a *repo_model.Attachment) *api.Attachment {
func WebAssetDownloadURL(repo *repo_model.Repository, attach *repo_model.Attachment) string {
return attach.DownloadURL()
}
func APIAssetDownloadURL(repo *repo_model.Repository, attach *repo_model.Attachment) string {
if attach.CustomDownloadURL != "" {
return attach.CustomDownloadURL
}
// /repos/{owner}/{repo}/releases/{id}/assets/{attachment_id}
return setting.AppURL + "api/repos/" + repo.FullName() + "/releases/" + strconv.FormatInt(attach.ReleaseID, 10) + "/assets/" + strconv.FormatInt(attach.ID, 10)
}
// ToAttachment converts models.Attachment to api.Attachment for API usage
func ToAttachment(repo *repo_model.Repository, a *repo_model.Attachment) *api.Attachment {
return toAttachment(repo, a, WebAssetDownloadURL)
}
// ToAPIAttachment converts models.Attachment to api.Attachment for API usage
func ToAPIAttachment(repo *repo_model.Repository, a *repo_model.Attachment) *api.Attachment {
return toAttachment(repo, a, APIAssetDownloadURL)
}
// toAttachment converts models.Attachment to api.Attachment for API usage
func toAttachment(repo *repo_model.Repository, a *repo_model.Attachment, getDownloadURL func(repo *repo_model.Repository, attach *repo_model.Attachment) string) *api.Attachment {
return &api.Attachment{
ID: a.ID,
Name: a.Name,
@@ -17,14 +43,18 @@ func ToAttachment(a *repo_model.Attachment) *api.Attachment {
DownloadCount: a.DownloadCount,
Size: a.Size,
UUID: a.UUID,
DownloadURL: a.DownloadURL(),
DownloadURL: getDownloadURL(repo, a), // for web request json and api request json, return different download urls
}
}
func ToAttachments(attachments []*repo_model.Attachment) []*api.Attachment {
func ToAPIAttachments(repo *repo_model.Repository, attachments []*repo_model.Attachment) []*api.Attachment {
return toAttachments(repo, attachments, APIAssetDownloadURL)
}
func toAttachments(repo *repo_model.Repository, attachments []*repo_model.Attachment, getDownloadURL func(repo *repo_model.Repository, attach *repo_model.Attachment) string) []*api.Attachment {
converted := make([]*api.Attachment, 0, len(attachments))
for _, attachment := range attachments {
converted = append(converted, ToAttachment(attachment))
converted = append(converted, toAttachment(repo, attachment, getDownloadURL))
}
return converted
}