2019-02-23 11:20:55 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-02-14 08:58:52 +08:00
|
|
|
"fmt"
|
2020-02-14 09:15:05 +08:00
|
|
|
"image/jpeg"
|
2019-02-23 11:20:55 +08:00
|
|
|
"io"
|
|
|
|
"net/http"
|
2020-02-14 09:15:05 +08:00
|
|
|
"strings"
|
2020-02-14 08:58:52 +08:00
|
|
|
|
|
|
|
"github.com/disintegration/imaging"
|
2019-02-23 11:20:55 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
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" {
|
2020-02-14 08:58:52 +08:00
|
|
|
if strings.HasSuffix(url, "photo.jpg") {
|
|
|
|
url += "?sz=38"
|
|
|
|
} else {
|
|
|
|
url += "=s38"
|
|
|
|
}
|
2019-02-23 11:20:55 +08:00
|
|
|
} else if c.Provider == "github" {
|
2020-02-14 08:58:52 +08:00
|
|
|
url += "&s=38"
|
2019-02-23 11:20:55 +08:00
|
|
|
} else if c.Provider == "twitter" {
|
|
|
|
url += "?size=normal"
|
|
|
|
} else if c.Provider == "gitlab" {
|
2020-02-14 08:58:52 +08:00
|
|
|
url += "?width=38"
|
2019-02-23 11:20:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2020-02-14 08:58:52 +08:00
|
|
|
if c.Provider != "commento" { // Custom URL avatars need to be resized.
|
|
|
|
io.Copy(w, resp.Body)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Limit the size of the response to 128 KiB to prevent DoS attacks
|
|
|
|
// that exhaust memory.
|
|
|
|
limitedResp := &io.LimitedReader{R: resp.Body, N: 128 * 1024}
|
|
|
|
|
|
|
|
img, err := jpeg.Decode(limitedResp)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(w, "JPEG decode failed: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = imaging.Encode(w, imaging.Resize(img, 38, 0, imaging.Lanczos), imaging.JPEG); err != nil {
|
|
|
|
fmt.Fprintf(w, "image encoding failed: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
2019-02-23 11:20:55 +08:00
|
|
|
}
|