mirror of
https://codeberg.org/gothub/gothub
synced 2024-12-06 19:16:24 +01:00
Merge pull request 'Fixed directory view for folders which are not the repository's root.' (#152) from al2f/gothub:nested-folder-view into dev
Reviewed-on: https://codeberg.org/gothub/gothub/pulls/152
This commit is contained in:
+49
-28
@@ -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)
|
||||
|
||||
+57
-51
@@ -1,57 +1,63 @@
|
||||
{{ template "header" .}}
|
||||
|
||||
<main>
|
||||
{{ if .dir }} {{ range $key, $value := .dir}}
|
||||
<div class="button-parent">
|
||||
<a
|
||||
rel="noreferrer"
|
||||
href="https://github.com/{{.Fullname}}/tree/{{.Branch}}/{{.DirName}}"
|
||||
class="button"
|
||||
>View on GitHub</a
|
||||
>
|
||||
<a href="/{{.Fullname}}/tree/{{.Branch}}" class="button"
|
||||
>Back to {{.Fullname}}</a
|
||||
>
|
||||
</div>
|
||||
|
||||
<div class="user-profile">
|
||||
<h1>{{.Fullname}} | {{.DirName}}</h1>
|
||||
</div>
|
||||
{{end}} {{ if .files}}
|
||||
<div class="user-readme">
|
||||
<h3>Files</h3>
|
||||
<div class="user-readme-text">
|
||||
<ul class="file-u-list">
|
||||
{{ range $key, $value := .files}} {{ if eq .Type "dir" }}
|
||||
<li class="file-list">
|
||||
📁
|
||||
<a
|
||||
href="/{{.Fullname}}/tree/{{.Branch}}/{{.Path}}"
|
||||
class="file-a"
|
||||
>{{.Name}}</a
|
||||
>
|
||||
</li>
|
||||
{{ else }}
|
||||
<li class="file-list">
|
||||
🗒️
|
||||
<a href="/{{.Fullname}}/blob/{{.Branch}}/{{.Path}}"
|
||||
>{{.Name}}</a
|
||||
>
|
||||
</li>
|
||||
{{ end }} {{ end }}
|
||||
</ul>
|
||||
{{ if .dir }} {{ range $key, $value := .dir}}
|
||||
<div class="button-parent">
|
||||
<a
|
||||
rel="noreferrer"
|
||||
href="https://github.com/{{.Fullname}}/tree/{{.Branch}}/{{.DirName}}"
|
||||
class="button"
|
||||
>View on GitHub</a
|
||||
>
|
||||
<a href="/{{.Fullname}}/tree/{{.Branch}}" class="button"
|
||||
>Back to {{.Fullname}}</a
|
||||
>
|
||||
</div>
|
||||
</div>
|
||||
{{ end }} {{ if .readme}}
|
||||
<div class="user-readme">
|
||||
{{ if .dir }} {{ range $key, $value := .dir }}
|
||||
<h3>{{.Readme}}</h3>
|
||||
{{end}} {{end}}
|
||||
<div class="user-readme-text">{{ unescape .readme}}</div>
|
||||
</div>
|
||||
{{ end }} {{ else }}
|
||||
<h2>Directory not found</h2>
|
||||
<p>That directory doesn't exist.</p>
|
||||
{{ end }}
|
||||
|
||||
<div class="user-profile">
|
||||
<h1>{{.Fullname}} | {{.DirName}}</h1>
|
||||
</div>
|
||||
{{end}}
|
||||
|
||||
{{ if .TruncationWarning }}
|
||||
<blockquote>{{.TruncationWarning}}</blockquote>
|
||||
{{ end }}
|
||||
|
||||
{{ if .files}}
|
||||
<div class="user-readme">
|
||||
<h3>Files</h3>
|
||||
<div class="user-readme-text">
|
||||
<ul class="file-u-list">
|
||||
{{ range $key, $value := .files}} {{ if eq .Type "dir" }}
|
||||
<li class="file-list">
|
||||
📁
|
||||
<a
|
||||
href="/{{.Fullname}}/tree/{{.Branch}}/{{.Path}}"
|
||||
class="file-a"
|
||||
>{{.Name}}</a
|
||||
>
|
||||
</li>
|
||||
{{ else }}
|
||||
<li class="file-list">
|
||||
🗒️
|
||||
<a href="/{{.Fullname}}/blob/{{.Branch}}/{{.Path}}"
|
||||
>{{.Name}}</a
|
||||
>
|
||||
</li>
|
||||
{{ end }} {{ end }}
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
{{ end }} {{ if .readme}}
|
||||
<div class="user-readme">
|
||||
{{ if .dir }} {{ range $key, $value := .dir }}
|
||||
<h3>{{.Readme}}</h3>
|
||||
{{end}} {{end}}
|
||||
<div class="user-readme-text">{{ unescape .readme}}</div>
|
||||
</div>
|
||||
{{ end }} {{ else }}
|
||||
<h2>Directory not found</h2>
|
||||
<p>That directory doesn't exist.</p>
|
||||
{{ end }}
|
||||
</main>
|
||||
{{ template "footer" .}}
|
||||
|
||||
Reference in New Issue
Block a user