add image support (with proxying) (closes #38)

このコミットが含まれているのは:
Arya Kiran 2023-04-28 15:49:33 +05:30
コミット 93b5a7b08b
この署名に対応する既知のキーがデータベースに存在しません
GPGキーID: 842D12BDA50DF120
5個のファイルの変更15行の追加3行の削除

ファイルの表示

@ -61,7 +61,7 @@ func DirView(c *fiber.Ctx) error {
}) })
sc.OnHTML("div#readme div.Box-body", func(e *colly.HTMLElement) { sc.OnHTML("div#readme div.Box-body", func(e *colly.HTMLElement) {
Content, _ := e.DOM.Html() Content, _ := e.DOM.Html()
readmeOutput = strings.Replace(strings.Replace(string(utils.UGCPolicy().SanitizeBytes([]byte(Content))), "https://github.com", "", -1), "user-content-", "", -1) readmeOutput = strings.Replace(strings.Replace(strings.Replace(strings.Replace(string(utils.UGCPolicy().SanitizeBytes([]byte(Content))), "https://github.com", "", -1), "user-content-", "", -1), "https://camo.githubusercontent.com", "/camo", -1), "https://raw.githubusercontent.com", "/raw", -1)
}) })
sc.OnHTML("div.js-details-container div.Details-content--hidden-not-important", func(e *colly.HTMLElement) { 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) { e.ForEach("div.js-navigation-item", func(i int, el *colly.HTMLElement) {

ファイルの表示

@ -86,7 +86,7 @@ func HandleRepo(c *fiber.Ctx) error {
}) })
sc.OnHTML("div#readme div.Box-body", func(e *colly.HTMLElement) { sc.OnHTML("div#readme div.Box-body", func(e *colly.HTMLElement) {
Content, _ := e.DOM.Html() Content, _ := e.DOM.Html()
readmeOutput = strings.Replace(strings.Replace(string(utils.UGCPolicy().SanitizeBytes([]byte(Content))), "https://github.com", "", -1), "user-content-", "", -1) readmeOutput = strings.Replace(strings.Replace(strings.Replace(strings.Replace(string(utils.UGCPolicy().SanitizeBytes([]byte(Content))), "https://github.com", "", -1), "user-content-", "", -1), "https://camo.githubusercontent.com", "/camo", -1), "https://raw.githubusercontent.com", "/raw", -1)
}) })
sc.OnHTML("div.BorderGrid-cell ul.list-style-none", func(e *colly.HTMLElement) { sc.OnHTML("div.BorderGrid-cell ul.list-style-none", func(e *colly.HTMLElement) {
e.ForEach("li.d-inline .d-inline-flex", func(i int, el *colly.HTMLElement) { e.ForEach("li.d-inline .d-inline-flex", func(i int, el *colly.HTMLElement) {

ファイルの表示

@ -157,7 +157,7 @@ func HandleUser(c *fiber.Ctx) error {
}) })
sc.OnHTML("article.markdown-body", func(e *colly.HTMLElement) { sc.OnHTML("article.markdown-body", func(e *colly.HTMLElement) {
Content, _ := e.DOM.Html() Content, _ := e.DOM.Html()
Scrape.Readme = strings.Replace(string(utils.UGCPolicy().SanitizeBytes([]byte(Content))), "https://github.com", "", -1) Scrape.Readme = strings.Replace(strings.Replace(strings.Replace(strings.Replace(string(utils.UGCPolicy().SanitizeBytes([]byte(Content))), "https://github.com", "", -1), "user-content-", "", -1), "https://camo.githubusercontent.com", "/camo", -1), "https://raw.githubusercontent.com", "/raw", -1)
}) })
sc.OnHTML("div.text-mono", func(e *colly.HTMLElement) { sc.OnHTML("div.text-mono", func(e *colly.HTMLElement) {
Scrape.ReadmeUrl = strings.Replace(e.ChildAttr("a", "href"), "tree", "blob", -1) Scrape.ReadmeUrl = strings.Replace(e.ChildAttr("a", "href"), "tree", "blob", -1)

ファイルの表示

@ -116,6 +116,10 @@ func Serve(port string) {
utils.ProxyRequest(c, "https://github.com/"+c.Params("user")+"/"+c.Params("repo")+"/archive/"+c.Params("branch")+".zip") utils.ProxyRequest(c, "https://github.com/"+c.Params("user")+"/"+c.Params("repo")+"/archive/"+c.Params("branch")+".zip")
return nil return nil
}) })
app.Get("/camo/:p1/:p2", func(c *fiber.Ctx) error {
utils.ProxyRequest(c, "https://camo.githubusercontent.com/"+c.Params("p1")+"/"+c.Params("p2"))
return nil
})
app.Get("/gist/:user/:gistID", pages.HandleGist) app.Get("/gist/:user/:gistID", pages.HandleGist)
app.Get("/download/gist/:user/:gistID/:revision", func(c *fiber.Ctx) error { app.Get("/download/gist/:user/:gistID/:revision", func(c *fiber.Ctx) error {
utils.ProxyRequest(c, "https://gist.github.com/"+c.Params("user")+"/"+c.Params("gistID")+"/archive/"+c.Params("revision")+".zip") utils.ProxyRequest(c, "https://gist.github.com/"+c.Params("user")+"/"+c.Params("gistID")+"/archive/"+c.Params("revision")+".zip")

ファイルの表示

@ -110,9 +110,17 @@ func UGCPolicy() *bluemonday.Policy {
p.AllowAttrs("shape").Matching( p.AllowAttrs("shape").Matching(
regexp.MustCompile(`(?i)^(default|circle|rect|poly)$`), regexp.MustCompile(`(?i)^(default|circle|rect|poly)$`),
).OnElements("area") ).OnElements("area")
// "img" is permitted
p.AllowImages()
p.AllowAttrs("usemap").Matching( p.AllowAttrs("usemap").Matching(
regexp.MustCompile(`(?i)^#[\p{L}\p{N}_-]+$`), regexp.MustCompile(`(?i)^#[\p{L}\p{N}_-]+$`),
).OnElements("img") ).OnElements("img")
p.AllowAttrs("src").Matching(
regexp.MustCompile(`(?i)^\/[\p{L}\p{N}\/\.]+$`),
).OnElements("img")
p.AllowAttrs("style").OnElements("img")
p.AllowAttrs("alt").OnElements("img")
// "link" is not permitted // "link" is not permitted