package main import ( "encoding/base64" "encoding/binary" "encoding/json" "fmt" "html" "io/ioutil" "log" "math/rand" "os" "strconv" "golang.org/x/crypto/blake2b" "srs.epita.fr/fic-server/libfic" ) type ResponsesUpload struct { Keys map[int64]string `json:"flags"` MCQs map[int64]bool `json:"mcqs"` MCQJ map[int64]string `json:"justifications"` } func treatSubmission(pathname string, team fic.Team, exercice_id string) { // Generate a unique identifier to follow the request in logs bid := make([]byte, 5) binary.LittleEndian.PutUint32(bid, rand.Uint32()) id := "[" + base64.StdEncoding.EncodeToString(bid) + "]" log.Println(id, "New submission receive", pathname) // Parse exercice_id argument eid, err := strconv.ParseInt(exercice_id, 10, 64) if err != nil { log.Printf("%s [ERR] %s is not a valid number: %s\n", id, exercice_id, err) return } // Find the given exercice exercice, err := fic.GetExercice(eid) if err != nil { log.Printf("%s [ERR] Unable to find exercice %d: %s\n", id, eid, err) return } // Check the team can access this exercice if !team.HasAccess(exercice) { log.Println("[!!!] The team submits something for an exercice it doesn't have access yet") return } // Find the corresponding theme theme, err := fic.GetTheme(exercice.IdTheme) if err != nil { log.Printf("%s [ERR] Unable to retrieve theme for exercice %d: %s\n", id, eid, err) return } // Read received file cnt_raw, err := ioutil.ReadFile(pathname) if err != nil { log.Println(id, "[ERR] Unable to read file;", err) return } // Save checksum to treat duplicates cksum := blake2b.Sum512(cnt_raw) if err != nil { log.Println(id, "[ERR] JSON parsing error:", err) return } // Parse it var responses ResponsesUpload err = json.Unmarshal(cnt_raw, &responses) if err != nil { log.Println(id, "[ERR] JSON parsing error:", err) return } // Ensure the team didn't already solve this exercice s, tm := team.HasSolved(exercice) if s { log.Printf("%s [WRN] Team %d ALREADY solved exercice %d (%s : %s)\n", id, team.Id, exercice.Id, theme.Name, exercice.Title) return } // Handle MCQ justifications: convert to expected keyid for cid, j := range responses.MCQJ { if mcq, choice, err := fic.GetMCQbyChoice(cid); err != nil { log.Println(id, "[ERR] Unable to retrieve mcq from justification:", err) return } else if mcq.IdExercice != exercice.Id { log.Println(id, "[ERR] We retrieve an invalid MCQ: from exercice", mcq.IdExercice, "whereas expected from exercice", exercice.Id) return } else if key, err := choice.GetJustifiedFlag(exercice); err != nil { // Most probably, we enter here because the selected choice has not to be justified // So, just ignore this case as it will be invalidated by the mcq validation continue } else { if responses.Keys == nil { responses.Keys = map[int64]string{} } responses.Keys[key.Id] = j } } // Check given answers solved, err := exercice.CheckResponse(cksum[:], responses.Keys, responses.MCQs, team) if err != nil { log.Println(id, "[ERR] Unable to CheckResponse:", err) genTeamQueue <- &team return } // At this point, we have treated the file, so it can be safely deleted if err := os.Remove(pathname); err != nil { log.Println(id, "[ERR] Can't remove file:", err) } if solved { log.Printf("%s Team %d SOLVED exercice %d (%s : %s)\n", id, team.Id, exercice.Id, theme.Name, exercice.Title) if err := exercice.Solved(team); err != nil { log.Println(id, "[ERR] Unable to mark the challenge as solved:", err) } // Write event if lvl, err := exercice.GetLevel(); err != nil { log.Println(id, "[ERR] Unable to get exercice level:", err) } else if _, err := fic.NewEvent(fmt.Sprintf("L'équipe %s a résolu le %de défi %s !", html.EscapeString(team.Name), lvl, theme.Name), "success"); err != nil { log.Println(id, "[WRN] Unable to create event:", err) } genTeamQueue <- &team appendGenQueue(genStruct{id, GenThemes}) appendGenQueue(genStruct{id, GenTeams}) } else { log.Printf("%s Team %d submit an invalid solution for exercice %d (%s : %s)\n", id, team.Id, exercice.Id, theme.Name, exercice.Title) // Write event (only on first try) if tm.Unix() == 0 { if lvl, err := exercice.GetLevel(); err != nil { log.Println(id, "[ERR] Unable to get exercice level:", err) } else if _, err := fic.NewEvent(fmt.Sprintf("L'équipe %s tente le %de défi %s !", html.EscapeString(team.Name), lvl, theme.Name), "warning"); err != nil { log.Println(id, "[WRN] Unable to create event:", err) } } genTeamQueue <- &team } appendGenQueue(genStruct{id, GenEvents}) }