Add support for viewing directories

このコミットが含まれているのは:
Arya Kiran 2023-03-16 14:58:53 +05:30
コミット b0318e1c75
この署名に対応する既知のキーがデータベースに存在しません
GPGキーID: 842D12BDA50DF120
5個のファイルの変更151行の追加1行の削除

104
pages/dirview.go ノーマルファイル
ファイルの表示

@ -0,0 +1,104 @@
package pages
import (
"codeberg.org/gothub/gothub/utils"
"context"
"github.com/carlmjohnson/requests"
"github.com/gocolly/colly"
"github.com/gofiber/fiber/v2"
"github.com/gomarkdown/markdown"
"log"
"net/http"
"os"
)
type Dir struct {
Readme string
Fullname string
DirName string
Branch string
}
type DirFiles struct {
Name string
Path string
Type string
Branch string
Fullname string
DirName string
}
func DirView(c *fiber.Ctx) error {
var dirArray []Dir
var dirFilesArray []DirFiles
resp, statusErr := http.Get("https://github.com/" + c.Params("user") + "/" + c.Params("repo") + "/tree/" + c.Params("branch") + "/" + c.Params("+"))
if statusErr != nil {
log.Println(statusErr)
}
if resp.StatusCode == 404 {
// I need a better way to do this
return c.Status(404).Render("error", fiber.Map{
"title": "Error",
"error": "Directory" + c.Params("+") + "not found",
})
}
// Scraping
Scrape := Dir{}
Scrape.Fullname = c.Params("user") + "/" + c.Params("repo")
Scrape.DirName = c.Params("+")
Scrape.Branch = c.Params("branch")
UserAgent, ok := os.LookupEnv("GOTHUB_USER_AGENT")
if !ok {
UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
}
sc := colly.NewCollector(colly.AllowedDomains("github.com"), colly.UserAgent(UserAgent))
sc.OnHTML("div#readme", func(e *colly.HTMLElement) {
Scrape.Readme = e.ChildText("a[href*='#readme']")
})
sc.OnHTML("div.js-details-container div.Details-content--hidden-not-important", func(e *colly.HTMLElement) {
e.ForEach("div.js-navigation-item", func(i int, el *colly.HTMLElement) {
var FileType string
if el.ChildAttr("div.flex-shrink-0 svg", "aria-label") == "Directory" {
FileType = "dir"
} else {
FileType = "file"
}
dirFilesArray = append(dirFilesArray, DirFiles{
Name: el.ChildText("div.flex-auto span.d-block a.js-navigation-open"),
Path: el.ChildText("div.flex-auto span.d-block a.js-navigation-open"),
Type: FileType,
Fullname: Scrape.Fullname,
DirName: Scrape.DirName,
Branch: Scrape.Branch,
})
})
})
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)
// README
var readmee string
err := requests.
URL("https://raw.githubusercontent.com/" + c.Params("user") + "/" + c.Params("repo") + "/" + c.Params("branch") + "/" + c.Params("+") + "/" + Scrape.Readme).
ToString(&readmee).
Fetch(context.Background())
if err != nil {
readmee = ""
log.Println(err)
}
mightBeUnsafe := markdown.ToHTML([]byte(readmee), nil, nil)
// Trust Nobody
readmeOutput := utils.UGCPolicy().SanitizeBytes(mightBeUnsafe)
return c.Render("dir", fiber.Map{
"title": "Directory " + c.Params("+") + " | " + c.Params("user") + "/" + c.Params("repo"),
"dir": dirArray,
"files": dirFilesArray,
"readme": string(readmeOutput),
})
}

ファイルの表示

@ -56,6 +56,7 @@ func FileView(c *fiber.Ctx) error {
writer.Flush()
cssw.Flush()
return c.Render("file", fiber.Map{
"title": "File " + c.Params("+") + " | " + c.Params("user") + "/" + c.Params("repo"),
"file": template.HTML(buf.String()),
"css": template.CSS(cssbuf.String()),
"fullname": c.Params("user") + "/" + c.Params("repo"),

ファイルの表示

@ -107,6 +107,7 @@ func Serve() {
})
app.Get("/:user/:repo", pages.HandleRepo)
app.Get("/:user/:repo/blob/:branch/+", pages.FileView)
app.Get("/:user/:repo/tree/:branch/+", pages.DirView)
app.Get("/download/:user/:repo/:branch", func(c *fiber.Ctx) error {
utils.ProxyRequest(c, "https://github.com/"+c.Params("user")+"/"+c.Params("repo")+"/archive/"+c.Params("branch")+".zip")
return nil

44
views/dir.html ノーマルファイル
ファイルの表示

@ -0,0 +1,44 @@
{{ template "header" .}}
<main>
{{ if .dir }} {{ range $key, $value := .dir}}
<div class="buttonParent">
<a href="https://github.com/{{.Fullname}}/tree/{{.DirName}}" class="button">View on GitHub</a>
<a href="/{{.Fullname}}" class="button">Back to {{.Fullname}}</a>
</div>
<div class="userProfile">
<h1>{{.Fullname}} | {{.DirName}}</h1>
</div>
{{end}} {{ if .files}}
<div class="userReadme">
<h3>Files</h3>
<div class="userReadmeText">
<ul class="filesUList">
{{ range $key, $value := .files}} {{ if eq .Type "dir" }}
<li class="filesList">
<a href="/{{.Fullname}}/tree/{{.Branch}}/{{.DirName}}/{{.Path}}" class="filesA">{{.Path}} (directory)</a>
</li>
{{ else }}
<li class="filesList">
<a href="/{{.Fullname}}/blob/{{.Branch}}/{{.DirName}}/{{.Path}}"
>{{.Path}}</a
>
</li>
{{ end }} {{ end }}
</ul>
</div>
</div>
{{ end }} {{ if .readme}}
<div class="userReadme">
{{ if .dir }} {{ range $key, $value := .dir }}
<h3>{{.Readme}}</h3>
{{end}} {{end}}
<div class="userReadmeText">{{ unescape .readme}}</div>
</div>
{{ end }} {{ else }}
<h2>Directory not found</h2>
<p>That directory doesn't exist.</p>
{{ end }}
</main>
{{ template "footer" .}}

ファイルの表示

@ -36,7 +36,7 @@
<ul class="filesUList">
{{ range $key, $value := .files}} {{ if eq .Type "dir" }}
<li class="filesList">
<a href="#" class="filesA">{{.Path}} (directory)</a>
<a href="/{{.Fullname}}/tree/{{.DefaultBranch}}/{{.Path}}" class="filesA">{{.Path}} (directory)</a>
</li>
{{ else }}
<li class="filesList">