db: Add a published attribute, filled by challenge.txt

This commit is contained in:
nemunaire 2022-10-31 18:52:29 +01:00
commit a28f108b8a
7 changed files with 75 additions and 19 deletions

View file

@ -5,6 +5,7 @@ import (
"fmt"
"log"
"net/http"
"path/filepath"
"strconv"
"srs.epita.fr/fic-server/admin/sync"
@ -166,8 +167,14 @@ func createExerciceFile(c *gin.Context) {
return
}
paramsFiles, err := sync.GetExerciceFilesParams(sync.GlobalImporter, exercice.(*fic.Exercice))
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
var uf uploadedFile
err := c.ShouldBindJSON(&uf)
err = c.ShouldBindJSON(&uf)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
@ -178,7 +185,12 @@ func createExerciceFile(c *gin.Context) {
if digest, err := hex.DecodeString(uf.Digest); err != nil {
return nil, err
} else {
return exercice.(*fic.Exercice).ImportFile(filePath, origin, digest, nil)
published := true
if f, exists := paramsFiles[filepath.Base(filePath)]; exists {
published = !f.Hidden
}
return exercice.(*fic.Exercice).ImportFile(filePath, origin, digest, nil, published)
}
})
if err != nil {

View file

@ -103,6 +103,10 @@
Taille&nbsp;: <span title="{{ file.size }} octets">{{ file.size | size }}</span> &dash;
BLAKE2b&nbsp;: <samp title="{{ file.checksum | cksum }}">{{ file.checksum | cksum }}</samp>
</div>
<div class="col-auto custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" ng-model="file.published" id="f{{file.id}}">
<label class="custom-control-label" for="f{{file.id}}">Publié aux équipes</label>
</div>
<div>
Dépend de la validation de :
<span ng-if="!file.depends">aucun flag</span>

View file

@ -28,6 +28,12 @@ type ExerciceUnlockFile struct {
Filename string `toml:",omitempty"`
}
// ExerciceFile defines attributes on files.
type ExerciceFile struct {
Filename string `toml:",omitempty"`
Hidden bool `toml:",omitempty"`
}
// ExerciceFlag holds informations about one flag.
type ExerciceFlag struct {
Id int64
@ -66,6 +72,7 @@ type ExerciceFlagChoice struct {
type ExerciceParams struct {
Gain int64
Tags []string
Files []ExerciceFile `toml:"file"`
Hints []ExerciceHintParams `toml:"hint"`
Dependencies []ExerciceDependency `toml:"depend"`
Flags []ExerciceFlag `toml:"flag"`
@ -127,3 +134,17 @@ func getExerciceParams(i Importer, exercice *fic.Exercice) (params ExerciceParam
}
return
}
func GetExerciceFilesParams(i Importer, exercice *fic.Exercice) (map[string]ExerciceFile, error) {
params, _, err := parseExerciceParams(i, exercice.Path)
if err != nil {
return nil, err
}
paramsFiles := map[string]ExerciceFile{}
for _, f := range params.Files {
paramsFiles[f.Filename] = f
}
return paramsFiles, nil
}

View file

@ -134,6 +134,12 @@ func SyncExerciceFiles(i Importer, exercice *fic.Exercice, exceptions *CheckExce
errs = append(errs, err)
}
paramsFiles, err := GetExerciceFilesParams(i, exercice)
if err != nil {
errs = append(errs, NewChallengeTxtError(exercice, 0, err))
return
}
files, digests, berrs := BuildFilesListInto(i, exercice, "files")
errs = append(errs, berrs...)
@ -147,7 +153,13 @@ func SyncExerciceFiles(i Importer, exercice *fic.Exercice, exceptions *CheckExce
digest_shown = d
}
}
return exercice.ImportFile(filePath, origin, digests[fname], digest_shown)
published := true
if f, exists := paramsFiles[fname]; exists {
published = !f.Hidden
}
return exercice.ImportFile(filePath, origin, digests[fname], digest_shown, published)
}); err != nil {
errs = append(errs, NewFileError(exercice, fname, err))
continue

View file

@ -170,6 +170,7 @@ CREATE TABLE IF NOT EXISTS exercice_files(
cksum BINARY(64) NOT NULL,
cksum_shown BINARY(64),
size BIGINT UNSIGNED NOT NULL,
published BOOLEAN NOT NULL DEFAULT 1,
FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice)
) DEFAULT CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
`); err != nil {

View file

@ -44,6 +44,8 @@ type EFile struct {
ChecksumShown []byte `json:"checksum_shown"`
// Size contains the cached size of the file
Size int64 `json:"size"`
// Published indicates if the file should be shown or not
Published bool `json:"published"`
}
// NewDummyFile creates an EFile, without any link to an actual Exercice File.
@ -58,12 +60,13 @@ func (e *Exercice) NewDummyFile(origin string, dest string, checksum []byte, siz
Checksum: checksum,
ChecksumShown: nil,
Size: size,
Published: true,
}
}
// GetFiles returns a list of all files living in the database.
func GetFiles() ([]*EFile, error) {
if rows, err := DBQuery("SELECT id_file, id_exercice, origin, path, name, cksum, cksum_shown, size FROM exercice_files"); err != nil {
if rows, err := DBQuery("SELECT id_file, id_exercice, origin, path, name, cksum, cksum_shown, size, published FROM exercice_files"); err != nil {
return nil, err
} else {
defer rows.Close()
@ -71,7 +74,7 @@ func GetFiles() ([]*EFile, error) {
files := []*EFile{}
for rows.Next() {
f := &EFile{}
if err := rows.Scan(&f.Id, &f.IdExercice, &f.origin, &f.Path, &f.Name, &f.Checksum, &f.ChecksumShown, &f.Size); err != nil {
if err := rows.Scan(&f.Id, &f.IdExercice, &f.origin, &f.Path, &f.Name, &f.Checksum, &f.ChecksumShown, &f.Size, &f.Published); err != nil {
return nil, err
}
files = append(files, f)
@ -87,13 +90,13 @@ func GetFiles() ([]*EFile, error) {
// GetFile retrieves the file with the given id.
func GetFile(id int64) (f *EFile, err error) {
f = &EFile{}
err = DBQueryRow("SELECT id_file, origin, path, name, cksum, cksum_shown, size FROM exercice_files WHERE id_file = ?", id).Scan(&f.Id, &f.origin, &f.Path, &f.Name, &f.Checksum, &f.ChecksumShown, &f.Size)
err = DBQueryRow("SELECT id_file, origin, path, name, cksum, cksum_shown, size, published FROM exercice_files WHERE id_file = ?", id).Scan(&f.Id, &f.origin, &f.Path, &f.Name, &f.Checksum, &f.ChecksumShown, &f.Size, &f.Published)
return
}