diff --git a/fallback-embed/fallback-embed-provider.go b/fallback-embed/fallback-embed-provider.go index 637fcef..4263ae1 100644 --- a/fallback-embed/fallback-embed-provider.go +++ b/fallback-embed/fallback-embed-provider.go @@ -6,6 +6,7 @@ import ( "io" "io/ioutil" "net/http" + "net/url" "regexp" "strconv" "strings" @@ -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) @@ -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 } diff --git a/server/web-preview.go b/server/web-preview.go index 884f0cf..a3a233a 100644 --- a/server/web-preview.go +++ b/server/web-preview.go @@ -7,6 +7,7 @@ import ( "encoding/hex" "errors" "io/ioutil" + "log" "net/http" "net/url" "os" @@ -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 @@ -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 } }