Refactor arch route handlers (#32972)

This commit is contained in:
wxiaoguang 2024-12-25 12:03:14 +08:00 committed by GitHub
parent 65e45fdcfd
commit ca31d478ee
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 218 additions and 147 deletions

View file

@ -37,6 +37,8 @@ import (
"code.gitea.io/gitea/routers/api/packages/vagrant"
"code.gitea.io/gitea/services/auth"
"code.gitea.io/gitea/services/context"
"github.com/go-chi/chi/v5"
)
func reqPackageAccess(accessMode perm.AccessMode) func(ctx *context.Context) {
@ -139,39 +141,33 @@ func CommonRoutes() *web.Router {
r.Group("/arch", func() {
r.Methods("HEAD,GET", "/repository.key", arch.GetRepositoryKey)
r.Methods("HEAD,GET,PUT,DELETE", "*", func(ctx *context.Context) {
path := strings.Trim(ctx.PathParam("*"), "/")
reqPutRepository := web.NewPathProcessor("PUT", "/<repository:*>")
reqGetRepoArchFile := web.NewPathProcessor("HEAD,GET", "/<repository:*>/<architecture>/<filename>")
reqDeleteRepoNameVerArch := web.NewPathProcessor("DELETE", "/<repository:*>/<name>/<version>/<architecture>")
if ctx.Req.Method == "PUT" {
r.Any("*", func(ctx *context.Context) {
chiCtx := chi.RouteContext(ctx.Req.Context())
path := ctx.PathParam("*")
if reqPutRepository.ProcessRequestPath(chiCtx, path) {
reqPackageAccess(perm.AccessModeWrite)(ctx)
if ctx.Written() {
return
}
ctx.SetPathParam("repository", path)
arch.UploadPackageFile(ctx)
return
}
pathFields := strings.Split(path, "/")
pathFieldsLen := len(pathFields)
if (ctx.Req.Method == "HEAD" || ctx.Req.Method == "GET") && pathFieldsLen >= 2 {
ctx.SetPathParam("repository", strings.Join(pathFields[:pathFieldsLen-2], "/"))
ctx.SetPathParam("architecture", pathFields[pathFieldsLen-2])
ctx.SetPathParam("filename", pathFields[pathFieldsLen-1])
if reqGetRepoArchFile.ProcessRequestPath(chiCtx, path) {
arch.GetPackageOrRepositoryFile(ctx)
return
}
if ctx.Req.Method == "DELETE" && pathFieldsLen >= 3 {
if reqDeleteRepoNameVerArch.ProcessRequestPath(chiCtx, path) {
reqPackageAccess(perm.AccessModeWrite)(ctx)
if ctx.Written() {
return
}
ctx.SetPathParam("repository", strings.Join(pathFields[:pathFieldsLen-3], "/"))
ctx.SetPathParam("name", pathFields[pathFieldsLen-3])
ctx.SetPathParam("version", pathFields[pathFieldsLen-2])
ctx.SetPathParam("architecture", pathFields[pathFieldsLen-1])
arch.DeletePackageVersion(ctx)
return
}