mirror of
https://github.com/go-gitea/gitea.git
synced 2026-09-02 11:03:23 +09:00
fix: correct repo/attatchment absolute url and release layout (#39178)
This commit is contained in:
@@ -4,30 +4,32 @@
|
||||
package convert
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
repo_model "gitea.dev/models/repo"
|
||||
api "gitea.dev/modules/structs"
|
||||
)
|
||||
|
||||
func WebAssetDownloadURL(repo *repo_model.Repository, attach *repo_model.Attachment) string {
|
||||
return attach.DownloadURL()
|
||||
func WebAssetDownloadURL(ctx context.Context, repo *repo_model.Repository, attach *repo_model.Attachment) string {
|
||||
return attach.DownloadURL(ctx)
|
||||
}
|
||||
|
||||
func APIAssetDownloadURL(repo *repo_model.Repository, attach *repo_model.Attachment) string {
|
||||
return attach.DownloadURL()
|
||||
func APIAssetDownloadURL(ctx context.Context, repo *repo_model.Repository, attach *repo_model.Attachment) string {
|
||||
return attach.DownloadURL(ctx)
|
||||
}
|
||||
|
||||
// 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)
|
||||
func ToAttachment(ctx context.Context, repo *repo_model.Repository, a *repo_model.Attachment) *api.Attachment {
|
||||
return toAttachment(ctx, 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)
|
||||
func ToAPIAttachment(ctx context.Context, repo *repo_model.Repository, a *repo_model.Attachment) *api.Attachment {
|
||||
return toAttachment(ctx, 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 {
|
||||
func toAttachment(ctx context.Context, repo *repo_model.Repository, a *repo_model.Attachment, getDownloadURL func(ctx context.Context, repo *repo_model.Repository, attach *repo_model.Attachment) string) *api.Attachment {
|
||||
return &api.Attachment{
|
||||
ID: a.ID,
|
||||
Name: a.Name,
|
||||
@@ -35,18 +37,18 @@ func toAttachment(repo *repo_model.Repository, a *repo_model.Attachment, getDown
|
||||
DownloadCount: a.DownloadCount,
|
||||
Size: a.Size,
|
||||
UUID: a.UUID,
|
||||
DownloadURL: getDownloadURL(repo, a), // for web request json and api request json, return different download urls
|
||||
DownloadURL: getDownloadURL(ctx, repo, a), // for web/api requests, return different download URLs
|
||||
}
|
||||
}
|
||||
|
||||
func ToAPIAttachments(repo *repo_model.Repository, attachments []*repo_model.Attachment) []*api.Attachment {
|
||||
return toAttachments(repo, attachments, APIAssetDownloadURL)
|
||||
func ToAPIAttachments(ctx context.Context, repo *repo_model.Repository, attachments []*repo_model.Attachment) []*api.Attachment {
|
||||
return toAttachments(ctx, 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 {
|
||||
func toAttachments(ctx context.Context, repo *repo_model.Repository, attachments []*repo_model.Attachment, getDownloadURL func(ctx context.Context, 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(repo, attachment, getDownloadURL))
|
||||
converted = append(converted, toAttachment(ctx, repo, attachment, getDownloadURL))
|
||||
}
|
||||
return converted
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ func ToAPIIssue(ctx context.Context, doer *user_model.User, issue *issues_model.
|
||||
return toIssue(ctx, doer, issue, APIAssetDownloadURL)
|
||||
}
|
||||
|
||||
func toIssue(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, getDownloadURL func(repo *repo_model.Repository, attach *repo_model.Attachment) string) *api.Issue {
|
||||
func toIssue(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, getDownloadURL func(ctx context.Context, repo *repo_model.Repository, attach *repo_model.Attachment) string) *api.Issue {
|
||||
if err := issue.LoadPoster(ctx); err != nil {
|
||||
return &api.Issue{}
|
||||
}
|
||||
@@ -53,7 +53,7 @@ func toIssue(ctx context.Context, doer *user_model.User, issue *issues_model.Iss
|
||||
Poster: ToUser(ctx, issue.Poster, doer),
|
||||
Title: issue.Title,
|
||||
Body: issue.Content,
|
||||
Attachments: toAttachments(issue.Repo, issue.Attachments, getDownloadURL),
|
||||
Attachments: toAttachments(ctx, issue.Repo, issue.Attachments, getDownloadURL),
|
||||
Ref: issue.Ref,
|
||||
State: issue.State(),
|
||||
IsLocked: issue.IsLocked,
|
||||
|
||||
@@ -23,7 +23,7 @@ func ToAPIComment(ctx context.Context, repo *repo_model.Repository, c *issues_mo
|
||||
IssueURL: c.IssueURL(ctx),
|
||||
PRURL: c.PRURL(ctx),
|
||||
Body: c.Content,
|
||||
Attachments: ToAPIAttachments(repo, c.Attachments),
|
||||
Attachments: ToAPIAttachments(ctx, repo, c.Attachments),
|
||||
Created: c.CreatedUnix.AsTime(),
|
||||
Updated: c.UpdatedUnix.AsTime(),
|
||||
}
|
||||
|
||||
@@ -29,6 +29,6 @@ func ToAPIRelease(ctx context.Context, repo *repo_model.Repository, r *repo_mode
|
||||
CreatedAt: r.CreatedUnix.AsTime(),
|
||||
PublishedAt: util.Iif(r.IsDraft, nil, r.PublishedUnix.AsTimePtr()),
|
||||
Publisher: ToUser(ctx, r.Publisher, nil),
|
||||
Attachments: ToAPIAttachments(repo, r.Attachments),
|
||||
Attachments: ToAPIAttachments(ctx, repo, r.Attachments),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,29 +202,17 @@ func getFileContentsByEntryInternal(ctx context.Context, repo *repo_model.Reposi
|
||||
}
|
||||
// Handle links
|
||||
if entry.IsRegular() || entry.IsLink() || entry.IsExecutable() {
|
||||
downloadURL, err := url.Parse(repo.HTMLURL() + "/raw/" + refCommit.RefName.RefWebLinkPath() + "/" + util.PathEscapeSegments(opts.TreePath))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
downloadURLString := downloadURL.String()
|
||||
contentsResponse.DownloadURL = &downloadURLString
|
||||
downloadURL := repo.HTMLURL(ctx) + "/raw/" + refCommit.RefName.RefWebLinkPath() + "/" + util.PathEscapeSegments(opts.TreePath)
|
||||
contentsResponse.DownloadURL = &downloadURL
|
||||
}
|
||||
if !entry.IsSubModule() {
|
||||
htmlURL, err := url.Parse(repo.HTMLURL() + "/src/" + refCommit.RefName.RefWebLinkPath() + "/" + util.PathEscapeSegments(opts.TreePath))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
htmlURLString := htmlURL.String()
|
||||
contentsResponse.HTMLURL = &htmlURLString
|
||||
contentsResponse.Links.HTMLURL = &htmlURLString
|
||||
htmlURL := repo.HTMLURL(ctx) + "/src/" + refCommit.RefName.RefWebLinkPath() + "/" + util.PathEscapeSegments(opts.TreePath)
|
||||
contentsResponse.HTMLURL = &htmlURL
|
||||
contentsResponse.Links.HTMLURL = &htmlURL
|
||||
|
||||
gitURL, err := url.Parse(repo.APIURL() + "/git/blobs/" + url.PathEscape(entry.ID.String()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
gitURLString := gitURL.String()
|
||||
contentsResponse.GitURL = &gitURLString
|
||||
contentsResponse.Links.GitURL = &gitURLString
|
||||
gitURL := repo.APIURL(ctx) + "/git/blobs/" + url.PathEscape(entry.ID.String())
|
||||
contentsResponse.GitURL = &gitURL
|
||||
contentsResponse.Links.GitURL = &gitURL
|
||||
}
|
||||
|
||||
return contentsResponse, nil
|
||||
|
||||
@@ -89,13 +89,13 @@ func GetFileCommitResponse(ctx context.Context, repo *repo_model.Repository, git
|
||||
}
|
||||
}
|
||||
}
|
||||
commitHTMLURL, _ := url.Parse(repo.HTMLURL() + "/commit/" + url.PathEscape(commit.ID.String()))
|
||||
commitHTMLURL := repo.HTMLURL(ctx) + "/commit/" + url.PathEscape(commit.ID.String())
|
||||
fileCommit := &api.FileCommitResponse{
|
||||
CommitMeta: api.CommitMeta{
|
||||
SHA: commit.ID.String(),
|
||||
URL: commitURL.String(),
|
||||
},
|
||||
HTMLURL: commitHTMLURL.String(),
|
||||
HTMLURL: commitHTMLURL,
|
||||
Author: &api.CommitUser{
|
||||
Identity: api.Identity{
|
||||
Name: commit.Author.Name,
|
||||
|
||||
Reference in New Issue
Block a user