mirror of
https://codeberg.org/gothub/gothub
synced 2024-12-06 19:16:24 +01:00
97 lines
2.4 KiB
Go
97 lines
2.4 KiB
Go
package pages
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/tidwall/gjson"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
|
|
"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
|
|
}
|
|
|
|
type DirFiles struct {
|
|
Name string
|
|
Path string
|
|
Type string
|
|
Branch string
|
|
Fullname string
|
|
}
|
|
|
|
func DirView(c *fiber.Ctx) error {
|
|
var dirArray []Dir
|
|
var dirFilesArray []DirFiles
|
|
var readmeOutput string
|
|
|
|
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.OnResponse(func(r *colly.Response) {
|
|
jsonMess := string(r.Body)
|
|
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
|
|
})
|
|
|
|
})
|
|
|
|
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)
|
|
return c.Render("dir", fiber.Map{
|
|
"title": "Directory " + c.Params("+") + " | " + c.Params("user") + "/" + c.Params("repo"),
|
|
"branch": utils.Branch,
|
|
"dir": dirArray,
|
|
"files": dirFilesArray,
|
|
"readme": string(readmeOutput),
|
|
})
|
|
}
|