From 4572499658986e9f9f27b284f0ab429fde7d3952 Mon Sep 17 00:00:00 2001 From: video-prize-ranch Date: Tue, 22 Feb 2022 19:06:39 -0500 Subject: [PATCH] Add support for .gifv (closes #13) and fixes loading media IDs --- api/album.go | 16 ++++++++++++++++ main.go | 3 ++- pages/embed.go | 16 ++++++++++++++++ pages/frontpage.go | 2 +- pages/post.go | 13 ++++++++++++- static/css/embed.css | 13 +++++++++++++ views/embed.hbs | 26 ++++++++++++++++++++++++++ 7 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 pages/embed.go create mode 100644 static/css/embed.css create mode 100644 views/embed.hbs diff --git a/api/album.go b/api/album.go index 730476b..31c93a6 100644 --- a/api/album.go +++ b/api/album.go @@ -52,6 +52,22 @@ func FetchPosts(albumID string) (types.Album, error) { return ParseAlbum(data) } +func FetchMedia(mediaID string) (types.Album, error) { + res, err := http.Get("https://api.imgur.com/post/v1/media/" + mediaID + "?client_id=" + viper.GetString("RIMGU_IMGUR_CLIENT_ID") + "&include=media%2Caccount") + if err != nil { + return types.Album{}, err + } + + body, err := io.ReadAll(res.Body) + if err != nil { + return types.Album{}, err + } + + data := gjson.Parse(string(body)) + + return ParseAlbum(data) +} + func ParseAlbum(data gjson.Result) (types.Album, error) { media := make([]types.Media, 0) data.Get("media").ForEach( diff --git a/main.go b/main.go index 84df114..6055bed 100644 --- a/main.go +++ b/main.go @@ -49,7 +49,8 @@ func main() { return err }) - app.Get("/", pages.FrontpageHandler) + app.Get("/", pages.HandleFrontpage) + app.Get("/:postID.gifv", pages.HandleEmbed) app.Get("/:baseName.:extension", pages.HandleMedia) app.Get("/:postID", pages.HandlePost) app.Get("/a/:galleryID", pages.HandleGallery) diff --git a/pages/embed.go b/pages/embed.go new file mode 100644 index 0000000..d425781 --- /dev/null +++ b/pages/embed.go @@ -0,0 +1,16 @@ +package pages + +import ( + "codeberg.org/video-prize-ranch/rimgo/utils" + "github.com/gofiber/fiber/v2" +) + +func HandleEmbed(c *fiber.Ctx) error { + utils.SetHeaders(c) + c.Set("Cache-Control", "public,max-age=31557600") + c.Set("Content-Security-Policy", "default-src 'none'; media-src 'self'; style-src 'self'; img-src 'self'; font-src 'self'; block-all-mixed-content") + + return c.Render("embed", fiber.Map{ + "id": c.Params("postID"), + }) +} \ No newline at end of file diff --git a/pages/frontpage.go b/pages/frontpage.go index 66fcbb4..b2d97a4 100644 --- a/pages/frontpage.go +++ b/pages/frontpage.go @@ -5,7 +5,7 @@ import ( "github.com/gofiber/fiber/v2" ) -func FrontpageHandler(c *fiber.Ctx) error { +func HandleFrontpage(c *fiber.Ctx) error { utils.SetHeaders(c) c.Set("Cache-Control", "public,max-age=31557600") c.Set("Content-Security-Policy", "default-src 'none'; style-src 'self'; img-src 'self'; font-src 'self'; block-all-mixed-content") diff --git a/pages/post.go b/pages/post.go index 088fd03..52d5c8f 100644 --- a/pages/post.go +++ b/pages/post.go @@ -1,6 +1,8 @@ package pages import ( + "fmt" + "codeberg.org/video-prize-ranch/rimgo/api" "codeberg.org/video-prize-ranch/rimgo/types" "codeberg.org/video-prize-ranch/rimgo/utils" @@ -12,7 +14,16 @@ func HandlePost(c *fiber.Ctx) error { c.Set("Content-Security-Policy", "default-src 'none'; media-src 'self'; style-src 'self'; img-src 'self'; font-src 'self'; block-all-mixed-content") post, err := api.FetchPosts(c.Params("postID")) - if err != nil { + if post.Id == "" { + post, err = api.FetchPosts(c.Params("postID")) + if post.Id == "" { + post, err = api.FetchMedia(c.Params("postID")) + if post.Id == "" { + return fmt.Errorf("404 page not found") + } + } + } + if err != nil { return err } diff --git a/static/css/embed.css b/static/css/embed.css new file mode 100644 index 0000000..9c5f952 --- /dev/null +++ b/static/css/embed.css @@ -0,0 +1,13 @@ +.logo { + filter: invert(180deg), hue-rotate(180deg); +} + +.postLink { + display: flex; + flex-direction: row; + align-items: center; +} + +.rimgoLink { + width: 100%; +} \ No newline at end of file diff --git a/views/embed.hbs b/views/embed.hbs new file mode 100644 index 0000000..e7438ae --- /dev/null +++ b/views/embed.hbs @@ -0,0 +1,26 @@ + + + + + {{> partials/head }} + + + + + + + + + + \ No newline at end of file