mirror of
https://codeberg.org/video-prize-ranch/rimgo.git
synced 2026-01-28 09:31:13 +00:00
move sanitization code out of api package
This commit is contained in:
@@ -4,16 +4,12 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"codeberg.org/rimgo/rimgo/utils"
|
||||
"github.com/microcosm-cc/bluemonday"
|
||||
"github.com/patrickmn/go-cache"
|
||||
"github.com/tidwall/gjson"
|
||||
"gitlab.com/golang-commonmark/linkify"
|
||||
)
|
||||
|
||||
type Comment struct {
|
||||
@@ -64,50 +60,11 @@ func (client *Client) FetchComments(galleryID string) ([]Comment, error) {
|
||||
return parsed.Data, nil
|
||||
}
|
||||
|
||||
var imgurRe = regexp.MustCompile(`https?://imgur\.com/(gallery|a)?/(.*)`)
|
||||
var imgurRe2 = regexp.MustCompile(`https?://imgur\.com/(.*)`)
|
||||
var imgRe = regexp.MustCompile(`https?://i\.imgur\.com/(.*)\.(png|gif|jpe?g|webp)`)
|
||||
var vidRe = regexp.MustCompile(`https?://i\.imgur\.com/(.*)\.(mp4|webm)`)
|
||||
var vidFormatRe = regexp.MustCompile(`\.(mp4|webm)`)
|
||||
var iImgurRe = regexp.MustCompile(`https?://i\.imgur\.com`)
|
||||
|
||||
func parseComment(data json.RawMessage, out *Comment) {
|
||||
err := json.Unmarshal(data, &out)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
comment := &out.Comment
|
||||
*comment = strings.ReplaceAll(*comment, "\n", "<br>")
|
||||
|
||||
for _, match := range imgRe.FindAllString(*comment, -1) {
|
||||
img := iImgurRe.ReplaceAllString(match, "")
|
||||
img = `<img src="` + img + `" class="comment__media" loading="lazy"/>`
|
||||
*comment = strings.Replace(*comment, match, img, 1)
|
||||
}
|
||||
for _, match := range vidRe.FindAllString(*comment, -1) {
|
||||
vid := iImgurRe.ReplaceAllString(match, "")
|
||||
vid = `<video class="comment__media" controls loop preload="none" poster="` + vidFormatRe.ReplaceAllString(vid, ".webp") + `"><source type="` + strings.Split(vid, ".")[1] + `" src="` + vid + `" /></video>`
|
||||
*comment = strings.Replace(*comment, match, vid, 1)
|
||||
}
|
||||
for _, l := range linkify.Links(*comment) {
|
||||
origLink := (*comment)[l.Start:l.End]
|
||||
link := `<a href="` + origLink + `">` + origLink + `</a>`
|
||||
*comment = strings.Replace(*comment, origLink, link, 1)
|
||||
}
|
||||
*comment = imgurRe.ReplaceAllString(*comment, "/$1/$2")
|
||||
*comment = imgurRe2.ReplaceAllString(*comment, "/$1")
|
||||
|
||||
p := bluemonday.UGCPolicy()
|
||||
p.AllowImages()
|
||||
p.AllowElements("video", "source")
|
||||
p.AllowAttrs("src", "tvpe").OnElements("source")
|
||||
p.AllowAttrs("controls", "loop", "preload", "poster").OnElements("video")
|
||||
p.AllowAttrs("class", "loading").OnElements("img", "video")
|
||||
p.RequireNoReferrerOnLinks(true)
|
||||
p.RequireNoFollowOnLinks(true)
|
||||
p.RequireCrossOriginAnonymous(true)
|
||||
*comment = p.Sanitize(*comment)
|
||||
}
|
||||
|
||||
type commentArray []Comment
|
||||
|
||||
Reference in New Issue
Block a user