letlist for remote servers it'll try fetch thumbnails from

This commit is contained in:
Jan Delta 2022-08-14 19:26:04 +09:00
parent 49eadf1288
commit 0fe6a09dbd
2 changed files with 27 additions and 10 deletions

View File

@ -7,6 +7,7 @@ import (
"io"
"log"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
@ -19,23 +20,38 @@ var picrewRegex = regexp.MustCompile(`^https?\:\/\/picrew\.me\/image_maker\/(\d+
var thumbExtractor = regexp.MustCompile(`\<meta[^\>]*property=\"(?:og\:)?image\" content=\"(https?\:\/\/\w+\.\w+\/[^>]+\.(?:png|jpeg|jpg))\"\>`)
var nameExtractor = regexp.MustCompile(`\<meta[^\>]*property=\"(?:og\:)?title\" content=\"([^">]+)\"\>`)
func getThumbURL(url string) (thumb string, name string) {
func getThumbURL(uri string) (thumb string, name string) {
purl, err := url.Parse(uri)
if err != nil {
log.Println(err)
return
}
if Conf.ThumbnailLetlist != nil {
for _, x := range Conf.ThumbnailLetlist {
if purl.Host == x {
goto pass
}
}
return
}
pass:
if EnableThumbs {
mapMu.Lock()
defer mapMu.Unlock()
if !thumbRatelimit[url].IsZero() && time.Now().Sub(thumbRatelimit[url]) < time.Hour*24 {
id, err := GetMakerID(url)
if !thumbRatelimit[uri].IsZero() && time.Now().Sub(thumbRatelimit[uri]) < time.Hour*24 {
id, err := GetMakerID(uri)
if err != nil {
return
}
thumb = Conf.BaseURL + "/media/" + strconv.Itoa(id) + ".jpeg"
name = nameCache[url]
name = nameCache[uri]
return
}
thumbRatelimit[url] = time.Now()
thumbRatelimit[uri] = time.Now()
}
req, err := http.NewRequest("GET", url, nil)
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
log.Println(err)
return "", ""
@ -57,15 +73,15 @@ func getThumbURL(url string) (thumb string, name string) {
if thumbExtractor.Match(dat) {
thumb = thumbExtractor.FindStringSubmatch(string(dat))[1]
if EnableThumbs && (strings.HasSuffix(thumb, ".png") || strings.HasSuffix(thumb, ".jpeg") || strings.HasSuffix(thumb, ".jpg")) {
go addThumbnail(url, thumb)
go addThumbnail(uri, thumb)
}
}
if nameExtractor.Match(dat) {
name = nameExtractor.FindStringSubmatch(string(dat))[1]
nameCache[url] = name
nameCache[uri] = name
}
if EnableThumbs {
thumbRatelimit[url] = time.Now()
thumbRatelimit[uri] = time.Now()
}
return
}

View File

@ -21,5 +21,6 @@ type Config struct {
BaseURL string
ThumbnailDir string
LogFile string // if unset it'll log to stdout
ThumbnailLetlist []string
LogFile string // if unset it'll log to stdout
}