aboutsummaryrefslogtreecommitdiff
path: root/server/ui/controller/proxy.go
diff options
context:
space:
mode:
authorGravatar Frédéric Guillot <fred@miniflux.net> 2017-11-21 23:09:01 -0800
committerGravatar Frédéric Guillot <fred@miniflux.net> 2017-11-21 23:09:01 -0800
commit38941f58cf72e8e8811377570fdab1426cc6947b (patch)
treec74cb4ded4931b1566cff9b270153f049a66e61f /server/ui/controller/proxy.go
parent99dfbdbb471ffc4e32fed78f23383b221d73155a (diff)
downloadv2-38941f58cf72e8e8811377570fdab1426cc6947b.tar.gz
v2-38941f58cf72e8e8811377570fdab1426cc6947b.tar.zst
v2-38941f58cf72e8e8811377570fdab1426cc6947b.zip
Improve image proxy
Diffstat (limited to 'server/ui/controller/proxy.go')
-rw-r--r--server/ui/controller/proxy.go25
1 files changed, 16 insertions, 9 deletions
diff --git a/server/ui/controller/proxy.go b/server/ui/controller/proxy.go
index 19a64366..fdbdb4e8 100644
--- a/server/ui/controller/proxy.go
+++ b/server/ui/controller/proxy.go
@@ -7,15 +7,23 @@ package controller
import (
"encoding/base64"
"errors"
- "github.com/miniflux/miniflux2/helper"
- "github.com/miniflux/miniflux2/server/core"
"io/ioutil"
"log"
- "net/http"
"time"
+
+ "github.com/miniflux/miniflux2/helper"
+ "github.com/miniflux/miniflux2/reader/http"
+ "github.com/miniflux/miniflux2/server/core"
)
+// ImageProxy fetch an image from a remote server and sent it back to the browser.
func (c *Controller) ImageProxy(ctx *core.Context, request *core.Request, response *core.Response) {
+ // If we receive a "If-None-Match" header we assume the image in stored in browser cache
+ if request.Request().Header.Get("If-None-Match") != "" {
+ response.NotModified()
+ return
+ }
+
encodedURL := request.StringParam("encodedURL", "")
if encodedURL == "" {
response.HTML().BadRequest(errors.New("No URL provided"))
@@ -28,22 +36,21 @@ func (c *Controller) ImageProxy(ctx *core.Context, request *core.Request, respon
return
}
- resp, err := http.Get(string(decodedURL))
+ client := http.NewClient(string(decodedURL))
+ resp, err := client.Get()
if err != nil {
- log.Println(err)
+ log.Println("[ImageProxy]", err)
response.HTML().NotFound()
return
}
- defer resp.Body.Close()
- if resp.StatusCode != http.StatusOK {
+ if resp.HasServerFailure() {
response.HTML().NotFound()
return
}
body, _ := ioutil.ReadAll(resp.Body)
etag := helper.HashFromBytes(body)
- contentType := resp.Header.Get("Content-Type")
- response.Cache(contentType, etag, body, 72*time.Hour)
+ response.Cache(resp.ContentType, etag, body, 72*time.Hour)
}