youtube handling + special site handling framework
This commit is contained in:
parent
6a88d9d0f4
commit
17d4d46d09
4 changed files with 97 additions and 8 deletions
|
|
@ -13,6 +13,7 @@ import (
|
||||||
func main() {
|
func main() {
|
||||||
var server = config.CreateServer(
|
var server = config.CreateServer(
|
||||||
os.Args[1:],
|
os.Args[1:],
|
||||||
|
//imagorvideoextended.LoaderConfig,
|
||||||
imagorvideoextended.Config,
|
imagorvideoextended.Config,
|
||||||
imagorvideo.Config,
|
imagorvideo.Config,
|
||||||
vipsconfig.WithVips,
|
vipsconfig.WithVips,
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,10 @@ import (
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func LoaderConfig(fs *flag.FlagSet, cb func() (*zap.Logger, bool)) imagor.Option {
|
||||||
|
return imagor.WithLoaders(NewSpecialLoader())
|
||||||
|
}
|
||||||
|
|
||||||
// Config imagorvideo config.Option
|
// Config imagorvideo config.Option
|
||||||
func Config(fs *flag.FlagSet, cb func() (*zap.Logger, bool)) imagor.Option {
|
func Config(fs *flag.FlagSet, cb func() (*zap.Logger, bool)) imagor.Option {
|
||||||
var (
|
var (
|
||||||
|
|
|
||||||
43
loader.go
Normal file
43
loader.go
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
package imagorvideoextended
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/cshum/imagor"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
/*
|
||||||
|
This was started to handle YouTube, however it turns out they leak just enough info to be useful in the processing
|
||||||
|
phase. This can still be used for sites that have more info in the URL than the connection, though.
|
||||||
|
Currently unloaded to eke out some more perf
|
||||||
|
*/
|
||||||
|
|
||||||
|
type LoaderOption func(h *SpecialLoader)
|
||||||
|
|
||||||
|
// SpecialLoader HTTP Loader implements imagor.Loader interface
|
||||||
|
type SpecialLoader struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSpecialLoader(options ...LoaderOption) *SpecialLoader {
|
||||||
|
loader := &SpecialLoader{}
|
||||||
|
for _, option := range options {
|
||||||
|
option(loader)
|
||||||
|
}
|
||||||
|
return loader
|
||||||
|
}
|
||||||
|
|
||||||
|
func (loader *SpecialLoader) Get(_ *http.Request, key string) (*imagor.Blob, error) {
|
||||||
|
components, err := url.Parse(key)
|
||||||
|
if err != nil {
|
||||||
|
return nil, imagor.ErrInvalid
|
||||||
|
}
|
||||||
|
hostname := components.Hostname()
|
||||||
|
if trimmed, ok := strings.CutPrefix(hostname, "www."); ok {
|
||||||
|
hostname = trimmed
|
||||||
|
}
|
||||||
|
switch components.Hostname() {
|
||||||
|
default:
|
||||||
|
return nil, imagor.ErrInvalid
|
||||||
|
}
|
||||||
|
}
|
||||||
41
processor.go
41
processor.go
|
|
@ -9,7 +9,9 @@ import (
|
||||||
"github.com/gabriel-vasile/mimetype"
|
"github.com/gabriel-vasile/mimetype"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"io"
|
"io"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -41,6 +43,37 @@ func (p *Processor) Shutdown(_ context.Context) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var youtubeIdRegex = regexp.MustCompile("[^\"&?\\/\\s\\.=]{11}")
|
||||||
|
|
||||||
|
func specialUrl(path string) (hostname string, specialHandler string, specialData map[string]string, err error) {
|
||||||
|
specialData = make(map[string]string)
|
||||||
|
components, err := url.Parse(path)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
hostname = components.Host
|
||||||
|
if trimmed, ok := strings.CutPrefix(hostname, "www."); ok {
|
||||||
|
hostname = trimmed
|
||||||
|
}
|
||||||
|
|
||||||
|
switch hostname {
|
||||||
|
case "youtu.be":
|
||||||
|
fallthrough
|
||||||
|
case "youtube.com":
|
||||||
|
fallthrough
|
||||||
|
case "m.youtube.com":
|
||||||
|
id := youtubeIdRegex.FindString(path)
|
||||||
|
println(id)
|
||||||
|
if id != "" {
|
||||||
|
specialHandler = "youtube"
|
||||||
|
specialData["id"] = id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return hostname, specialHandler, specialData, nil
|
||||||
|
}
|
||||||
|
|
||||||
func subThumbnail(url string) string {
|
func subThumbnail(url string) string {
|
||||||
key := os.Getenv("IMAGOR_SECRET")
|
key := os.Getenv("IMAGOR_SECRET")
|
||||||
params := imagorpath.Params{
|
params := imagorpath.Params{
|
||||||
|
|
@ -102,12 +135,17 @@ func (p *Processor) Process(ctx context.Context, in *imagor.Blob, params imagorp
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hostname, specialHandler, specialData, _ := specialUrl(params.Image)
|
||||||
|
|
||||||
doc, err := htmlquery.Parse(strings.NewReader(string(all[:])))
|
doc, err := htmlquery.Parse(strings.NewReader(string(all[:])))
|
||||||
meta := Metadata{
|
meta := Metadata{
|
||||||
Format: strings.TrimPrefix(mime.Extension(), "."),
|
Format: strings.TrimPrefix(mime.Extension(), "."),
|
||||||
Title: "",
|
Title: "",
|
||||||
Description: "",
|
Description: "",
|
||||||
Image: "",
|
Image: "",
|
||||||
|
Hostname: hostname,
|
||||||
|
SpecialHandler: specialHandler,
|
||||||
|
SpecialData: specialData,
|
||||||
}
|
}
|
||||||
metaTags := htmlquery.Find(doc, "//meta[@property]")
|
metaTags := htmlquery.Find(doc, "//meta[@property]")
|
||||||
for _, metaTag := range metaTags {
|
for _, metaTag := range metaTags {
|
||||||
|
|
@ -152,6 +190,9 @@ type Metadata struct {
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
Image string `json:"image"`
|
Image string `json:"image"`
|
||||||
|
Hostname string `json:"hostname"`
|
||||||
|
SpecialHandler string `json:"special_handler"`
|
||||||
|
SpecialData map[string]string `json:"special_data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var transPixel = []byte("\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x21\xF9\x04\x01\x00\x00\x00\x00\x2C\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x44\x01\x00\x3B")
|
var transPixel = []byte("\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x21\xF9\x04\x01\x00\x00\x00\x00\x2C\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x44\x01\x00\x3B")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue