ictgj-voting/admin_games.go

98 lines
2.5 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"
2017-07-14 12:28:09 +00:00
"fmt"
"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
type gamesPageData struct {
2017-06-30 19:35:36 +00:00
Teams []Team
}
gpd := new(gamesPageData)
2017-06-30 19:35:36 +00:00
gpd.Teams = dbGetAllTeams()
page.TemplateData = gpd
2017-06-15 17:35:53 +00:00
page.SubTitle = "Games"
page.show("admin-games.html", w)
} else {
2017-06-08 20:43:11 +00:00
switch vars["function"] {
case "save":
name := req.FormValue("gamename")
2017-06-15 17:35:53 +00:00
desc := req.FormValue("gamedesc")
2017-06-08 20:43:11 +00:00
if dbIsValidTeam(teamId) {
2017-06-15 17:35:53 +00:00
if err := dbUpdateTeamGame(teamId, name, desc); err != nil {
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
}
2017-06-15 17:35:53 +00:00
redirect("/admin/teams/"+teamId, w, req)
}
case "screenshotupload":
if err := saveScreenshots(teamId, req); err != nil {
page.session.setFlashMessage("Error updating game: "+err.Error(), "error")
}
redirect("/admin/teams/"+teamId, w, req)
case "screenshotdelete":
ssid := vars["subid"]
if err := dbDeleteTeamGameScreenshot(teamId, ssid); err != nil {
page.session.setFlashMessage("Error deleting screenshot: "+err.Error(), "error")
}
redirect("/admin/teams/"+teamId, w, req)
2017-06-15 17:35:53 +00:00
}
}
}
func saveScreenshots(teamId string, req *http.Request) error {
2017-07-14 12:28:09 +00:00
var err error
file, hdr, err := req.FormFile("newssfile")
2017-06-15 17:35:53 +00:00
if err != nil {
return err
}
2017-07-14 12:28:09 +00:00
fmt.Println("File Received: " + hdr.Filename)
extIdx := strings.LastIndex(hdr.Filename, ".")
fltp := "png"
if len(hdr.Filename) > extIdx {
fltp = hdr.Filename[extIdx+1:]
}
m, _, err := image.Decode(file)
buf := new(bytes.Buffer)
// We convert everything to jpg
if err = jpeg.Encode(buf, m, nil); err != nil {
return errors.New("Unable to encode image")
}
thm := resize.Resize(200, 0, m, 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())
2017-07-14 12:28:09 +00:00
return dbSaveTeamGameScreenshot(teamId, &Screenshot{
Image: base64.StdEncoding.EncodeToString(buf.Bytes()),
Thumbnail: thmString,
Filetype: fltp,
2017-07-14 12:28:09 +00:00
})
2017-06-08 17:20:43 +00:00
}