extract repo name/path mangling to middleware
1 file changed, 31 insertions(+), 121 deletions(-)
changed files
M routes/routes.go → routes/routes.go
@@ -4,7 +4,6 @@ import ( "compress/gzip" "fmt" "log" - "path" "path/filepath" "strconv" "strings"@@ -41,21 +40,10 @@ return templates.IndexPage(pageData, d.repos).Render(rc) } func (d *deps) RepoIndex(rc *atreugo.RequestCtx) error { - category, _ := rc.UserValue("category").(string) - name, _ := rc.UserValue("name").(string) - repoName := path.Join(category, name) + repoName, _ := rc.UserValue("repoName").(string) + repoPath, _ := rc.UserValue("repoPath").(string) - if d.isNotAllowed(repoName) { - return d.NotFound(rc) - } - path, err := d.GetCleanPath(repoName) - if err != nil { - log.Printf("getcleanpath error: %v", err) - - return d.NotFound(rc) - } - - gr, err := git.Open(path, "") + gr, err := git.Open(repoPath, "") if err != nil { return d.NotFound(rc) }@@ -108,7 +96,7 @@ Meta: d.c.Meta, Name: repoName, DisplayName: getDisplayName(repoName), Ref: mainBranch, - Description: getDescription(path), + Description: getDescription(repoPath), Servername: d.c.Server.Name, Gomod: isGoModule(gr), }@@ -119,25 +107,14 @@ return templates.RepoPage(pageData, commits, readmeContent).Render(rc) } func (d *deps) RepoTree(rc *atreugo.RequestCtx) error { - category, _ := rc.UserValue("category").(string) - name, _ := rc.UserValue("name").(string) - repoName := path.Join(category, name) - - if d.isNotAllowed(repoName) { - return d.NotFound(rc) - } + repoName, _ := rc.UserValue("repoName").(string) + repoPath, _ := rc.UserValue("repoPath").(string) rest, _ := rc.UserValue("rest").(string) treePath := strings.TrimSuffix(rest, "/") ref := rc.UserValue("ref").(string) - path, err := d.GetCleanPath(repoName) - if err != nil { - log.Printf("getcleanpath error: %v", err) - - return d.NotFound(rc) - } - gr, err := git.Open(path, ref) + gr, err := git.Open(repoPath, ref) if err != nil { return err }@@ -152,7 +129,7 @@ data["name"] = repoName data["displayname"] = getDisplayName(repoName) data["ref"] = ref data["parent"] = treePath - data["desc"] = getDescription(path) + data["desc"] = getDescription(repoPath) data["dotdot"] = filepath.Dir(treePath) rc.SetContentType("text/html; charset=utf-8")@@ -166,24 +143,13 @@ if rawParam, err := strconv.ParseBool(string(rc.Request.URI().QueryArgs().Peek("raw"))); err == nil { raw = rawParam } - category, _ := rc.UserValue("category").(string) - name, _ := rc.UserValue("name").(string) - repoName := path.Join(category, name) + repoName, _ := rc.UserValue("repoName").(string) + repoPath, _ := rc.UserValue("repoPath").(string) - if d.isNotAllowed(repoName) { - return d.NotFound(rc) - } treePath := rc.UserValue("rest").(string) ref := rc.UserValue("ref").(string) - path, err := d.GetCleanPath(repoName) - if err != nil { - log.Printf("getcleanpath error: %v", err) - - return d.NotFound(rc) - } - - gr, err := git.Open(path, ref) + gr, err := git.Open(repoPath, ref) if err != nil { return d.NotFound(rc) }@@ -196,7 +162,7 @@ data := make(map[string]any) data["name"] = repoName data["displayname"] = getDisplayName(repoName) data["ref"] = ref - data["desc"] = getDescription(path) + data["desc"] = getDescription(repoPath) data["path"] = treePath if raw {@@ -209,13 +175,8 @@ return d.showFile(contents, data, rc) } func (d *deps) Archive(rc *atreugo.RequestCtx) error { - category, _ := rc.UserValue("category").(string) - name, _ := rc.UserValue("name").(string) - repoName := path.Join(category, name) - - if d.isNotAllowed(repoName) { - return d.NotFound(rc) - } + repoName, _ := rc.UserValue("repoName").(string) + repoPath, _ := rc.UserValue("repoPath").(string) file := rc.UserValue("file").(string)@@ -230,14 +191,7 @@ filename := fmt.Sprintf("%s-%s.tar.gz", repoName, ref) setContentDisposition(rc, filename) setGZipMIME(rc) - path, err := d.GetCleanPath(repoName) - if err != nil { - log.Printf("getcleanpath error: %v", err) - - return d.NotFound(rc) - } - - gr, err := git.Open(path, ref) + gr, err := git.Open(repoPath, ref) if err != nil { return d.NotFound(rc) }@@ -271,23 +225,11 @@ return nil } func (d *deps) Log(rc *atreugo.RequestCtx) error { - category, _ := rc.UserValue("category").(string) - name, _ := rc.UserValue("name").(string) - repoName := path.Join(category, name) - - if d.isNotAllowed(repoName) { - return d.NotFound(rc) - } + repoName, _ := rc.UserValue("repoName").(string) + repoPath, _ := rc.UserValue("repoPath").(string) ref := rc.UserValue("ref").(string) - path, err := d.GetCleanPath(repoName) - if err != nil { - log.Printf("getcleanpath error: %v", err) - - return d.NotFound(rc) - } - - gr, err := git.Open(path, ref) + gr, err := git.Open(repoPath, ref) if err != nil { return d.NotFound(rc) }@@ -302,7 +244,7 @@ Meta: d.c.Meta, Name: repoName, DisplayName: getDisplayName(repoName), Ref: ref, - Description: getDescription(path), + Description: getDescription(repoPath), Log: true, }@@ -312,22 +254,11 @@ return templates.LogPage(pageData, commits).Render(rc) } func (d *deps) Diff(rc *atreugo.RequestCtx) error { - category, _ := rc.UserValue("category").(string) - name, _ := rc.UserValue("name").(string) - repoName := path.Join(category, name) - - if d.isNotAllowed(repoName) { - return d.NotFound(rc) - } + repoName, _ := rc.UserValue("repoName").(string) + repoPath, _ := rc.UserValue("repoPath").(string) ref := rc.UserValue("ref").(string) - path, err := d.GetCleanPath(repoName) - if err != nil { - log.Printf("getcleanpath error: %v", err) - - return d.NotFound(rc) - } - gr, err := git.Open(path, ref) + gr, err := git.Open(repoPath, ref) if err != nil { return d.NotFound(rc) }@@ -344,7 +275,7 @@ Stat: diff.Stat, Diff: diff.Diff, DisplayName: getDisplayName(repoName), Ref: ref, - Description: getDescription(path), + Description: getDescription(repoPath), } rc.SetContentType("text/html; charset=utf-8")@@ -354,24 +285,13 @@ } // FileDiff shows the changes to a specific file in a commit func (d *deps) FileDiff(rc *atreugo.RequestCtx) error { - category, _ := rc.UserValue("category").(string) - name, _ := rc.UserValue("name").(string) - repoName := path.Join(category, name) + repoName, _ := rc.UserValue("repoName").(string) + repoPath, _ := rc.UserValue("repoPath").(string) - if d.isNotAllowed(repoName) { - return d.NotFound(rc) - } ref := rc.UserValue("ref").(string) filePath := strings.TrimSuffix(rc.UserValue("file").(string), "/") - path, err := d.GetCleanPath(repoName) - if err != nil { - log.Printf("getcleanpath error: %v", err) - - return d.NotFound(rc) - } - - g, err := git.Open(path, ref) + g, err := git.Open(repoPath, ref) if err != nil { return err }@@ -386,7 +306,7 @@ Meta: d.c.Meta, DisplayName: getDisplayName(repoName), Name: repoName, Ref: ref, - Description: getDescription(path), + Description: getDescription(repoPath), Path: filePath, Diff: diff.Diff, }@@ -397,20 +317,10 @@ return templates.CommitPage(pageData, diff).Render(rc) } func (d *deps) Refs(rc *atreugo.RequestCtx) error { - category, _ := rc.UserValue("category").(string) - name, _ := rc.UserValue("name").(string) - repoName := path.Join(category, name) + repoName, _ := rc.UserValue("repoName").(string) + repoPath, _ := rc.UserValue("repoPath").(string) - if d.isNotAllowed(repoName) { - return d.NotFound(rc) - } - - path, err := d.GetCleanPath(repoName) - if err != nil { - return err - } - - gr, err := git.Open(path, "") + gr, err := git.Open(repoPath, "") if err != nil { return d.NotFound(rc) }@@ -430,7 +340,7 @@ pageData := templates.PageData{ Meta: d.c.Meta, Name: repoName, DisplayName: getDisplayName(repoName), - Description: getDescription(path), + Description: getDescription(repoPath), } rc.SetContentType("text/html; charset=utf-8")