ictgj-voting/admin_games.go

77 lines
1.8 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 (
2017-06-15 17:35:53 +00:00
"fmt"
"io"
"mime/multipart"
2017-06-08 20:43:11 +00:00
"net/http"
2017-06-15 17:35:53 +00:00
"os"
"strconv"
2017-06-08 20:43:11 +00:00
"github.com/gorilla/mux"
)
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 {
Games []Game
}
page.TemplateData = gamesPageData{Games: dbGetAllGames()}
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)
}
}
}
func saveScreenshots(teamId string, req *http.Request) error {
err := req.ParseMultipartForm((1 << 10) * 24)
if err != nil {
return err
}
for _, fheaders := range req.MultipartForm.File {
for _, hdr := range fheaders {
// open uploaded
var infile multipart.File
if infile, err = hdr.Open(); err != nil {
return err
}
// open destination
var outfile *os.File
if outfile, err = os.Create("./uploaded/" + hdr.Filename); err != nil {
return err
}
// 32K buffer copy
var written int64
if written, err = io.Copy(outfile, infile); err != nil {
return err
2017-06-08 20:43:11 +00:00
}
2017-06-15 17:35:53 +00:00
fmt.Println("uploaded file:" + hdr.Filename + ";length:" + strconv.Itoa(int(written)))
2017-06-08 20:43:11 +00:00
}
}
2017-06-15 17:35:53 +00:00
return nil
2017-06-08 17:20:43 +00:00
}