Skip to content

Commit 1eb851c

Browse files
committed
Fixes and cleanup
1 parent 83ac2e4 commit 1eb851c

File tree

6 files changed

+360
-122
lines changed

6 files changed

+360
-122
lines changed

fileuploader.config.example.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ CheckInterval = "5m"
4949
TemplatePath = "templates/embed.html"
5050
CacheMaxAge = "1h"
5151
CacheCleanInterval = "15m"
52-
ImageCachePath = "image-cache"
53-
ImageCacheMaxSize = 1073741824
54-
5552

5653
# If EXTJWT is supported by the gateway or network, a validated token with an account present (when
5754
# the user is authenticated to an irc services account) will use the IdentifiedMaxAge setting above

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ require (
77
github.com/OneOfOne/xxhash v1.2.7 // indirect
88
github.com/bmizerany/pat v0.0.0-20170815010413-6226ea591a40 // indirect
99
github.com/c2h5oh/datasize v0.0.0-20171227191756-4eba002a5eae
10-
github.com/davecgh/go-spew v1.1.1
1110
github.com/dgrijalva/jwt-go v3.2.0+incompatible
1211
github.com/dyatlov/go-oembed v0.0.0-20191103150536-a57c85b3b37c
1312
github.com/gin-contrib/sse v0.1.0 // indirect

go.sum

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ github.com/coreos/go-systemd v0.0.0-20181012123002-c6f51f82210d/go.mod h1:F5haX7
5151
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
5252
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
5353
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
54-
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
5554
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5655
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
5756
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=

noembed/noembed.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package noembed
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"io"
7+
"io/ioutil"
8+
"net/http"
9+
"regexp"
10+
"strings"
11+
)
12+
13+
var noembedURL = "https://noembed.com/embed?url={url}"
14+
15+
// NoEmbed represents this package
16+
type NoEmbed struct {
17+
data *Data
18+
}
19+
20+
// Data represents the data for noembed providers
21+
type Data []struct {
22+
Name string `json:"name"`
23+
Patterns []Regex `json:"patterns"`
24+
}
25+
26+
// Response represents the data returned by noembed server
27+
type Response struct {
28+
AuthorName string `json:"author_name"`
29+
AuthorURL string `json:"author_url"`
30+
ProviderName string `json:"provider_name"`
31+
ProviderURL string `json:"provider_url"`
32+
Title string `json:"title"`
33+
Type string `json:"type"`
34+
URL string `json:"url"`
35+
HTML string `json:"html"`
36+
Version string `json:"version"`
37+
ThumbnailURL string `json:"thumbnail_url"`
38+
ThumbnailWidth int `json:"thumbnail_width,string"`
39+
ThumbnailHeight int `json:"thumbnail_height,string"`
40+
Width int `json:"width,string"`
41+
Height int `json:"height,string"`
42+
}
43+
44+
// New returns a Noembed object
45+
func New() *NoEmbed {
46+
return &NoEmbed{}
47+
}
48+
49+
// ParseProviders parses the raw json obtained from noembed.com
50+
func (n *NoEmbed) ParseProviders(buf io.Reader) error {
51+
data, err := ioutil.ReadAll(buf)
52+
if err != nil {
53+
return err
54+
}
55+
56+
var noembedData Data
57+
err = json.Unmarshal(data, &noembedData)
58+
if err != nil {
59+
return err
60+
}
61+
62+
n.data = &noembedData
63+
return nil
64+
}
65+
66+
// Get returns a noembed response object
67+
func (n *NoEmbed) Get(url string) (resp *Response, err error) {
68+
if !n.ValidURL(url) {
69+
err = errors.New("Unsupported URL")
70+
return
71+
}
72+
73+
reqURL := strings.Replace(noembedURL, "{url}", url, 1)
74+
75+
var httpResp *http.Response
76+
httpResp, err = http.Get(reqURL)
77+
if err != nil {
78+
return
79+
}
80+
defer httpResp.Body.Close()
81+
82+
var body []byte
83+
body, err = ioutil.ReadAll(httpResp.Body)
84+
if err != nil {
85+
return
86+
}
87+
88+
err = json.Unmarshal(body, &resp)
89+
if err != nil {
90+
return
91+
}
92+
93+
return
94+
}
95+
96+
// ValidURL is used to test if a url is supported by noembed
97+
func (n *NoEmbed) ValidURL(url string) bool {
98+
for _, entry := range *n.data {
99+
for _, pattern := range entry.Patterns {
100+
if pattern.Regexp.MatchString(url) {
101+
return true
102+
}
103+
}
104+
}
105+
return false
106+
}
107+
108+
// Regex Unmarshaler
109+
type Regex struct {
110+
regexp.Regexp
111+
}
112+
113+
// UnmarshalText used to unmarshal regexp's from text
114+
func (r *Regex) UnmarshalText(text []byte) error {
115+
reg, err := regexp.Compile(string(text))
116+
r.Regexp = *reg
117+
return err
118+
}

0 commit comments

Comments
 (0)