From a9f4fd651f10b588f3067b5f1b88c260f0a867b8 Mon Sep 17 00:00:00 2001 From: al2f Date: Thu, 18 Jul 2024 19:24:13 +0930 Subject: [PATCH] Fixed directory view for folders which are not the repository's root. --- pages/dirview.go | 77 +++++++++++++++++++++------------ views/dir.html | 108 +++++++++++++++++++++++++---------------------- 2 files changed, 106 insertions(+), 79 deletions(-) diff --git a/pages/dirview.go b/pages/dirview.go index a9a3a5d..3fbb875 100644 --- a/pages/dirview.go +++ b/pages/dirview.go @@ -1,23 +1,29 @@ package pages import ( - "github.com/tidwall/gjson" "log" "net/http" "os" + "github.com/tidwall/gjson" + "codeberg.org/gothub/gothub/utils" "github.com/gocolly/colly" "github.com/gofiber/fiber/v2" ) type Dir struct { - Readme string - Fullname string - DirName string - Branch string + Readme string + Fullname string + DirName string + Branch string +// TruncationWarning string } +// TODO: Handle repository truncation warning. +// Example text: "Sorry, we had to truncate this directory to 1,000 files. +// 718 entries were omitted from the list. Latest commit info may be omitted." + type DirFiles struct { Name string Path string @@ -26,6 +32,33 @@ type DirFiles struct { Fullname string } +// Given a gjson object, as well as branch name and fullname ('user/repository' format), +// return an array of directory files +func return_dir_files_array(dirList gjson.Result, branch string, fullname string) []DirFiles { + var dirFilesArray []DirFiles + + dirList.ForEach(func(i, e gjson.Result) bool { + var FileType string + if e.Get("contentType").Str == "directory" { + FileType = "dir" + } else { + FileType = "file" + } + + dirFilesArray = append(dirFilesArray, DirFiles{ + Name: e.Get("name").Str, + // Path is the path to the file/folder from the repository root + Path: e.Get("path").Str, + Type: FileType, + Branch: branch, + Fullname: fullname, + }) + + return true //keep iterating + }) + return dirFilesArray + +} func DirView(c *fiber.Ctx) error { var dirArray []Dir var dirFilesArray []DirFiles @@ -56,32 +89,20 @@ func DirView(c *fiber.Ctx) error { sc := colly.NewCollector(colly.AllowedDomains("github.com"), colly.UserAgent(UserAgent)) - sc.OnResponse(func(r *colly.Response) { - jsonMess := string(r.Body) + sc.OnHTML("react-app script[data-target]", func(e *colly.HTMLElement) { + jsonMess := e.Text dirList := gjson.Get(jsonMess, "payload.tree.items") - - dirList.ForEach(func(i, e gjson.Result) bool { - var FileType string - if e.Get("contentType").Str == "directory" { - FileType = "dir" - } else { - FileType = "file" - } - - dirFilesArray = append(dirFilesArray, DirFiles{ - Name: e.Get("name").Str, - // Path is the path to the file/folder from the repository root - Path: e.Get("path").Str, - Type: FileType, - Branch: Scrape.Branch, - Fullname: Scrape.Fullname, - }) - - return true //keep iterating - }) - + dirFilesArray = return_dir_files_array(dirList, Scrape.Branch, Scrape.Fullname) }) + // Get repository truncation warning. This only works with js enabled, so it does not work with colly + /* + sc.OnHTML("[data-testid='repo-truncation-warning']", func(e *colly.HTMLElement) { + fmt.Printf("Truncation warning: %s", e.Text) + Scrape.TruncationWarning = e.Text + }) + */ + sc.Visit("https://github.com/" + c.Params("user") + "/" + c.Params("repo") + "/tree/" + c.Params("branch") + "/" + c.Params("+")) // Add scrape-based info to dirArray dirArray = append(dirArray, Scrape) diff --git a/views/dir.html b/views/dir.html index 9f083d5..215895c 100644 --- a/views/dir.html +++ b/views/dir.html @@ -1,57 +1,63 @@ {{ template "header" .}}
- {{ if .dir }} {{ range $key, $value := .dir}} - - - - {{end}} {{ if .files}} -
-

Files

-
-
    - {{ range $key, $value := .files}} {{ if eq .Type "dir" }} -
  • - 📁 - {{.Name}} -
  • - {{ else }} -
  • - 🗒️ - {{.Name}} -
  • - {{ end }} {{ end }} -
+ {{ if .dir }} {{ range $key, $value := .dir}} + -
- {{ end }} {{ if .readme}} -
- {{ if .dir }} {{ range $key, $value := .dir }} -

{{.Readme}}

- {{end}} {{end}} -
{{ unescape .readme}}
-
- {{ end }} {{ else }} -

Directory not found

-

That directory doesn't exist.

- {{ end }} + + + {{end}} + + {{ if .TruncationWarning }} +
{{.TruncationWarning}}
+ {{ end }} + + {{ if .files}} +
+

Files

+
+
    + {{ range $key, $value := .files}} {{ if eq .Type "dir" }} +
  • + 📁 + {{.Name}} +
  • + {{ else }} +
  • + 🗒️ + {{.Name}} +
  • + {{ end }} {{ end }} +
+
+
+ {{ end }} {{ if .readme}} +
+ {{ if .dir }} {{ range $key, $value := .dir }} +

{{.Readme}}

+ {{end}} {{end}} +
{{ unescape .readme}}
+
+ {{ end }} {{ else }} +

Directory not found

+

That directory doesn't exist.

+ {{ end }}
{{ template "footer" .}}