Skip to content

Commit ed5fad9

Browse files
committed
Correctly encode url in fallback embed && better error handling
1 parent 517cd32 commit ed5fad9

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

fallback-embed/fallback-embed-provider.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"io/ioutil"
88
"net/http"
9+
"net/url"
910
"regexp"
1011
"strconv"
1112
"strings"
@@ -57,13 +58,13 @@ func (f *FallbackEmbed) ParseProviders(buf io.Reader) error {
5758
}
5859

5960
// Get returns html string
60-
func (f *FallbackEmbed) Get(url string, width int, height int) (html string, err error) {
61-
if !f.ValidURL(url) {
61+
func (f *FallbackEmbed) Get(wantedURL string, width int, height int) (html string, err error) {
62+
if !f.ValidURL(wantedURL) {
6263
return
6364
}
6465

6566
// Do replacements
66-
reqURL := strings.Replace(f.providerURL, "{url}", url, 1)
67+
reqURL := strings.Replace(f.providerURL, "{url}", url.QueryEscape(wantedURL), 1)
6768
reqURL = strings.Replace(reqURL, "{width}", strconv.Itoa(width), 1)
6869
reqURL = strings.Replace(reqURL, "{height}", strconv.Itoa(height), 1)
6970

@@ -96,7 +97,16 @@ func (f *FallbackEmbed) Get(url string, width int, height int) (html string, err
9697
}
9798
}
9899

99-
err = errors.New("Failed to get target json key")
100+
// Check for error key in json response
101+
if jsonVal, ok := resp["error"]; ok {
102+
// Check error is string
103+
if errorString, ok := jsonVal.(string); ok {
104+
err = errors.New(errorString)
105+
return
106+
}
107+
}
108+
109+
err = errors.New(`Fallback embed provider did not include a JSON property of "` + f.targetKey + `"`)
100110
return
101111
}
102112

server/web-preview.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"encoding/hex"
88
"errors"
99
"io/ioutil"
10+
"log"
1011
"net/http"
1112
"net/url"
1213
"os"
@@ -233,6 +234,7 @@ func (serv *UploadServer) handleImageCache(c *gin.Context) {
233234

234235
func (serv *UploadServer) handleWebPreview(c *gin.Context) {
235236
queryURL := c.Query("url")
237+
log.Printf("queryURL: " + queryURL)
236238
if !isValidURL(queryURL) {
237239
c.AbortWithStatus(http.StatusBadRequest)
238240
return
@@ -319,13 +321,13 @@ func (serv *UploadServer) handleWebPreview(c *gin.Context) {
319321

320322
// No embedable html, time to try our fallback provider
321323
if item.html == "" && !fallbackEmbedDisabled {
322-
noEmbedResp, err := fallbackEmbed.Get(queryURL, width, height)
324+
fallbackEmbedResp, err := fallbackEmbed.Get(queryURL, width, height)
323325
if err != nil {
324326
serv.log.Error().
325327
Err(err).
326-
Msg("Unexpected error in noEmbed")
328+
Msg("Unexpected error in fallback embed")
327329
} else {
328-
item.html = noEmbedResp
330+
item.html = fallbackEmbedResp
329331
}
330332
}
331333

0 commit comments

Comments
 (0)