ictgj-voting/admin_games.go

141 lines
3.9 KiB
Go
Raw Normal View History

2017-06-08 17:20:43 +00:00
package main
2017-06-08 20:43:11 +00:00
import (
"bytes"
"encoding/base64"
"errors"
"image"
"image/gif"
"image/jpeg"
2017-06-08 20:43:11 +00:00
"net/http"
2017-07-14 12:28:09 +00:00
"strings"
2017-06-21 20:07:17 +00:00
2017-06-08 20:43:11 +00:00
"github.com/gorilla/mux"
"github.com/nfnt/resize"
2017-06-08 20:43:11 +00:00
)
2017-06-08 17:20:43 +00:00
func handleAdminGames(w http.ResponseWriter, req *http.Request, page *pageData) {
2017-06-08 20:43:11 +00:00
vars := mux.Vars(req)
page.SubTitle = "Games"
2017-06-15 17:35:53 +00:00
teamId := vars["id"]
if teamId == "" {
// Games List
page.TemplateData = m.jam
2017-06-15 17:35:53 +00:00
page.SubTitle = "Games"
page.show("admin-games.html", w)
} else {
2017-10-19 19:53:07 +00:00
tm, _ := m.jam.GetTeamById(teamId)
if tm != nil {
switch vars["function"] {
case "save":
2017-10-19 19:53:07 +00:00
var err error
var gm *Game
if gm, err = NewGame(tm.UUID); err != nil {
page.session.setFlashMessage("Error updating game: "+err.Error(), "error")
}
gm.Name = req.FormValue("gamename")
gm.Link = req.FormValue("gamelink")
gm.Description = req.FormValue("gamedesc")
2017-10-18 22:18:12 +00:00
if err := m.jam.UpdateGame(tm.UUID, gm); err != nil {
2017-06-15 17:35:53 +00:00
page.session.setFlashMessage("Error updating game: "+err.Error(), "error")
} else {
page.session.setFlashMessage("Team game updated", "success")
2017-06-08 20:43:11 +00:00
}
redirect("/admin/teams/"+tm.UUID+"#game", w, req)
case "screenshotupload":
var ss *Screenshot
tm, err := m.jam.GetTeamById(tm.UUID)
if err != nil {
page.session.setFlashMessage("Error updating game: "+err.Error(), "error")
redirect("/admin/teams/"+tm.UUID+"#game", w, req)
}
ss, err = ssFromRequest(tm, req)
if err != nil {
page.session.setFlashMessage("Error updating game: "+err.Error(), "error")
redirect("/admin/teams/"+tm.UUID+"#game", w, req)
}
gm := tm.Game
gm.Screenshots = append(gm.Screenshots, *ss)
if err = m.jam.UpdateGame(tm.UUID, gm); err != nil {
page.session.setFlashMessage("Error updating game: "+err.Error(), "error")
} else {
page.session.setFlashMessage("Screenshot Uploaded", "success")
}
redirect("/admin/teams/"+tm.UUID+"#game", w, req)
case "screenshotdelete":
2017-10-19 19:53:07 +00:00
var err error
ssid := vars["subid"]
tm, err := m.jam.GetTeamById(tm.UUID)
if err != nil {
page.session.setFlashMessage("Error updating game: "+err.Error(), "error")
redirect("/admin/teams/"+tm.UUID+"#game", w, req)
break
}
gm := tm.Game
if err = gm.RemoveScreenshot(ssid); err != nil {
page.session.setFlashMessage("Error updating game: "+err.Error(), "error")
redirect("/admin/teams/"+tm.UUID+"#game", w, req)
break
2017-10-19 19:53:07 +00:00
}
if err = m.jam.UpdateGame(tm.UUID, gm); err != nil {
page.session.setFlashMessage("Error updating game: "+err.Error(), "error")
} else {
page.session.setFlashMessage("Screenshot Removed", "success")
}
redirect("/admin/teams/"+tm.UUID+"#game", w, req)
}
} else {
page.session.setFlashMessage("Not a valid team id", "error")
redirect("/admin/teams", w, req)
2017-06-15 17:35:53 +00:00
}
}
}
func ssFromRequest(tm *Team, req *http.Request) (*Screenshot, error) {
2017-07-14 12:28:09 +00:00
var err error
2017-10-19 19:53:07 +00:00
var ss *Screenshot
2017-07-14 12:28:09 +00:00
file, hdr, err := req.FormFile("newssfile")
2017-06-15 17:35:53 +00:00
if err != nil {
return nil, err
2017-06-15 17:35:53 +00:00
}
2017-07-14 12:28:09 +00:00
extIdx := strings.LastIndex(hdr.Filename, ".")
fltp := "png"
if len(hdr.Filename) > extIdx {
fltp = hdr.Filename[extIdx+1:]
}
2017-10-19 19:53:07 +00:00
mI, _, err := image.Decode(file)
buf := new(bytes.Buffer)
// We convert everything to jpg
2017-10-19 19:53:07 +00:00
if err = jpeg.Encode(buf, mI, nil); err != nil {
return nil, errors.New("Unable to encode image")
}
2017-10-19 19:53:07 +00:00
thm := resize.Resize(200, 0, mI, resize.Lanczos3)
thmBuf := new(bytes.Buffer)
var thmString string
if fltp == "gif" {
if err = gif.Encode(thmBuf, thm, nil); err != nil {
return nil, errors.New("Unable to encode image")
}
} else {
if err = jpeg.Encode(thmBuf, thm, nil); err != nil {
return nil, errors.New("Unable to encode image")
}
}
thmString = base64.StdEncoding.EncodeToString(thmBuf.Bytes())
2017-10-19 19:53:07 +00:00
if ss, err = NewScreenshot(tm.UUID, ""); err != nil {
return nil, err
2017-10-19 19:53:07 +00:00
}
ss.Image = base64.StdEncoding.EncodeToString(buf.Bytes())
ss.Thumbnail = thmString
ss.Filetype = fltp
return ss, nil
//return m.jam.SaveScreenshot(ss)
2017-06-08 17:20:43 +00:00
}