proxy.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package controller
  5. import (
  6. "encoding/base64"
  7. "errors"
  8. "github.com/miniflux/miniflux2/helper"
  9. "github.com/miniflux/miniflux2/server/core"
  10. "io/ioutil"
  11. "log"
  12. "net/http"
  13. "time"
  14. )
  15. func (c *Controller) ImageProxy(ctx *core.Context, request *core.Request, response *core.Response) {
  16. encodedURL := request.StringParam("encodedURL", "")
  17. if encodedURL == "" {
  18. response.HTML().BadRequest(errors.New("No URL provided"))
  19. return
  20. }
  21. decodedURL, err := base64.StdEncoding.DecodeString(encodedURL)
  22. if err != nil {
  23. response.HTML().BadRequest(errors.New("Unable to decode this URL"))
  24. return
  25. }
  26. resp, err := http.Get(string(decodedURL))
  27. if err != nil {
  28. log.Println(err)
  29. response.HTML().NotFound()
  30. return
  31. }
  32. defer resp.Body.Close()
  33. if resp.StatusCode != http.StatusOK {
  34. response.HTML().NotFound()
  35. return
  36. }
  37. body, _ := ioutil.ReadAll(resp.Body)
  38. etag := helper.HashFromBytes(body)
  39. contentType := resp.Header.Get("Content-Type")
  40. response.Cache(contentType, etag, body, 72*time.Hour)
  41. }