Commit 9eafabb

Richard Luby <richluby@gmail.com>
2016-11-01 09:13:48
modified file writing scheme
server now writes files with the filname stored as <numQuestions>-<score>-<id>
1 parent 7c246e6
Changed files (1)
serverHandlers.go
@@ -31,6 +31,7 @@ func handleRequestForTest(writer http.ResponseWriter, request *http.Request) err
 }
 
 // writeTestFile writes the the data to the given writer for the specified username
+// files are written in the format <numQuestions>-<score>-<id>
 func writeTestFile(clientTest ClientTest, data []byte) error {
 	var err error
 	resultsFilePath := filepath.Join(serverConfig.USER_TESTS, clientTest.Username)
@@ -38,10 +39,13 @@ func writeTestFile(clientTest ClientTest, data []byte) error {
 		log.Printf("Could not create test directory for user %s: %+v", clientTest.Username, err)
 		return err
 	}
+	resultsFilePath = filepath.Join(resultsFilePath,
+		strconv.Itoa(len(clientTest.Records))+
+			"-"+strconv.Itoa(clientTest.Score))
 	id := fmt.Sprintf("%05d", rand.Intn(99999)) // generate random 5-digit id
 	for fileNeedsWrite := true; fileNeedsWrite; {
-		if err = ioutil.WriteFile(filepath.Join(resultsFilePath, id), data, 0400); err != nil {
-			id = fmt.Sprint(rand.Intn(99999))
+		if err = ioutil.WriteFile(resultsFilePath+"-"+id, data, 0400); err != nil {
+			id = strconv.Itoa(rand.Intn(99999))
 			continue
 		}
 		fileNeedsWrite = false