From 12441d7c5cb6e4a8753a2fd7ac8fe95cc8d4c7f3 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sun, 3 Mar 2024 01:38:38 +0800 Subject: [PATCH] Fix incorrect relative/absolute URL usages (#29531) Add two "HTMLURL" methods for PackageDescriptor. And rename "FullWebLink" to "VersionWebLink" (cherry picked from commit bf6502a8f7a2e9a2b64b43b7733316d863c9a768) --- models/packages/descriptor.go | 16 +++++++++++++--- routers/api/packages/npm/api.go | 3 ++- routers/web/user/package.go | 2 +- services/convert/package.go | 2 +- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/models/packages/descriptor.go b/models/packages/descriptor.go index f849ab5c0..b8ef698d3 100644 --- a/models/packages/descriptor.go +++ b/models/packages/descriptor.go @@ -70,16 +70,26 @@ type PackageFileDescriptor struct { Properties PackagePropertyList } -// PackageWebLink returns the package web link +// PackageWebLink returns the relative package web link func (pd *PackageDescriptor) PackageWebLink() string { return fmt.Sprintf("%s/-/packages/%s/%s", pd.Owner.HomeLink(), string(pd.Package.Type), url.PathEscape(pd.Package.LowerName)) } -// FullWebLink returns the package version web link -func (pd *PackageDescriptor) FullWebLink() string { +// VersionWebLink returns the relative package version web link +func (pd *PackageDescriptor) VersionWebLink() string { return fmt.Sprintf("%s/%s", pd.PackageWebLink(), url.PathEscape(pd.Version.LowerVersion)) } +// PackageHTMLURL returns the absolute package HTML URL +func (pd *PackageDescriptor) PackageHTMLURL() string { + return fmt.Sprintf("%s/-/packages/%s/%s", pd.Owner.HTMLURL(), string(pd.Package.Type), url.PathEscape(pd.Package.LowerName)) +} + +// VersionHTMLURL returns the absolute package version HTML URL +func (pd *PackageDescriptor) VersionHTMLURL() string { + return fmt.Sprintf("%s/%s", pd.PackageHTMLURL(), url.PathEscape(pd.Version.LowerVersion)) +} + // CalculateBlobSize returns the total blobs size in bytes func (pd *PackageDescriptor) CalculateBlobSize() int64 { size := int64(0) diff --git a/routers/api/packages/npm/api.go b/routers/api/packages/npm/api.go index 847087488..f8e839c42 100644 --- a/routers/api/packages/npm/api.go +++ b/routers/api/packages/npm/api.go @@ -12,6 +12,7 @@ import ( packages_model "code.gitea.io/gitea/models/packages" npm_module "code.gitea.io/gitea/modules/packages/npm" + "code.gitea.io/gitea/modules/setting" ) func createPackageMetadataResponse(registryURL string, pds []*packages_model.PackageDescriptor) *npm_module.PackageMetadata { @@ -98,7 +99,7 @@ func createPackageSearchResponse(pds []*packages_model.PackageDescriptor, total Maintainers: []npm_module.User{}, // npm cli needs this field Keywords: metadata.Keywords, Links: &npm_module.PackageSearchPackageLinks{ - Registry: pd.FullWebLink(), + Registry: setting.AppURL + "api/packages/" + pd.Owner.Name + "/npm", Homepage: metadata.ProjectURL, }, }, diff --git a/routers/web/user/package.go b/routers/web/user/package.go index 4f3de13df..3ecc59a2a 100644 --- a/routers/web/user/package.go +++ b/routers/web/user/package.go @@ -163,7 +163,7 @@ func RedirectToLastVersion(ctx *context.Context) { return } - ctx.Redirect(pd.FullWebLink()) + ctx.Redirect(pd.VersionWebLink()) } // ViewPackageVersion displays a single package version diff --git a/services/convert/package.go b/services/convert/package.go index e90ce8a00..b5fca21a3 100644 --- a/services/convert/package.go +++ b/services/convert/package.go @@ -35,7 +35,7 @@ func ToPackage(ctx context.Context, pd *packages.PackageDescriptor, doer *user_m Name: pd.Package.Name, Version: pd.Version.Version, CreatedAt: pd.Version.CreatedUnix.AsTime(), - HTMLURL: pd.FullWebLink(), + HTMLURL: pd.VersionHTMLURL(), }, nil }