diff --git a/api/album.go b/api/album.go index f56e679..6cb2a74 100644 --- a/api/album.go +++ b/api/album.go @@ -1,8 +1,6 @@ package api import ( - "io" - "net/http" "strings" "time" @@ -20,25 +18,16 @@ func FetchAlbum(albumID string) (types.Album, error) { return cacheData.(types.Album), nil } - res, err := http.Get("https://api.imgur.com/post/v1/albums/" + albumID + "?client_id=" + utils.Config["imgurId"].(string) + "&include=media%2Caccount") + data, err := utils.GetJSON("https://api.imgur.com/post/v1/albums/" + albumID + "?client_id=" + utils.Config["imgurId"].(string) + "&include=media%2Caccount") if err != nil { return types.Album{}, err } - body, err := io.ReadAll(res.Body) + album, err := ParseAlbum(data) if err != nil { return types.Album{}, err } - data := gjson.Parse(string(body)) - - album := types.Album{} - if data.Get("shared_with_community").Bool() || res.StatusCode == 404 { - album, err = FetchPosts(albumID) - } else { - album, err = ParseAlbum(data) - } - albumCache.Set(albumID + "-album", album, cache.DefaultExpiration) return album, err } @@ -49,17 +38,11 @@ func FetchPosts(albumID string) (types.Album, error) { return cacheData.(types.Album), nil } - res, err := http.Get("https://api.imgur.com/post/v1/posts/" + albumID + "?client_id=" + utils.Config["imgurId"].(string) + "&include=media%2Caccount%2Ctags") + data, err := utils.GetJSON("https://api.imgur.com/post/v1/posts/" + albumID + "?client_id=" + utils.Config["imgurId"].(string) + "&include=media%2Caccount%2Ctags") 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)) album, err := ParseAlbum(data) if err != nil { return types.Album{}, err @@ -75,17 +58,11 @@ func FetchMedia(mediaID string) (types.Album, error) { return cacheData.(types.Album), nil } - res, err := http.Get("https://api.imgur.com/post/v1/media/" + mediaID + "?client_id=" + utils.Config["imgurId"].(string) + "&include=media%2Caccount") + data, err := utils.GetJSON("https://api.imgur.com/post/v1/media/" + mediaID + "?client_id=" + utils.Config["imgurId"].(string) + "&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)) album, err := ParseAlbum(data) if err != nil { return types.Album{}, err diff --git a/api/comments.go b/api/comments.go index 0508f46..6e05d51 100644 --- a/api/comments.go +++ b/api/comments.go @@ -1,8 +1,6 @@ package api import ( - "io" - "net/http" "strings" "sync" "time" @@ -22,18 +20,11 @@ func FetchComments(galleryID string) ([]types.Comment, error) { return cacheData.([]types.Comment), nil } - res, err := http.Get("https://api.imgur.com/comment/v1/comments?client_id=" + utils.Config["imgurId"].(string) + "&filter[post]=eq:" + galleryID + "&include=account,adconfig&per_page=30&sort=best") + data, err := utils.GetJSON("https://api.imgur.com/comment/v1/comments?client_id=" + utils.Config["imgurId"].(string) + "&filter[post]=eq:" + galleryID + "&include=account,adconfig&per_page=30&sort=best") if err != nil { - return []types.Comment{}, err + return []types.Comment{}, nil } - body, err := io.ReadAll(res.Body) - if err != nil { - return []types.Comment{}, err - } - - data := gjson.Parse(string(body)) - wg := sync.WaitGroup{} comments := make([]types.Comment, 0) data.Get("data").ForEach( diff --git a/api/user.go b/api/user.go index d4d3381..1c9c70f 100644 --- a/api/user.go +++ b/api/user.go @@ -55,18 +55,11 @@ func FetchSubmissions(username string, sort string, page string) ([]types.Submis return cacheData.([]types.Submission), nil } - res, err := http.Get("https://api.imgur.com/3/account/" + username + "/submissions/" + page + "/" + sort + "?album_previews=1&client_id=" + utils.Config["imgurId"].(string)) + data, err := utils.GetJSON("https://api.imgur.com/3/account/" + username + "/submissions/" + page + "/" + sort + "?album_previews=1&client_id=" + utils.Config["imgurId"].(string)) if err != nil { return []types.Submission{}, err } - body, err := io.ReadAll(res.Body) - if err != nil { - return []types.Submission{}, err - } - - data := gjson.Parse(string(body)) - submissions := []types.Submission{} wg := sync.WaitGroup{} diff --git a/main.go b/main.go index f2a0ba8..2ed1209 100644 --- a/main.go +++ b/main.go @@ -44,13 +44,13 @@ func main() { app.Get("/:postID.gifv", pages.HandleEmbed) app.Get("/:baseName.:extension", pages.HandleMedia) app.Get("/:postID", pages.HandlePost) - app.Get("/a/:galleryID", pages.HandleGallery) + app.Get("/a/:postID", pages.HandlePost) app.Get("/t/:tag", pages.HandleTag) app.Get("/user/:userID", pages.HandleUser) app.Get("/r/:sub/:postID", pages.HandlePost) app.Get("/user/:userID/cover", pages.HandleUserCover) app.Get("/user/:userID/avatar", pages.HandleUserAvatar) - app.Get("/gallery/:galleryID", pages.HandleGallery) + app.Get("/gallery/:postID", pages.HandlePost) app.Listen(":" + utils.Config["port"].(string)) } diff --git a/pages/gallery.go b/pages/gallery.go deleted file mode 100644 index 2c5de97..0000000 --- a/pages/gallery.go +++ /dev/null @@ -1,34 +0,0 @@ -package pages - -import ( - "codeberg.org/video-prize-ranch/rimgo/api" - "codeberg.org/video-prize-ranch/rimgo/types" - "codeberg.org/video-prize-ranch/rimgo/utils" - "github.com/gofiber/fiber/v2" -) - -func HandleGallery(c *fiber.Ctx) error { - utils.SetHeaders(c) - c.Set("Content-Security-Policy", "default-src 'none'; media-src 'self'; style-src 'unsafe-inline' 'self'; img-src 'self'; font-src 'self'; manifest-src 'self'; block-all-mixed-content") - - album, err := api.FetchAlbum(c.Params("galleryID")) - if err != nil { - return err - } - - comments := []types.Comment{} - if album.SharedWithCommunity { - c.Set("Cache-Control", "public,max-age=604800") - comments, err = api.FetchComments(c.Params("galleryID")) - if err != nil { - return err - } - } else { - c.Set("Cache-Control", "public,max-age=31557600") - } - - return c.Render("post", fiber.Map{ - "post": album, - "comments": comments, - }) -} diff --git a/pages/post.go b/pages/post.go index 47adec5..f36d56a 100644 --- a/pages/post.go +++ b/pages/post.go @@ -2,6 +2,7 @@ package pages import ( "fmt" + "strings" "codeberg.org/video-prize-ranch/rimgo/api" "codeberg.org/video-prize-ranch/rimgo/types" @@ -13,15 +14,18 @@ func HandlePost(c *fiber.Ctx) error { utils.SetHeaders(c) c.Set("Content-Security-Policy", "default-src 'none'; media-src 'self'; style-src 'self'; img-src 'self'; font-src 'self'; manifest-src 'self'; block-all-mixed-content") - post, err := api.FetchPosts(c.Params("postID")) - if post.Id == "" { + post, err := types.Album{}, error(nil) + switch { + case strings.HasPrefix(c.Path(), "/a"): + post, err = api.FetchAlbum(c.Params("postID")) + println(post.Title) + case strings.HasPrefix(c.Path(), "/gallery"): 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") - } - } + default: + post, err = api.FetchMedia(c.Params("postID")) + } + if post.Id == "" || strings.Contains(err.Error(), "404") { + return fmt.Errorf("404 page not found") } if err != nil { return err diff --git a/utils/request.go b/utils/request.go new file mode 100644 index 0000000..cb8e20e --- /dev/null +++ b/utils/request.go @@ -0,0 +1,44 @@ +package utils + +import ( + "fmt" + "io" + "net/http" + + "github.com/tidwall/gjson" +) + +func GetJSON(url string) (gjson.Result, error) { + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return gjson.Result{}, err + } + + req.Header.Set("Accept", "*/*") + req.Header.Set("Accept-Language", "en-US,en;q=0.5") + req.Header.Set("Cache-Control", "no-cache") + req.Header.Set("Connection", "keep-alive") + req.Header.Set("Origin", "https://imgur.com") + req.Header.Set("Pragma", "no-cache") + req.Header.Set("Sec-Fetch-Dest", "empty") + req.Header.Set("Sec-Fetch-Mode", "cors") + req.Header.Set("Sec-Fetch-Site", "same-site") + req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0") + + client := http.Client{} + res, err := client.Do(req) + if err != nil { + return gjson.Result{}, err + } + + body, err := io.ReadAll(res.Body) + if err != nil { + return gjson.Result{}, err + } + + if res.StatusCode != 200 { + return gjson.Result{}, fmt.Errorf("received status %s, expected 200 OK.\n%s", res.Status, string(body)) + } + + return gjson.Parse(string(body)), nil +} \ No newline at end of file