backend: add an identifier to each treated file

This commit is contained in:
nemunaire 2019-11-25 16:16:27 +01:00
parent 9693940d8c
commit 6ad11e49d5
5 changed files with 78 additions and 43 deletions

View file

@ -1,10 +1,13 @@
package main
import (
"encoding/base64"
"encoding/binary"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math/rand"
"os"
"regexp"
@ -17,24 +20,30 @@ func validTeamName(name string) bool {
}
func treatRename(pathname string, team fic.Team) {
// 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 renameTeam receive", pathname)
var keys map[string]string
if cnt_raw, err := ioutil.ReadFile(pathname); err != nil {
log.Println("[ERR]", err)
log.Printf("%s [ERR] %s\n", id, err)
} else if err := json.Unmarshal(cnt_raw, &keys); err != nil {
log.Println("[ERR]", err)
log.Printf("%s [ERR] %s\n", id, err)
} else if validTeamName(keys["newName"]) {
team.Name = keys["newName"]
if _, err := team.Update(); err != nil {
log.Println("[WRN] Unable to change team name:", err)
log.Printf("%s [WRN] Unable to change team name: %s\n", id, err)
}
genTeamQueue <- &team
if _, err := fic.NewEvent(fmt.Sprintf("Souhaitons bonne chance à l'équipe <strong>%s</strong> qui vient de nous rejoindre&#160;!", team.Name), "info"); err != nil {
log.Println("[WRN] Unable to create event:", err)
log.Printf("%s [WRN] Unable to create event: %s\n", id, err)
}
appendGenQueue(genStruct{Type: GenEvents})
if err := os.Remove(pathname); err != nil {
log.Println("[ERR]", err)
log.Printf("%s [ERR] %s\n", id, err)
}
}
}