From 62c2481bd31f822eb65b41425da5b2a34c05e7b5 Mon Sep 17 00:00:00 2001 From: Andreas Mieke Date: Sun, 14 Feb 2016 01:26:46 +0100 Subject: [PATCH] Adding admin interface Also changing year in copyright and fixing search --- gserver/admin.go | 286 +++++++++++++++++++++++++++++ gserver/main.go | 20 ++ gserver/templates/admin_at.html | 34 ++++ gserver/templates/admin_ats.html | 25 +++ gserver/templates/admin_ep.html | 52 ++++++ gserver/templates/admin_eps.html | 29 +++ gserver/templates/admin_index.html | 13 ++ gserver/templates/admin_lp.html | 31 ++++ gserver/templates/admin_lps.html | 27 +++ gserver/templates/admin_lt.html | 52 ++++++ gserver/templates/admin_lts.html | 27 +++ gserver/templates/footer.html | 4 +- image/http.go | 7 +- 13 files changed, 602 insertions(+), 5 deletions(-) create mode 100644 gserver/admin.go create mode 100644 gserver/templates/admin_at.html create mode 100644 gserver/templates/admin_ats.html create mode 100644 gserver/templates/admin_ep.html create mode 100644 gserver/templates/admin_eps.html create mode 100644 gserver/templates/admin_index.html create mode 100644 gserver/templates/admin_lp.html create mode 100644 gserver/templates/admin_lps.html create mode 100644 gserver/templates/admin_lt.html create mode 100644 gserver/templates/admin_lts.html diff --git a/gserver/admin.go b/gserver/admin.go new file mode 100644 index 0000000..f56d1ba --- /dev/null +++ b/gserver/admin.go @@ -0,0 +1,286 @@ +package main + +import ( + //"html/template" + "strings" + "strconv" + "time" + + . "git.1750studios.com/gronkhDE/gogronkh/gserver/utlis" + + "git.1750studios.com/gronkhDE/gogronkh/database" + "git.1750studios.com/gronkhDE/gogronkh/image" + + "github.com/gin-gonic/gin" +) + +func GetAdminIndex(c *gin.Context) { + var LPs int + var LTs int + var EPs int + var ATs int + database.Db.Model(database.LetsPlay{}).Count(&LPs) + database.Db.Model(database.LetsTest{}).Count(<s) + database.Db.Model(database.Episode{}).Count(&EPs) + database.Db.Model(database.Author{}).Count(&ATs) + + c.HTML(200, "admin_index.html", gin.H{ + "title": "Admin Index", + "LPs": LPs, + "LTs": LTs, + "EPs": EPs, + "ATs": ATs, + "page": "admin_index", + }) +} + +func GetAdminLetsPlay(c *gin.Context) { + if id, ok := CleanParam(c.Param("id")); ok { + //LP in URL + var LP database.LetsPlay + if database.Db.Where(id).First(&LP).Error == nil { + //LP found + c.HTML(200, "admin_lp.html", gin.H{ + "title": "Admin Let's Plays", + "LP": LP, + "page": "admin_lp", + }) + } else { + //LP not found + c.AbortWithStatus(404) + } + } else { + var LPs []database.LetsPlay + database.Db.Order("id asc").Find(&LPs) + c.HTML(200, "admin_lps.html", gin.H{ + "title": "Admin Let's Plays", + "LPs": LPs, + "page": "admin_lps", + }) + } +} + +func PostAdminLetsPlay(c *gin.Context) { + if id, ok := CleanParam(c.Param("id")); ok { + var LP database.LetsPlay + database.Db.Where(id).First(&LP) + if id, err := strconv.ParseUint(c.PostForm("id"), 10, 0); err == nil { + LP.ID = uint(id) + } + if id, err := strconv.ParseUint(c.PostForm("authorid"), 10, 0); err == nil { + LP.AuthorID = uint(id) + } + LP.Slug.String = c.PostForm("slug") + LP.Name.String = c.PostForm("name") + if c.PostForm("posters") == "" && !strings.HasPrefix(c.PostForm("posterb"), "/") { + LP.PosterS.String, LP.PosterB.String, _ = image.ResizeCover(c.PostForm("posterb")) + } else { + LP.PosterS.String = c.PostForm("posters") + LP.PosterB.String = c.PostForm("posterb") + } + if t, err := time.Parse("2006-01-02 15:04:05 -0700 MST", c.PostForm("aired")); err == nil { + LP.Aired = t + } + database.Db.Save(&LP); + c.Redirect(301, "/admin/lets-play#" + strconv.FormatUint(uint64(LP.ID), 10)) + } else { + c.Redirect(301, "/admin/lets-play") + } +} + +func GetAdminLetsTest(c *gin.Context) { + if id, ok := CleanParam(c.Param("id")); ok { + //LT in URL + var LT database.LetsTest + if database.Db.Where(id).First(<).Error == nil { + //LT found + c.HTML(200, "admin_lt.html", gin.H{ + "title": "Admin Let's Tests", + "LT": LT, + "page": "admin_lt", + }) + } else { + //LT not found + c.AbortWithStatus(404) + } + } else { + var LTs []database.LetsTest + database.Db.Order("id asc").Find(<s) + c.HTML(200, "admin_lts.html", gin.H{ + "title": "Admin Let's Tests", + "LTs": LTs, + "page": "admin_lts", + }) + } +} + +func PostAdminLetsTest(c *gin.Context) { + if id, ok := CleanParam(c.Param("id")); ok { + var LT database.LetsTest + database.Db.Where(id).First(<) + if id, err := strconv.ParseUint(c.PostForm("id"), 10, 0); err == nil { + LT.ID = uint(id) + } + if id, err := strconv.ParseUint(c.PostForm("authorid"), 10, 0); err == nil { + LT.AuthorID = uint(id) + } + LT.Slug.String = c.PostForm("slug") + LT.Name.String = c.PostForm("name") + if c.PostForm("posters") == "" && !strings.HasPrefix(c.PostForm("posterb"), "/") { + LT.PosterS.String, LT.PosterB.String, _ = image.ResizeCover(c.PostForm("posterb")) + } else { + LT.PosterS.String = c.PostForm("posters") + LT.PosterB.String = c.PostForm("posterb") + } + if c.PostForm("thumbs") == "" && !strings.HasPrefix(c.PostForm("thumbb"), "/") { + LT.ThumbS.String, LT.ThumbB.String, _ = image.ResizeThumb(c.PostForm("thumbb")) + } else { + LT.ThumbS.String = c.PostForm("thumbs") + LT.ThumbB.String = c.PostForm("thumbb") + } + LT.Youtube.String = c.PostForm("youtube") + LT.Descr.String = c.PostForm("descr") + if t, err := time.Parse("2006-01-02 15:04:05 -0700 MST", c.PostForm("aired")); err == nil { + LT.Aired = t + } + if ra, err := strconv.ParseFloat(c.PostForm("authorid"), 64); err == nil { + LT.Rating.Float64 = ra + } + if vo, err := strconv.ParseInt(c.PostForm("votes"), 10, 64); err == nil { + LT.Votes.Int64 = vo + } + if du, err := strconv.ParseInt(c.PostForm("duration"), 10, 64); err == nil { + LT.Duration.Int64 = du + } + database.Db.Save(<); + c.Redirect(301, "/admin/lets-test#" + strconv.FormatUint(uint64(LT.ID), 10)) + } else { + c.Redirect(301, "/admin/lets-test") + } +} + +func GetAdminEpisode(c *gin.Context) { + if id, ok := CleanParam(c.Param("id")); ok { + //EP in URL + var EP database.Episode + if database.Db.Where(id).First(&EP).Error == nil { + //EP found + c.HTML(200, "admin_ep.html", gin.H{ + "title": "Admin Episoden", + "EP": EP, + "page": "admin_ep", + }) + } else { + //EP not found + c.AbortWithStatus(404) + } + } else { + var EPs []database.Episode + database.Db.Order("id asc").Find(&EPs) + c.HTML(200, "admin_eps.html", gin.H{ + "title": "Admin Episoden", + "EPs": EPs, + "page": "admin_eps", + }) + } +} + +func PostAdminEpisode(c *gin.Context) { + if id, ok := CleanParam(c.Param("id")); ok { + var EP database.Episode + database.Db.Where(id).First(&EP) + if id, err := strconv.ParseUint(c.PostForm("id"), 10, 0); err == nil { + EP.ID = uint(id) + } + if id, err := strconv.ParseUint(c.PostForm("authorid"), 10, 0); err == nil { + EP.AuthorID = uint(id) + } + if id, err := strconv.ParseUint(c.PostForm("letsplayid"), 10, 0); err == nil { + EP.LetsPlayID = uint(id) + } + EP.Slug.String = c.PostForm("slug") + EP.Name.String = c.PostForm("name") + if ep, err := strconv.ParseInt(c.PostForm("episode"), 10, 64); err == nil { + EP.Episode.Int64 = ep + } + if c.PostForm("thumbs") == "" && !strings.HasPrefix(c.PostForm("thumbb"), "/") { + EP.ThumbS.String, EP.ThumbB.String, _ = image.ResizeThumb(c.PostForm("thumbb")) + } else { + EP.ThumbS.String = c.PostForm("thumbs") + EP.ThumbB.String = c.PostForm("thumbb") + } + EP.Youtube.String = c.PostForm("youtube") + EP.Descr.String = c.PostForm("descr") + if t, err := time.Parse("2006-01-02 15:04:05 -0700 MST", c.PostForm("aired")); err == nil { + EP.Aired = t + } + if ra, err := strconv.ParseFloat(c.PostForm("authorid"), 64); err == nil { + EP.Rating.Float64 = ra + } + if vo, err := strconv.ParseInt(c.PostForm("votes"), 10, 64); err == nil { + EP.Votes.Int64 = vo + } + if du, err := strconv.ParseInt(c.PostForm("duration"), 10, 64); err == nil { + EP.Duration.Int64 = du + } + database.Db.Save(&EP); + c.Redirect(301, "/admin/episode#" + strconv.FormatUint(uint64(EP.ID), 10)) + } else { + c.Redirect(301, "/admin/episode") + } +} + +func GetAdminAuthor(c *gin.Context) { + if id, ok := CleanParam(c.Param("id")); ok { + //AT in URL + var AT database.Author + if database.Db.Where(id).First(&AT).Error == nil { + //AT found + c.HTML(200, "admin_at.html", gin.H{ + "title": "Admin Sprecher", + "AT": AT, + "page": "admin_at", + }) + } else { + //AT not found + c.AbortWithStatus(404) + } + } else { + var ATs []database.Author + database.Db.Order("id asc").Find(&ATs) + c.HTML(200, "admin_ats.html", gin.H{ + "title": "Admin Sprecher", + "ATs": ATs, + "page": "admin_ats", + }) + } +} + +func PostAdminAuthor(c *gin.Context) { + if id, ok := CleanParam(c.Param("id")); ok { + var AT database.Author + database.Db.Where(id).First(&AT) + if id, err := strconv.ParseUint(c.PostForm("id"), 10, 0); err == nil { + AT.ID = uint(id) + } + AT.Slug.String = c.PostForm("slug") + AT.Name.String = c.PostForm("name") + AT.Youtube.String = c.PostForm("youtube") + if c.PostForm("avatars") == "" && !strings.HasPrefix(c.PostForm("avatarb"), "/") { + AT.AvatarS.String, AT.AvatarB.String, _ = image.ResizeAvatar(c.PostForm("avatarb")) + } else { + AT.AvatarS.String = c.PostForm("avatars") + AT.AvatarB.String = c.PostForm("avatarb") + } + if c.PostForm("fanarts") == "" && !strings.HasPrefix(c.PostForm("fanartb"), "/") { + AT.FanArtS.String, AT.FanArtB.String, _ = image.ResizeFanArt(c.PostForm("fanartb")) + } else { + AT.FanArtS.String = c.PostForm("fanarts") + AT.FanArtB.String = c.PostForm("fanartb") + } + database.Db.Save(&AT); + c.Redirect(301, "/admin/sprecher#" + strconv.FormatUint(uint64(AT.ID), 10)) + } else { + c.Redirect(301, "/admin/sprecher") + } +} diff --git a/gserver/main.go b/gserver/main.go index 804ee69..73e9536 100644 --- a/gserver/main.go +++ b/gserver/main.go @@ -40,6 +40,25 @@ func initRouter() *gin.Engine { router.GET("/zeige/:aslug/testet", GetAtLts) router.GET("/zeige/:aslug/episoden", GetAtEps) + admin := router.Group("/admin") + + admin.GET("/", GetAdminIndex) + + admin.GET("/lets-play", GetAdminLetsPlay) + admin.GET("/lets-play/:id", GetAdminLetsPlay) + admin.POST("/lets-play/:id", PostAdminLetsPlay) + + admin.GET("/lets-test", GetAdminLetsTest) + admin.GET("/lets-test/:id", GetAdminLetsTest) + admin.POST("/lets-test/:id", PostAdminLetsTest) + + admin.GET("/episode", GetAdminEpisode) + admin.GET("/episode/:id", GetAdminEpisode) + admin.POST("/episode/:id", PostAdminEpisode) + + admin.GET("/sprecher", GetAdminAuthor) + admin.GET("/sprecher/:id", GetAdminAuthor) + admin.POST("/sprecher/:id", PostAdminAuthor) // API api := router.Group("/api") @@ -71,6 +90,7 @@ func initRouter() *gin.Engine { v3.GET("/recent/:limit", apiv3.GetRecent) v3.GET("/search/:type/:query", apiv3.GetSearch) } + return router } diff --git a/gserver/templates/admin_at.html b/gserver/templates/admin_at.html new file mode 100644 index 0000000..56c3f72 --- /dev/null +++ b/gserver/templates/admin_at.html @@ -0,0 +1,34 @@ +{{ template "header.html" . }} +
+
+

{{ .title }} #{{ .AT.ID }}

+
+ + + + + + + + + +
+
+
+{{ template "footer.html" . }} \ No newline at end of file diff --git a/gserver/templates/admin_ats.html b/gserver/templates/admin_ats.html new file mode 100644 index 0000000..6ffcdfd --- /dev/null +++ b/gserver/templates/admin_ats.html @@ -0,0 +1,25 @@ +{{ template "header.html" . }} +
+
+

{{ .title }}

+ + + + + + + + + + {{ range .ATs }} + + + + + + {{ end }} + +
#NameSlug
{{ .ID }}{{ .Name.String }}{{ .Slug.String }}
+
+
+{{ template "footer.html" . }} \ No newline at end of file diff --git a/gserver/templates/admin_ep.html b/gserver/templates/admin_ep.html new file mode 100644 index 0000000..e390289 --- /dev/null +++ b/gserver/templates/admin_ep.html @@ -0,0 +1,52 @@ +{{ template "header.html" . }} +
+
+

{{ .title }} #{{ .EP.ID }}

+
+ + + + + + + + + + + + + + + +
+
+
+{{ template "footer.html" . }} \ No newline at end of file diff --git a/gserver/templates/admin_eps.html b/gserver/templates/admin_eps.html new file mode 100644 index 0000000..d45f39c --- /dev/null +++ b/gserver/templates/admin_eps.html @@ -0,0 +1,29 @@ +{{ template "header.html" . }} +
+
+

{{ .title }}

+ + + + + + + + + + + + {{ range .EPs }} + + + + + + + + {{ end }} + +
#AT #LP #NameSlug
{{ .ID }}{{ .AuthorID }}{{ .LetsPlayID }}{{ .Name.String }}{{ .Slug.String }}
+
+
+{{ template "footer.html" . }} \ No newline at end of file diff --git a/gserver/templates/admin_index.html b/gserver/templates/admin_index.html new file mode 100644 index 0000000..989fab3 --- /dev/null +++ b/gserver/templates/admin_index.html @@ -0,0 +1,13 @@ +{{ template "header.html" . }} +
+
+

{{ .title }}

+ +
+
+{{ template "footer.html" . }} diff --git a/gserver/templates/admin_lp.html b/gserver/templates/admin_lp.html new file mode 100644 index 0000000..6ecbf45 --- /dev/null +++ b/gserver/templates/admin_lp.html @@ -0,0 +1,31 @@ +{{ template "header.html" . }} +
+
+

{{ .title }} #{{ .LP.ID }}

+
+ + + + + + + + +
+
+
+{{ template "footer.html" . }} \ No newline at end of file diff --git a/gserver/templates/admin_lps.html b/gserver/templates/admin_lps.html new file mode 100644 index 0000000..23d3a2c --- /dev/null +++ b/gserver/templates/admin_lps.html @@ -0,0 +1,27 @@ +{{ template "header.html" . }} +
+
+

{{ .title }}

+ + + + + + + + + + + {{ range .LPs }} + + + + + + + {{ end }} + +
#AT #NameSlug
{{ .ID }}{{ .AuthorID }}{{ .Name.String }}{{ .Slug.String }}
+
+
+{{ template "footer.html" . }} \ No newline at end of file diff --git a/gserver/templates/admin_lt.html b/gserver/templates/admin_lt.html new file mode 100644 index 0000000..d76bec9 --- /dev/null +++ b/gserver/templates/admin_lt.html @@ -0,0 +1,52 @@ +{{ template "header.html" . }} +
+
+

{{ .title }} #{{ .LT.ID }}

+
+ + + + + + + + + + + + + + + +
+
+
+{{ template "footer.html" . }} \ No newline at end of file diff --git a/gserver/templates/admin_lts.html b/gserver/templates/admin_lts.html new file mode 100644 index 0000000..57b456b --- /dev/null +++ b/gserver/templates/admin_lts.html @@ -0,0 +1,27 @@ +{{ template "header.html" . }} +
+
+

{{ .title }}

+ + + + + + + + + + + {{ range .LTs }} + + + + + + + {{ end }} + +
#AT #NameSlug
{{ .ID }}{{ .AuthorID }}{{ .Name.String }}{{ .Slug.String }}
+
+
+{{ template "footer.html" . }} \ No newline at end of file diff --git a/gserver/templates/footer.html b/gserver/templates/footer.html index ecd1cb6..53392b8 100644 --- a/gserver/templates/footer.html +++ b/gserver/templates/footer.html @@ -13,7 +13,7 @@

Gronkh.de in Kodi

-

Copyright © 2010 - 2015
gronkh.de — Fair use

+

Copyright © 2010 - 2016
gronkh.de — Fair use

@@ -55,7 +55,7 @@ dropd.empty(); $.each(data, function (index, value) { li = $(document.createElement("li")); - li.html("" + value.name + ""); + li.html("" + value.name + ""); dropd.append(li); }); }); diff --git a/image/http.go b/image/http.go index fbaf9f2..2d10de0 100644 --- a/image/http.go +++ b/image/http.go @@ -5,6 +5,7 @@ import ( "net/http" "strconv" "time" + "errors" ) // Make an HTTP Get Request to u @@ -19,7 +20,7 @@ func GetHTTPResource(u string) (*http.Response, error) { } req, err := http.NewRequest("GET", u, nil) req.AddCookie(cookie) - req.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") + req.Header.Add("Accept", "*/*") req.Header.Add("Cache-Control", "max-age=0") req.Header.Add("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/600.7.12 (KHTML, like Gecko) Version/8.0.7 Safari/600.7.12") if err != nil { @@ -31,8 +32,8 @@ func GetHTTPResource(u string) (*http.Response, error) { if err != nil { return nil, err } - if res.StatusCode == http.StatusNotFound { - return nil, nil + if res.StatusCode != 200 { + return nil, errors.New(strconv.Itoa(res.StatusCode)) } return res, nil }