Improve code reuse
This commit is contained in:
parent
5569feb2c8
commit
6eee86d682
31
api/album.go
31
api/album.go
|
@ -1,8 +1,6 @@
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -20,25 +18,16 @@ func FetchAlbum(albumID string) (types.Album, error) {
|
||||||
return cacheData.(types.Album), nil
|
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 {
|
if err != nil {
|
||||||
return types.Album{}, err
|
return types.Album{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
body, err := io.ReadAll(res.Body)
|
album, err := ParseAlbum(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return types.Album{}, err
|
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)
|
albumCache.Set(albumID + "-album", album, cache.DefaultExpiration)
|
||||||
return album, err
|
return album, err
|
||||||
}
|
}
|
||||||
|
@ -49,17 +38,11 @@ func FetchPosts(albumID string) (types.Album, error) {
|
||||||
return cacheData.(types.Album), nil
|
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 {
|
if err != nil {
|
||||||
return types.Album{}, err
|
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)
|
album, err := ParseAlbum(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return types.Album{}, err
|
return types.Album{}, err
|
||||||
|
@ -75,17 +58,11 @@ func FetchMedia(mediaID string) (types.Album, error) {
|
||||||
return cacheData.(types.Album), nil
|
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 {
|
if err != nil {
|
||||||
return types.Album{}, err
|
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)
|
album, err := ParseAlbum(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return types.Album{}, err
|
return types.Album{}, err
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
@ -22,18 +20,11 @@ func FetchComments(galleryID string) ([]types.Comment, error) {
|
||||||
return cacheData.([]types.Comment), nil
|
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 {
|
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{}
|
wg := sync.WaitGroup{}
|
||||||
comments := make([]types.Comment, 0)
|
comments := make([]types.Comment, 0)
|
||||||
data.Get("data").ForEach(
|
data.Get("data").ForEach(
|
||||||
|
|
|
@ -55,18 +55,11 @@ func FetchSubmissions(username string, sort string, page string) ([]types.Submis
|
||||||
return cacheData.([]types.Submission), nil
|
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 {
|
if err != nil {
|
||||||
return []types.Submission{}, err
|
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{}
|
submissions := []types.Submission{}
|
||||||
|
|
||||||
wg := sync.WaitGroup{}
|
wg := sync.WaitGroup{}
|
||||||
|
|
4
main.go
4
main.go
|
@ -44,13 +44,13 @@ func main() {
|
||||||
app.Get("/:postID.gifv", pages.HandleEmbed)
|
app.Get("/:postID.gifv", pages.HandleEmbed)
|
||||||
app.Get("/:baseName.:extension", pages.HandleMedia)
|
app.Get("/:baseName.:extension", pages.HandleMedia)
|
||||||
app.Get("/:postID", pages.HandlePost)
|
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("/t/:tag", pages.HandleTag)
|
||||||
app.Get("/user/:userID", pages.HandleUser)
|
app.Get("/user/:userID", pages.HandleUser)
|
||||||
app.Get("/r/:sub/:postID", pages.HandlePost)
|
app.Get("/r/:sub/:postID", pages.HandlePost)
|
||||||
app.Get("/user/:userID/cover", pages.HandleUserCover)
|
app.Get("/user/:userID/cover", pages.HandleUserCover)
|
||||||
app.Get("/user/:userID/avatar", pages.HandleUserAvatar)
|
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))
|
app.Listen(":" + utils.Config["port"].(string))
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,
|
|
||||||
})
|
|
||||||
}
|
|
|
@ -2,6 +2,7 @@ package pages
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"codeberg.org/video-prize-ranch/rimgo/api"
|
"codeberg.org/video-prize-ranch/rimgo/api"
|
||||||
"codeberg.org/video-prize-ranch/rimgo/types"
|
"codeberg.org/video-prize-ranch/rimgo/types"
|
||||||
|
@ -13,16 +14,19 @@ func HandlePost(c *fiber.Ctx) error {
|
||||||
utils.SetHeaders(c)
|
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")
|
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"))
|
post, err := types.Album{}, error(nil)
|
||||||
if post.Id == "" {
|
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"))
|
post, err = api.FetchPosts(c.Params("postID"))
|
||||||
if post.Id == "" {
|
default:
|
||||||
post, err = api.FetchMedia(c.Params("postID"))
|
post, err = api.FetchMedia(c.Params("postID"))
|
||||||
if post.Id == "" {
|
}
|
||||||
|
if post.Id == "" || strings.Contains(err.Error(), "404") {
|
||||||
return fmt.Errorf("404 page not found")
|
return fmt.Errorf("404 page not found")
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue