From ac98a54f79d57e034469c0eb963861f9abd14078 Mon Sep 17 00:00:00 2001 From: video-prize-ranch Date: Mon, 17 Jan 2022 15:48:24 -0500 Subject: [PATCH] Stream images --- api/album.go | 3 +-- main.go | 2 +- pages/media.go | 17 ++++------------- static/css/album.css | 0 static/css/base.css | 11 +++++++++++ views/gallery.hbs | 14 +++++++++++--- views/partials/head.hbs | 5 ++--- views/partials/header.hbs | 2 +- 8 files changed, 31 insertions(+), 23 deletions(-) create mode 100644 static/css/album.css create mode 100644 static/css/base.css diff --git a/api/album.go b/api/album.go index 6754ae5..6ef9948 100644 --- a/api/album.go +++ b/api/album.go @@ -30,8 +30,7 @@ func FetchAlbum(albumID string) (types.Album, error) { data.Get("media").ForEach( func(key gjson.Result, value gjson.Result) bool { url := value.Get("url").String() - println(url) - url = strings.ReplaceAll(url, "https://i.imgur.com", "") + url = strings.ReplaceAll(url, "https://i.imgur.com", "/media") media = append(media, url) return true diff --git a/main.go b/main.go index c253ac1..ea911e2 100644 --- a/main.go +++ b/main.go @@ -43,7 +43,7 @@ func main() { Root: http.FS(static.GetFiles()), })) - app.Get("/:baseName.:extension", pages.HandleMedia) + app.Get("/media/:baseName.:extension", pages.HandleMedia) app.Get("/a/:albumID", pages.HandleAlbum) app.Get("/t/:tagID", pages.HandleAlbum) /*app.Get("/user/:userID", pages.HandleUser) diff --git a/pages/media.go b/pages/media.go index f93011e..6781863 100644 --- a/pages/media.go +++ b/pages/media.go @@ -1,29 +1,20 @@ package pages import ( - "io" "net/http" + "strconv" "github.com/gofiber/fiber/v2" ) func HandleMedia(c *fiber.Ctx) error { - res, err := FetchMedia(c.Params("baseName") + "." + c.Params("extension")) - if err != nil { - return err - } - - data, err := io.ReadAll(res.Body) + res, err := http.Get("https://i.imgur.com/" + c.Params("baseName") + "." + c.Params("extension")) if err != nil { return err } c.Set("Content-Type", res.Header.Get("Content-Type")); - c.Write(data) + contentLen, _ := strconv.Atoi(res.Header.Get("Content-Length")) + c.SendStream(res.Body, contentLen) return nil -} - -func FetchMedia(filename string) (*http.Response, error) { - res, err := http.Get("https://i.imgur.com/" + filename) - return res, err } \ No newline at end of file diff --git a/static/css/album.css b/static/css/album.css new file mode 100644 index 0000000..e69de29 diff --git a/static/css/base.css b/static/css/base.css new file mode 100644 index 0000000..6b4e203 --- /dev/null +++ b/static/css/base.css @@ -0,0 +1,11 @@ +body { + font-family: -apple-system, BlinkMacSystemFont, avenir next, avenir, segoe ui, helvetica neue, helvetica, Ubuntu, roboto, noto, arial, sans-serif; +} + +main { + margin: 0 18vw; +} + +img { + max-width: 100%; +} \ No newline at end of file diff --git a/views/gallery.hbs b/views/gallery.hbs index 2813156..813fc86 100644 --- a/views/gallery.hbs +++ b/views/gallery.hbs @@ -1,11 +1,19 @@ + {{> partials/head }} + + - {{#each album.Media}} - - {{/each}} + {{> partials/header }} + +
+ {{#each album.Media}} + + {{/each}} +
+ \ No newline at end of file diff --git a/views/partials/head.hbs b/views/partials/head.hbs index 2dbb9f6..2c4a2e7 100644 --- a/views/partials/head.hbs +++ b/views/partials/head.hbs @@ -1,4 +1,4 @@ -rimgu +rimgo @@ -16,5 +16,4 @@ --> - - \ No newline at end of file + \ No newline at end of file diff --git a/views/partials/header.hbs b/views/partials/header.hbs index c7094d3..a723fdc 100644 --- a/views/partials/header.hbs +++ b/views/partials/header.hbs @@ -1,3 +1,3 @@ \ No newline at end of file