ictgj-voting/admin_games.go

118 lines
3.2 KiB
Go

package main
import (
"bytes"
"encoding/base64"
"errors"
"image"
"image/gif"
"image/jpeg"
"net/http"
"strings"
"github.com/gorilla/mux"
"github.com/nfnt/resize"
)
func handleAdminGames(w http.ResponseWriter, req *http.Request, page *pageData) {
vars := mux.Vars(req)
page.SubTitle = "Games"
teamId := vars["id"]
if teamId == "" {
// Games List
// TODO: We should be able to just pass m.jam to the template instead of a custom struct
type gamesPageData struct {
Teams []Team
}
gpd := new(gamesPageData)
gpd.Teams = m.jam.Teams
page.TemplateData = gpd
page.SubTitle = "Games"
page.show("admin-games.html", w)
} else {
tm, _ := m.jam.GetTeamById(teamId)
if tm != nil {
switch vars["function"] {
case "save":
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")
if err := m.jam.UpdateGame(tm.UUID, gm); err != nil {
page.session.setFlashMessage("Error updating game: "+err.Error(), "error")
} else {
page.session.setFlashMessage("Team game updated", "success")
}
redirect("/admin/teams/"+tm.UUID+"#game", w, req)
case "screenshotupload":
if err := saveScreenshots(tm, req); err != nil {
page.session.setFlashMessage("Error updating game: "+err.Error(), "error")
}
redirect("/admin/teams/"+tm.UUID+"#game", w, req)
case "screenshotdelete":
var ss *Screenshot
var err error
ssid := vars["subid"]
if ss, err = NewScreenshot(tm.UUID, ssid); err != nil {
page.session.setFlashMessage("Error deleting screenshot: "+err.Error(), "error")
}
if err = m.jam.DeleteScreenshot(ss); err != nil {
page.session.setFlashMessage("Error deleting screenshot: "+err.Error(), "error")
}
redirect("/admin/teams/"+tm.UUID+"#game", w, req)
}
} else {
page.session.setFlashMessage("Not a valid team id", "error")
redirect("/admin/teams", w, req)
}
}
}
func saveScreenshots(tm *Team, req *http.Request) error {
var err error
var ss *Screenshot
file, hdr, err := req.FormFile("newssfile")
if err != nil {
return err
}
extIdx := strings.LastIndex(hdr.Filename, ".")
fltp := "png"
if len(hdr.Filename) > extIdx {
fltp = hdr.Filename[extIdx+1:]
}
mI, _, err := image.Decode(file)
buf := new(bytes.Buffer)
// We convert everything to jpg
if err = jpeg.Encode(buf, mI, nil); err != nil {
return errors.New("Unable to encode image")
}
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 errors.New("Unable to encode image")
}
} else {
if err = jpeg.Encode(thmBuf, thm, nil); err != nil {
return errors.New("Unable to encode image")
}
}
thmString = base64.StdEncoding.EncodeToString(thmBuf.Bytes())
if ss, err = NewScreenshot(tm.UUID, ""); err != nil {
return err
}
ss.Image = base64.StdEncoding.EncodeToString(buf.Bytes())
ss.Thumbnail = thmString
ss.Filetype = fltp
return m.jam.SaveScreenshot(ss)
}