Files
rimgo/pages/embed.go
orangix 02be603dcc refactor api package comments
* use encoding/json for comment parsing

* refactor by moving loop code to an UnmarshalJSON

* use a preallocated array and indices to maintain order while using
  goroutines again, this was removed a while ago

* use new struct in comment.hbs and contextComment.hbs

* rewriteUrl partial to reduce rimgo-specific code in api

* move RenderError into pages package to avoid import cycle between render and utils
2026-01-25 06:08:28 +01:00

50 lines
1.5 KiB
Go

package pages
import (
"net/http"
"strings"
"codeberg.org/rimgo/rimgo/api"
"codeberg.org/rimgo/rimgo/render"
"codeberg.org/rimgo/rimgo/utils"
)
func HandleEmbed(w http.ResponseWriter, r *http.Request) error {
utils.SetHeaders(w)
w.Header().Set("Cache-Control", "public,max-age=31557600")
w.Header().Set("Content-Security-Policy", "default-src 'none'; base-uri 'none'; form-action 'none'; media-src 'self'; style-src 'self'; img-src 'self'; block-all-mixed-content")
post, err := api.Album{}, error(nil)
switch {
case strings.HasPrefix(r.URL.Path, "/a"):
post, err = ApiClient.FetchAlbum(r.PathValue("postID"))
case strings.HasPrefix(r.URL.Path, "/gallery"):
post, err = ApiClient.FetchPosts(r.PathValue("postID"))
default:
post, err = ApiClient.FetchMedia(r.PathValue("postID"))
}
if err != nil && err.Error() == "ratelimited by imgur" {
return RenderError(w, r, 429)
}
if err != nil && post.Id == "" && strings.Contains(err.Error(), "404") {
return RenderError(w, r, 404)
}
if err != nil {
return err
}
return render.Render(w, "embed", map[string]any{
"post": post,
})
}
func HandleGifv(w http.ResponseWriter, r *http.Request) error {
utils.SetHeaders(w)
w.Header().Set("Cache-Control", "public,max-age=31557600")
w.Header().Set("Content-Security-Policy", "default-src 'none'; base-uri 'none'; form-action 'none'; media-src 'self'; style-src 'self'; img-src 'self'; block-all-mixed-content")
return render.Render(w, "gifv", map[string]any{
"id": r.PathValue("postID"),
})
}