aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--models/item/item.go24
-rw-r--r--web/web.go26
2 files changed, 50 insertions, 0 deletions
diff --git a/models/item/item.go b/models/item/item.go
index 77ee02c..1e7e86a 100644
--- a/models/item/item.go
+++ b/models/item/item.go
@@ -3,11 +3,14 @@ package item
import (
"adammathes.com/neko/models"
"adammathes.com/neko/vlog"
+ "encoding/base64"
"fmt"
+ "github.com/PuerkitoBio/goquery"
"github.com/advancedlogic/GoOse"
"github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday"
"log"
+ "strings"
)
type Item struct {
@@ -176,6 +179,7 @@ func Filter(max_id int64, feed_id int64, category string, unread_only bool, star
// but still may need to adjust rules
i.Title = p.Sanitize(i.Title)
i.Description = p.Sanitize(i.Description)
+ i.Description = rewriteImages(i.Description)
i.Url = p.Sanitize(i.Url)
i.FeedTitle = p.Sanitize(i.FeedTitle)
i.FeedUrl = p.Sanitize(i.FeedUrl)
@@ -196,3 +200,23 @@ func (i *Item) CleanHeaderImage() {
i.HeaderImage = ""
}
}
+
+func rewriteImages(s string) string {
+ doc, err := goquery.NewDocumentFromReader(strings.NewReader(s))
+ if err != nil {
+ panic("can not parse doc to rewrite")
+ }
+
+ doc.Find("img").Each(func(i int, img *goquery.Selection) {
+ if src, ok := img.Attr("src"); ok {
+ img.SetAttr("src", proxyURL(src))
+ }
+ })
+
+ output, _ := doc.Html()
+ return output
+}
+
+func proxyURL(url string) string {
+ return "/image/" + base64.URLEncoding.EncodeToString([]byte(url))
+}
diff --git a/web/web.go b/web/web.go
index 0dd0025..d871990 100644
--- a/web/web.go
+++ b/web/web.go
@@ -5,9 +5,11 @@ import (
"adammathes.com/neko/crawler"
"adammathes.com/neko/models/feed"
"adammathes.com/neko/models/item"
+ "encoding/base64"
"encoding/json"
"fmt"
"golang.org/x/crypto/bcrypt"
+ "io/ioutil"
"log"
"net/http"
"path"
@@ -128,6 +130,29 @@ func categoryHandler(w http.ResponseWriter, r *http.Request) {
}
}
+func imageProxyHandler(w http.ResponseWriter, r *http.Request) {
+ // caching...???
+
+ imgURL := r.URL.String()
+ fmt.Printf("proxying: %s\n", imgURL)
+ decodedURL, err := base64.URLEncoding.DecodeString(imgURL)
+ fmt.Printf("decoded: %s\n", decodedURL)
+
+ resp, err := http.Get(string(decodedURL))
+ if err != nil {
+ http.Error(w, "sorry", 404)
+ return
+ }
+
+ bts, _ := ioutil.ReadAll(resp.Body)
+
+ etag := crypto.HashFromBytes(body)
+
+ w.Header().Set("Content-Type", "application/json")
+ w.Write(bts)
+ return
+}
+
var AuthCookie = "auth"
var SecondsInAYear = 60 * 60 * 24 * 365
@@ -186,6 +211,7 @@ func Serve() {
http.HandleFunc("/item/", AuthWrap(itemHandler))
http.HandleFunc("/feed/", AuthWrap(feedHandler))
http.HandleFunc("/tag/", AuthWrap(categoryHandler))
+ http.Handle("/image/", http.StripPrefix("/image/", AuthWrap(imageProxyHandler)))
http.HandleFunc("/login/", loginHandler)
http.HandleFunc("/logout/", logoutHandler)