147 lines
3.3 KiB
Go
147 lines
3.3 KiB
Go
package handler
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/fogleman/gg"
|
|
"github.com/golang/freetype"
|
|
"github.com/golang/freetype/truetype"
|
|
"github.com/gorilla/mux"
|
|
"golang.org/x/image/font"
|
|
"golang.org/x/image/font/gofont/goregular"
|
|
"golang.org/x/image/math/fixed"
|
|
"image"
|
|
"image/color"
|
|
"image/draw"
|
|
//"image/png"
|
|
"net/http"
|
|
)
|
|
|
|
func (h *Handler) showUserView(w http.ResponseWriter, r *http.Request) {
|
|
user := mux.Vars(r)["user"]
|
|
if !h.storage.UserExists(user) {
|
|
notFound(w)
|
|
return
|
|
}
|
|
statuses, err := h.storage.StatusByUsername(mux.Vars(r)["user"], 20, 0)
|
|
if err != nil {
|
|
serverError(w, err)
|
|
return
|
|
}
|
|
h.renderLayout(w, "user", map[string]interface{}{
|
|
"user": user,
|
|
"statuses": statuses,
|
|
}, "")
|
|
}
|
|
|
|
type statusjson struct {
|
|
Author string `json:"author"`
|
|
Content string `json:"content"`
|
|
TimeAgo string `json:"timeAgo"`
|
|
}
|
|
|
|
func (h *Handler) showUserStatusView(w http.ResponseWriter, r *http.Request) {
|
|
user := mux.Vars(r)["user"]
|
|
if !h.storage.UserExists(user) {
|
|
notFound(w)
|
|
return
|
|
}
|
|
statuses, err := h.storage.StatusByUsername(mux.Vars(r)["user"], 1, 0)
|
|
if err != nil {
|
|
serverError(w, err)
|
|
return
|
|
}
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
w.Header().Set("Content-Type", "application/json")
|
|
var res statusjson
|
|
if len(statuses) > 0 {
|
|
res.Author = statuses[0].User
|
|
res.Content = statuses[0].Content
|
|
res.TimeAgo = statuses[0].TimeAgo()
|
|
}
|
|
json.NewEncoder(w).Encode(res)
|
|
}
|
|
|
|
func drawText(canvas *image.RGBA, text string) error {
|
|
var (
|
|
fgColor image.Image
|
|
fontFace *truetype.Font
|
|
err error
|
|
fontSize = 12.0
|
|
)
|
|
fgColor = image.White
|
|
fontFace, err = freetype.ParseFont(goregular.TTF)
|
|
fontDrawer := &font.Drawer{
|
|
Dst: canvas,
|
|
Src: fgColor,
|
|
Face: truetype.NewFace(fontFace, &truetype.Options{
|
|
Size: fontSize,
|
|
Hinting: font.HintingFull,
|
|
}),
|
|
}
|
|
//textBounds, _ := fontDrawer.BoundString(text)
|
|
xPosition := fixed.I(20)
|
|
//textHeight := textBounds.Max.Y - textBounds.Min.Y
|
|
yPosition := fixed.I(20)
|
|
fontDrawer.Dot = fixed.Point26_6{
|
|
X: xPosition,
|
|
Y: yPosition,
|
|
}
|
|
fontDrawer.DrawString(text)
|
|
return err
|
|
}
|
|
|
|
func createAvatar(text string) *image.RGBA {
|
|
bgColor := color.RGBA{
|
|
R: 255,
|
|
G: 20,
|
|
B: 100,
|
|
A: 255,
|
|
}
|
|
background := image.NewRGBA(image.Rect(0, 0, 200, 100))
|
|
draw.Draw(background, background.Bounds(), &image.Uniform{C: bgColor},
|
|
image.Point{}, draw.Src)
|
|
drawText(background, text)
|
|
return background
|
|
}
|
|
|
|
func (h *Handler) showUserStatusImageView(w http.ResponseWriter, r *http.Request) {
|
|
user := mux.Vars(r)["user"]
|
|
if !h.storage.UserExists(user) {
|
|
notFound(w)
|
|
return
|
|
}
|
|
statuses, err := h.storage.StatusByUsername(mux.Vars(r)["user"], 1, 0)
|
|
if err != nil {
|
|
serverError(w, err)
|
|
return
|
|
}
|
|
//w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
//w.Header().Set("Content-Type", "application/json")
|
|
var text string
|
|
if len(statuses) > 0 {
|
|
text = statuses[0].Content
|
|
}
|
|
|
|
background := "#ffffff"
|
|
foreground := "#000000"
|
|
|
|
if c := r.URL.Query().Get("background"); c != "" {
|
|
background = "#" + c
|
|
}
|
|
|
|
if c := r.URL.Query().Get("foreground"); c != "" {
|
|
foreground = "#" + c
|
|
}
|
|
|
|
width := 350
|
|
dc := gg.NewContext(width, 80)
|
|
dc.SetHexColor(background)
|
|
dc.Clear()
|
|
dc.SetHexColor(foreground)
|
|
dc.DrawStringWrapped(text, 19, 16, 0, 0, float64(width-(2*16)), 1.5, gg.AlignLeft)
|
|
dc.EncodePNG(w)
|
|
//dc.SavePNG("out.png")
|
|
|
|
//avatar := createAvatar(text)
|
|
//png.Encode(w, avatar)
|
|
}
|