Improve image proxy
This commit is contained in:
parent
99dfbdbb47
commit
38941f58cf
2 changed files with 21 additions and 9 deletions
|
@ -46,6 +46,11 @@ func (r *Response) Redirect(path string) {
|
||||||
http.Redirect(r.writer, r.request, path, http.StatusFound)
|
http.Redirect(r.writer, r.request, path, http.StatusFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NotModified sends a response with a 304 status code.
|
||||||
|
func (r *Response) NotModified() {
|
||||||
|
r.writer.WriteHeader(http.StatusNotModified)
|
||||||
|
}
|
||||||
|
|
||||||
// Cache returns a response with caching headers.
|
// Cache returns a response with caching headers.
|
||||||
func (r *Response) Cache(mimeType, etag string, content []byte, duration time.Duration) {
|
func (r *Response) Cache(mimeType, etag string, content []byte, duration time.Duration) {
|
||||||
r.writer.Header().Set("Content-Type", mimeType)
|
r.writer.Header().Set("Content-Type", mimeType)
|
||||||
|
|
|
@ -7,15 +7,23 @@ package controller
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/miniflux/miniflux2/helper"
|
|
||||||
"github.com/miniflux/miniflux2/server/core"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
|
||||||
"time"
|
"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) {
|
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", "")
|
encodedURL := request.StringParam("encodedURL", "")
|
||||||
if encodedURL == "" {
|
if encodedURL == "" {
|
||||||
response.HTML().BadRequest(errors.New("No URL provided"))
|
response.HTML().BadRequest(errors.New("No URL provided"))
|
||||||
|
@ -28,22 +36,21 @@ func (c *Controller) ImageProxy(ctx *core.Context, request *core.Request, respon
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := http.Get(string(decodedURL))
|
client := http.NewClient(string(decodedURL))
|
||||||
|
resp, err := client.Get()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println("[ImageProxy]", err)
|
||||||
response.HTML().NotFound()
|
response.HTML().NotFound()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.HasServerFailure() {
|
||||||
response.HTML().NotFound()
|
response.HTML().NotFound()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
body, _ := ioutil.ReadAll(resp.Body)
|
body, _ := ioutil.ReadAll(resp.Body)
|
||||||
etag := helper.HashFromBytes(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)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue