mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-11-04 02:04:11 +01:00 
			
		
		
		
	Merge b4efc0e726e2ab52e7f518ccba5036bf69740fce into 3ab8ae5807775a81069005a5abbd20bacfa0a15f
This commit is contained in:
		
						commit
						a6a3d67b43
					
				@ -336,6 +336,7 @@ func CommonRoutes() *web.Router {
 | 
			
		||||
			})
 | 
			
		||||
		}, reqPackageAccess(perm.AccessModeRead))
 | 
			
		||||
		r.Group("/generic", func() {
 | 
			
		||||
			r.Get("/{packagename}/list", generic.EnumeratePackageVersions)
 | 
			
		||||
			r.Group("/{packagename}/{packageversion}", func() {
 | 
			
		||||
				r.Delete("", reqPackageAccess(perm.AccessModeWrite), generic.DeletePackage)
 | 
			
		||||
				r.Group("/{filename}", func() {
 | 
			
		||||
 | 
			
		||||
@ -12,6 +12,7 @@ import (
 | 
			
		||||
 | 
			
		||||
	packages_model "code.gitea.io/gitea/models/packages"
 | 
			
		||||
	packages_module "code.gitea.io/gitea/modules/packages"
 | 
			
		||||
	"code.gitea.io/gitea/modules/timeutil"
 | 
			
		||||
	"code.gitea.io/gitea/routers/api/packages/helper"
 | 
			
		||||
	"code.gitea.io/gitea/services/context"
 | 
			
		||||
	packages_service "code.gitea.io/gitea/services/packages"
 | 
			
		||||
@ -22,11 +23,70 @@ var (
 | 
			
		||||
	filenameRegex    = regexp.MustCompile(`\A[-_+=:;.()\[\]{}~!@#$%^& \w]+\z`)
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// PackageFileInfo represents information about an existing package file
 | 
			
		||||
// swagger:model
 | 
			
		||||
type PackageFileInfo struct {
 | 
			
		||||
	// Name of package file
 | 
			
		||||
	Name string `json:"name"`
 | 
			
		||||
	// swagger:strfmt date-time
 | 
			
		||||
	// Date when package file was created/uploaded
 | 
			
		||||
	CreatedUnix timeutil.TimeStamp `json:"created"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// PackageInfo represents information about an existing package file
 | 
			
		||||
// swagger:model
 | 
			
		||||
type PackageInfo struct {
 | 
			
		||||
	/// Version linked to package information
 | 
			
		||||
	Version string `json:"version"`
 | 
			
		||||
	/// Download count for files within version
 | 
			
		||||
	DownloadCount int64 `json:"downloads"`
 | 
			
		||||
	/// Files uploaded for package version
 | 
			
		||||
	Files []PackageFileInfo `json:"files"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func apiError(ctx *context.Context, status int, obj any) {
 | 
			
		||||
	message := helper.ProcessErrorForUser(ctx, status, obj)
 | 
			
		||||
	ctx.PlainText(status, message)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// EnumeratePackageVersions lists upload versions and their associated files
 | 
			
		||||
func EnumeratePackageVersions(ctx *context.Context) {
 | 
			
		||||
	pvs, err := packages_model.GetVersionsByPackageName(ctx, ctx.Package.Owner.ID, packages_model.TypeGeneric, ctx.PathParam("packagename"))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		apiError(ctx, http.StatusInternalServerError, err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	if len(pvs) == 0 {
 | 
			
		||||
		apiError(ctx, http.StatusNotFound, err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var info []PackageInfo
 | 
			
		||||
	for _, pv := range pvs {
 | 
			
		||||
		packageFiles, err := packages_model.GetFilesByVersionID(ctx, pv.ID)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			apiError(ctx, http.StatusInternalServerError, err)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		var files []PackageFileInfo
 | 
			
		||||
		for _, file := range packageFiles {
 | 
			
		||||
			files = append(files, PackageFileInfo{
 | 
			
		||||
				Name:        file.Name,
 | 
			
		||||
				CreatedUnix: file.CreatedUnix,
 | 
			
		||||
			})
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		info = append(info, PackageInfo{
 | 
			
		||||
			Version:       pv.Version,
 | 
			
		||||
			DownloadCount: pv.DownloadCount,
 | 
			
		||||
			Files:         files,
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ctx.JSON(http.StatusOK, info)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// DownloadPackageFile serves the specific generic package.
 | 
			
		||||
func DownloadPackageFile(ctx *context.Context) {
 | 
			
		||||
	s, u, pf, err := packages_service.OpenFileForDownloadByPackageNameAndVersion(
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user