Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate pgn #45

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/hashicorp/go-version"
"github.com/jinzhu/gorm"
"github.com/notnil/chess"
)

func checkUser(c *gin.Context) (*db.User, *db.Client, uint64, error) {
Expand Down Expand Up @@ -595,6 +596,20 @@ func checkPermissionExpr(expr string, user db.User, trainingRunId uint64, engine
return value.(bool)
}

func validatePgn(file string) error {
data, err := os.Open(file)
if err != nil {
log.Println("readfile error in file %v", file)
log.Println(err)
return err
}

pgn, err := chess.PGN(data)
if pgn != nil {}

return err
}

func uploadGame(c *gin.Context) {
user, client, version, err := checkUser(c)
if err != nil {
Expand Down Expand Up @@ -724,6 +739,15 @@ func uploadGame(c *gin.Context) {
return
}

if config.Config.Clients.VerifyPgns {
parsingErr := validatePgn(game_path)
if parsingErr != nil {
fmt.Println("corrupt pgn file exited with error: %v", parsingErr)
c.String(500, "internal error")
return
}
}

// Save pgn
pgn_path := fmt.Sprintf("pgns/run%d/%d.pgn", training_run.ID, nextGameNumber)
os.MkdirAll(filepath.Dir(pgn_path), os.ModePerm)
Expand Down
1 change: 1 addition & 0 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var Config struct {
NextClientVersion uint64
MinEngineVersion string
NextEngineVersion string
VerifyPgns bool
}
URLs struct {
OnNewNetwork []string
Expand Down