Fix incorrect relative/absolute URL usages (#29531) (#29547)

Backport #29531 by wxiaoguang

Add two "HTMLURL" methods for PackageDescriptor.
And rename "FullWebLink" to "VersionWebLink"

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
(cherry picked from commit 8723389028bcb5e96359fca61efd7d6da0d6af99)
This commit is contained in:
Giteabot 2024-03-03 02:00:15 +08:00 committed by Earl Warren
parent 22419d9f8d
commit 1a65ecb867
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
4 changed files with 17 additions and 6 deletions

View file

@ -70,16 +70,26 @@ type PackageFileDescriptor struct {
Properties PackagePropertyList Properties PackagePropertyList
} }
// PackageWebLink returns the package web link // PackageWebLink returns the relative package web link
func (pd *PackageDescriptor) PackageWebLink() string { func (pd *PackageDescriptor) PackageWebLink() string {
return fmt.Sprintf("%s/-/packages/%s/%s", pd.Owner.HomeLink(), string(pd.Package.Type), url.PathEscape(pd.Package.LowerName)) 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 // VersionWebLink returns the relative package version web link
func (pd *PackageDescriptor) FullWebLink() string { func (pd *PackageDescriptor) VersionWebLink() string {
return fmt.Sprintf("%s/%s", pd.PackageWebLink(), url.PathEscape(pd.Version.LowerVersion)) 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 // CalculateBlobSize returns the total blobs size in bytes
func (pd *PackageDescriptor) CalculateBlobSize() int64 { func (pd *PackageDescriptor) CalculateBlobSize() int64 {
size := int64(0) size := int64(0)

View file

@ -12,6 +12,7 @@ import (
packages_model "code.gitea.io/gitea/models/packages" packages_model "code.gitea.io/gitea/models/packages"
npm_module "code.gitea.io/gitea/modules/packages/npm" 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 { 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 Maintainers: []npm_module.User{}, // npm cli needs this field
Keywords: metadata.Keywords, Keywords: metadata.Keywords,
Links: &npm_module.PackageSearchPackageLinks{ Links: &npm_module.PackageSearchPackageLinks{
Registry: pd.FullWebLink(), Registry: setting.AppURL + "api/packages/" + pd.Owner.Name + "/npm",
Homepage: metadata.ProjectURL, Homepage: metadata.ProjectURL,
}, },
}, },

View file

@ -161,7 +161,7 @@ func RedirectToLastVersion(ctx *context.Context) {
return return
} }
ctx.Redirect(pd.FullWebLink()) ctx.Redirect(pd.VersionWebLink())
} }
// ViewPackageVersion displays a single package version // ViewPackageVersion displays a single package version

View file

@ -35,7 +35,7 @@ func ToPackage(ctx context.Context, pd *packages.PackageDescriptor, doer *user_m
Name: pd.Package.Name, Name: pd.Package.Name,
Version: pd.Version.Version, Version: pd.Version.Version,
CreatedAt: pd.Version.CreatedUnix.AsTime(), CreatedAt: pd.Version.CreatedUnix.AsTime(),
HTMLURL: pd.FullWebLink(), HTMLURL: pd.VersionHTMLURL(),
}, nil }, nil
} }