Skip to content

Commit

Permalink
Make fallback embed more generalised && bundle html templates in the …
Browse files Browse the repository at this point in the history
…binary
  • Loading branch information
ItsOnlyBinary committed Dec 14, 2020
1 parent 9dded0a commit 27b3bef
Show file tree
Hide file tree
Showing 8 changed files with 378 additions and 238 deletions.
125 changes: 125 additions & 0 deletions fallback-embed/fallback-embed-provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package fallbackembed

import (
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
"regexp"
"strconv"
"strings"
"time"
)

// FallbackEmbed represents this package
type FallbackEmbed struct {
data *Data
httpClient *http.Client
targetKey string
providerURL string
}

// Data represents the data for FallbackEmbed providers
type Data []struct {
Name string `json:"name"`
Patterns []Regex `json:"patterns"`
}

// New returns a FallbackEmbed object
func New(providerURL, targetKey string) *FallbackEmbed {
obj := &FallbackEmbed{
httpClient: &http.Client{
Timeout: time.Second * 30,
},
providerURL: providerURL,
targetKey: targetKey,
}

return obj
}

// ParseProviders parses the raw json obtained from noembed.com
func (f *FallbackEmbed) ParseProviders(buf io.Reader) error {
data, err := ioutil.ReadAll(buf)
if err != nil {
return err
}

var providerData Data
err = json.Unmarshal(data, &providerData)
if err != nil {
return err
}

f.data = &providerData
return nil
}

// Get returns html string
func (f *FallbackEmbed) Get(url string, width int, height int) (html string, err error) {
if !f.ValidURL(url) {
return
}

// Do replacements
reqURL := strings.Replace(f.providerURL, "{url}", url, 1)
reqURL = strings.Replace(reqURL, "{width}", strconv.Itoa(width), 1)
reqURL = strings.Replace(reqURL, "{height}", strconv.Itoa(height), 1)

var httpResp *http.Response
httpResp, err = f.httpClient.Get(reqURL)
if err != nil {
return
}
defer httpResp.Body.Close()

var body []byte
body, err = ioutil.ReadAll(httpResp.Body)
if err != nil {
return
}

// Try to parse json response
resp := make(map[string]interface{})
err = json.Unmarshal(body, &resp)
if err != nil {
return
}

// Check targetKey exists
if jsonVal, ok := resp[f.targetKey]; ok {
// Check targetVal is string
if htmlString, ok := jsonVal.(string); ok {
html = htmlString
return
}
}

err = errors.New("Failed to get target json key")
return
}

// ValidURL is used to test if a url is supported by noembed
func (f *FallbackEmbed) ValidURL(url string) bool {
for _, entry := range *f.data {
for _, pattern := range entry.Patterns {
if pattern.Regexp.MatchString(url) {
return true
}
}
}
return false
}

// Regex Unmarshaler
type Regex struct {
regexp.Regexp
}

// UnmarshalText used to unmarshal regexp's from text
func (r *Regex) UnmarshalText(text []byte) error {
reg, err := regexp.Compile(string(text))
r.Regexp = *reg
return err
}
10 changes: 8 additions & 2 deletions fileuploader.config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,17 @@ MaxAge = "24h" # 1 day
IdentifiedMaxAge = "168h" # 1 week
CheckInterval = "5m"

[Embed]
TemplatePath = "templates/embed.html"
[WebPreview]
TemplatesDirectory = "templates"
CacheMaxAge = "1h"
CacheCleanInterval = "15m"

# Fallback provider specific
FallbackProviderDisabled = false
FallbackProviderURL = "https://noembed.com/embed?url={url}"
FallbackProviderFile = "fallback-providers.json"
FallbackProviderJsonKey = "html"

# If EXTJWT is supported by the gateway or network, a validated token with an account present (when
# the user is authenticated to an irc services account) will use the IdentifiedMaxAge setting above
# instead of the base MaxAge.
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/kiwiirc/plugin-fileuploader/server"
)

//go:generate go run ./scripts/generate-templates.go

func main() {
var configPath = flag.String("config", "fileuploader.config.toml", "path to config file")
flag.Parse()
Expand Down
124 changes: 0 additions & 124 deletions noembed/noembed.go

This file was deleted.

27 changes: 27 additions & 0 deletions scripts/generate-templates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"io"
"io/ioutil"
"os"
"path"
"strings"
)

const templatesDir = "./templates"

func main() {
files, _ := ioutil.ReadDir(templatesDir)

out, _ := os.Create(path.Join(templatesDir, "templates.go"))
out.Write([]byte("package templates\n\nvar Get = map[string]string{\n"))
for _, fileInfo := range files {
if strings.HasSuffix(fileInfo.Name(), ".html") {
out.Write([]byte(strings.TrimSuffix(fileInfo.Name(), ".html") + ": `"))
file, _ := os.Open(path.Join(templatesDir, fileInfo.Name()))
io.Copy(out, file)
out.Write([]byte("`,\n"))
}
}
out.Write([]byte("}\n"))
}
14 changes: 9 additions & 5 deletions server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,17 @@ type Config struct {
JwtSecretsByIssuer map[string]string
Loggers []LoggerConfig

// Embed Provider
Embed struct {
TemplatePath string
// WebPreview config options
WebPreview struct {
TemplatesDirectory string
CacheMaxAge duration
CacheCleanInterval duration
ImageCachePath string
ImageCacheMaxSize uint64

// Fallback provider configuration
FallbackProviderDisabled bool
FallbackProviderURL string
FallbackProviderFile string
FallbackProviderJsonKey string
}
}

Expand Down
Loading

0 comments on commit 27b3bef

Please sign in to comment.