Skip to content

Commit

Permalink
Fixes and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsOnlyBinary committed Dec 10, 2020
1 parent 83ac2e4 commit 1eb851c
Show file tree
Hide file tree
Showing 6 changed files with 360 additions and 122 deletions.
3 changes: 0 additions & 3 deletions fileuploader.config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ CheckInterval = "5m"
TemplatePath = "templates/embed.html"
CacheMaxAge = "1h"
CacheCleanInterval = "15m"
ImageCachePath = "image-cache"
ImageCacheMaxSize = 1073741824


# 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
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/OneOfOne/xxhash v1.2.7 // indirect
github.com/bmizerany/pat v0.0.0-20170815010413-6226ea591a40 // indirect
github.com/c2h5oh/datasize v0.0.0-20171227191756-4eba002a5eae
github.com/davecgh/go-spew v1.1.1
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/dyatlov/go-oembed v0.0.0-20191103150536-a57c85b3b37c
github.com/gin-contrib/sse v0.1.0 // indirect
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ github.com/coreos/go-systemd v0.0.0-20181012123002-c6f51f82210d/go.mod h1:F5haX7
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
Expand Down
118 changes: 118 additions & 0 deletions noembed/noembed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package noembed

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

var noembedURL = "https://noembed.com/embed?url={url}"

// NoEmbed represents this package
type NoEmbed struct {
data *Data
}

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

// Response represents the data returned by noembed server
type Response struct {
AuthorName string `json:"author_name"`
AuthorURL string `json:"author_url"`
ProviderName string `json:"provider_name"`
ProviderURL string `json:"provider_url"`
Title string `json:"title"`
Type string `json:"type"`
URL string `json:"url"`
HTML string `json:"html"`
Version string `json:"version"`
ThumbnailURL string `json:"thumbnail_url"`
ThumbnailWidth int `json:"thumbnail_width,string"`
ThumbnailHeight int `json:"thumbnail_height,string"`
Width int `json:"width,string"`
Height int `json:"height,string"`
}

// New returns a Noembed object
func New() *NoEmbed {
return &NoEmbed{}
}

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

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

n.data = &noembedData
return nil
}

// Get returns a noembed response object
func (n *NoEmbed) Get(url string) (resp *Response, err error) {
if !n.ValidURL(url) {
err = errors.New("Unsupported URL")
return
}

reqURL := strings.Replace(noembedURL, "{url}", url, 1)

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

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

err = json.Unmarshal(body, &resp)
if err != nil {
return
}

return
}

// ValidURL is used to test if a url is supported by noembed
func (n *NoEmbed) ValidURL(url string) bool {
for _, entry := range *n.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
}
Loading

0 comments on commit 1eb851c

Please sign in to comment.