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 8059daf
Show file tree
Hide file tree
Showing 4 changed files with 360 additions and 120 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
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 8059daf

Please sign in to comment.