Skip to content

Commit

Permalink
Correctly encode url in fallback embed && better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsOnlyBinary committed Jan 4, 2021
1 parent 517cd32 commit 5bb64aa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
16 changes: 13 additions & 3 deletions fallback-embed/fallback-embed-provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -57,13 +58,13 @@ func (f *FallbackEmbed) ParseProviders(buf io.Reader) error {
}

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

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

Expand Down Expand Up @@ -96,6 +97,15 @@ func (f *FallbackEmbed) Get(url string, width int, height int) (html string, err
}
}

// Check for error key in json response
if jsonVal, ok := resp["error"]; ok {
// Check error is string
if errorString, ok := jsonVal.(string); ok {
err = errors.New(errorString)
return
}
}

err = errors.New("Failed to get target json key")
return
}
Expand Down
8 changes: 5 additions & 3 deletions server/web-preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/hex"
"errors"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -233,6 +234,7 @@ func (serv *UploadServer) handleImageCache(c *gin.Context) {

func (serv *UploadServer) handleWebPreview(c *gin.Context) {
queryURL := c.Query("url")
log.Printf("queryURL: " + queryURL)
if !isValidURL(queryURL) {
c.AbortWithStatus(http.StatusBadRequest)
return
Expand Down Expand Up @@ -319,13 +321,13 @@ func (serv *UploadServer) handleWebPreview(c *gin.Context) {

// No embedable html, time to try our fallback provider
if item.html == "" && !fallbackEmbedDisabled {
noEmbedResp, err := fallbackEmbed.Get(queryURL, width, height)
fallbackEmbedResp, err := fallbackEmbed.Get(queryURL, width, height)
if err != nil {
serv.log.Error().
Err(err).
Msg("Unexpected error in noEmbed")
Msg("Unexpected error in fallback embed")
} else {
item.html = noEmbedResp
item.html = fallbackEmbedResp
}
}

Expand Down

0 comments on commit 5bb64aa

Please sign in to comment.