api: mirror user photos for better privacy

This commit is contained in:
Adhityaa Chandrasekar 2019-02-22 22:20:55 -05:00
parent 95093326e0
commit d4b466b04f
3 changed files with 37 additions and 18 deletions

34
api/commenter_photo.go Normal file
View File

@ -0,0 +1,34 @@
package main
import (
"io"
"net/http"
)
func commenterPhotoHandler(w http.ResponseWriter, r *http.Request) {
c, err := commenterGetByHex(r.FormValue("commenterHex"))
if err != nil {
http.NotFound(w, r)
return
}
url := c.Photo
if c.Provider == "google" {
url += "?sz=50"
} else if c.Provider == "github" {
url += "&s=50"
} else if c.Provider == "twitter" {
url += "?size=normal"
} else if c.Provider == "gitlab" {
url += "?width=50"
}
resp, err := http.Get(url)
if err != nil {
http.NotFound(w, r)
return
}
defer resp.Body.Close()
io.Copy(w, resp.Body)
}

View File

@ -27,6 +27,7 @@ func apiRouterInit(router *mux.Router) error {
router.HandleFunc("/api/commenter/new", commenterNewHandler).Methods("POST") router.HandleFunc("/api/commenter/new", commenterNewHandler).Methods("POST")
router.HandleFunc("/api/commenter/login", commenterLoginHandler).Methods("POST") router.HandleFunc("/api/commenter/login", commenterLoginHandler).Methods("POST")
router.HandleFunc("/api/commenter/self", commenterSelfHandler).Methods("POST") router.HandleFunc("/api/commenter/self", commenterSelfHandler).Methods("POST")
router.HandleFunc("/api/commenter/photo", commenterPhotoHandler).Methods("GET")
router.HandleFunc("/api/email/get", emailGetHandler).Methods("POST") router.HandleFunc("/api/email/get", emailGetHandler).Methods("POST")
router.HandleFunc("/api/email/update", emailUpdateHandler).Methods("POST") router.HandleFunc("/api/email/update", emailUpdateHandler).Methods("POST")

View File

@ -258,15 +258,7 @@
classAdd(avatar, "avatar"); classAdd(avatar, "avatar");
} else { } else {
avatar = create("img"); avatar = create("img");
if (commenter.provider === "google") { attrSet(avatar, "src", cdn + "/api/commenter/photo?commenterHex=" + commenter.commenterHex);
attrSet(avatar, "src", commenter.photo + "?sz=50");
} else if (commenter.provider === "github") {
attrSet(avatar, "src", commenter.photo + "&s=50");
} else if (commenter.provider === "twitter") {
attrSet(avatar, "src", commenter.photo + "?size=normal");
} else {
attrSet(avatar, "src", commenter.photo);
}
classAdd(avatar, "avatar-img"); classAdd(avatar, "avatar-img");
} }
@ -743,15 +735,7 @@
classAdd(avatar, "avatar"); classAdd(avatar, "avatar");
} else { } else {
avatar = create("img"); avatar = create("img");
if (commenter.provider === "google") { attrSet(avatar, "src", cdn + "/api/commenter/photo?commenterHex=" + commenter.commenterHex);
attrSet(avatar, "src", commenter.photo + "?sz=50");
} else if (commenter.provider === "github") {
attrSet(avatar, "src", commenter.photo + "&s=50");
} else if (commenter.provider === "twitter") {
attrSet(avatar, "src", commenter.photo + "?size=normal");
} else {
attrSet(avatar, "src", commenter.photo);
}
classAdd(avatar, "avatar-img"); classAdd(avatar, "avatar-img");
} }