New attribute "disclaimer" on downloadable files

This commit is contained in:
nemunaire 2022-11-23 16:55:49 +01:00
parent c28ad9533b
commit bd19d31577
6 changed files with 80 additions and 22 deletions

View file

@ -186,11 +186,17 @@ func createExerciceFile(c *gin.Context) {
return nil, err return nil, err
} else { } else {
published := true published := true
disclaimer := ""
if f, exists := paramsFiles[filepath.Base(filePath)]; exists { if f, exists := paramsFiles[filepath.Base(filePath)]; exists {
published = !f.Hidden published = !f.Hidden
if disclaimer, err = sync.ProcessMarkdown(sync.GlobalImporter, f.Disclaimer, exercice.(*fic.Exercice).Path); err != nil {
return nil, fmt.Errorf("error during markdown formating of disclaimer: %w", err)
}
} }
return exercice.(*fic.Exercice).ImportFile(filePath, origin, digest, nil, published) return exercice.(*fic.Exercice).ImportFile(filePath, origin, digest, nil, disclaimer, published)
} }
}) })
if err != nil { if err != nil {

View file

@ -30,8 +30,9 @@ type ExerciceUnlockFile struct {
// ExerciceFile defines attributes on files. // ExerciceFile defines attributes on files.
type ExerciceFile struct { type ExerciceFile struct {
Filename string `toml:",omitempty"` Filename string `toml:",omitempty"`
Hidden bool `toml:",omitempty"` Hidden bool `toml:",omitempty"`
Disclaimer string `toml:",omitempty"`
} }
// ExerciceFlag holds informations about one flag. // ExerciceFlag holds informations about one flag.

View file

@ -143,7 +143,27 @@ func CheckExerciceFiles(i Importer, exercice *fic.Exercice, exceptions *CheckExc
} }
} }
file := exercice.NewDummyFile(path.Join(exercice.Path, "files", fname), getDestinationFilePath(path.Join(exercice.Path, "files", fname)), (*hash512).Sum(nil), digest_shown, size) paramsFiles, err := GetExerciceFilesParams(i, exercice)
if err != nil {
errs = append(errs, NewChallengeTxtError(exercice, 0, err))
return
}
disclaimer := ""
if f, exists := paramsFiles[fname]; exists {
// Call checks hooks
for _, hk := range hooks.mdTextHooks {
for _, err := range hk(f.Disclaimer, exceptions) {
errs = append(errs, NewFileError(exercice, fname, err))
}
}
if disclaimer, err = ProcessMarkdown(i, fixnbsp(f.Disclaimer), exercice.Path); err != nil {
errs = append(errs, NewFileError(exercice, fname, fmt.Errorf("error during markdown formating of disclaimer: %w", err)))
}
}
file := exercice.NewDummyFile(path.Join(exercice.Path, "files", fname), getDestinationFilePath(path.Join(exercice.Path, "files", fname)), (*hash512).Sum(nil), digest_shown, disclaimer, size)
// Call checks hooks // Call checks hooks
for _, h := range hooks.fileHooks { for _, h := range hooks.fileHooks {
@ -187,11 +207,23 @@ func SyncExerciceFiles(i Importer, exercice *fic.Exercice, exceptions *CheckExce
} }
published := true published := true
disclaimer := ""
if f, exists := paramsFiles[fname]; exists { if f, exists := paramsFiles[fname]; exists {
published = !f.Hidden published = !f.Hidden
// Call checks hooks
for _, hk := range hooks.mdTextHooks {
for _, err := range hk(f.Disclaimer, exceptions) {
errs = append(errs, NewFileError(exercice, fname, err))
}
}
if disclaimer, err = ProcessMarkdown(i, fixnbsp(f.Disclaimer), exercice.Path); err != nil {
errs = append(errs, NewFileError(exercice, fname, fmt.Errorf("error during markdown formating of disclaimer: %w", err)))
}
} }
return exercice.ImportFile(filePath, origin, digests[fname], digest_shown, published) return exercice.ImportFile(filePath, origin, digests[fname], digest_shown, disclaimer, published)
}); err != nil { }); err != nil {
errs = append(errs, NewFileError(exercice, fname, err)) errs = append(errs, NewFileError(exercice, fname, err))
continue continue

View file

@ -27,12 +27,17 @@
</CardBody> </CardBody>
<ListGroup class="border-dark"> <ListGroup class="border-dark">
{#each files as file, index} {#each files as file, index}
<ListGroupItem tag="a" href={file.path} target={(file.name.endsWith(".txt") || file.name.endsWith(".jpg") || file.name.endsWith(".png") || file.name.endsWith(".pdf"))?"_blank":"_self"} class="d-flex align-items-center"> <ListGroupItem tag="a" href={file.path} target={(file.name.endsWith(".txt") || file.name.endsWith(".jpg") || file.name.endsWith(".png") || file.name.endsWith(".pdf"))?"_blank":"_self"} class="d-flex">
<h1 class="me-3"> <h1 class="me-3">
<Icon name="arrow-down-circle" /> <Icon name="arrow-down-circle" />
</h1> </h1>
<div style="min-width: 0"> <div style="min-width: 0">
<h4 class="fw-bold"><samp>{file.name}</samp></h4> <h4 class="fw-bold"><samp>{file.name}</samp></h4>
{#if file.disclamer}
<div class="file-disclamer text-warning">
{file.disclamer}
</div>
{/if}
<nobr> <nobr>
Taille&nbsp;: Taille&nbsp;:
<FileSize size={file.size} /> <FileSize size={file.size} />
@ -47,3 +52,12 @@
</ListGroup> </ListGroup>
</Card> </Card>
{/if} {/if}
<style>
.file-disclamer {
display: none;
}
:global(.list-group-item:hover .file-disclamer) {
display: block;
}
</style>

View file

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

View file

@ -43,13 +43,15 @@ type EFile struct {
ChecksumShown []byte `json:"checksum_shown"` ChecksumShown []byte `json:"checksum_shown"`
// Size contains the cached size of the file // Size contains the cached size of the file
Size int64 `json:"size"` Size int64 `json:"size"`
// Disclaimer contains a string to display before downloading the content
Disclaimer string `json:"disclaimer"`
// Published indicates if the file should be shown or not // Published indicates if the file should be shown or not
Published bool `json:"published"` Published bool `json:"published"`
} }
// NewDummyFile creates an EFile, without any link to an actual Exercice File. // NewDummyFile creates an EFile, without any link to an actual Exercice File.
// It is used to check the file validity // It is used to check the file validity
func (e *Exercice) NewDummyFile(origin string, dest string, checksum []byte, checksumShown []byte, size int64) *EFile { func (e *Exercice) NewDummyFile(origin string, dest string, checksum []byte, checksumShown []byte, disclaimer string, size int64) *EFile {
return &EFile{ return &EFile{
Id: 0, Id: 0,
origin: origin, origin: origin,
@ -59,13 +61,14 @@ func (e *Exercice) NewDummyFile(origin string, dest string, checksum []byte, che
Checksum: checksum, Checksum: checksum,
ChecksumShown: checksumShown, ChecksumShown: checksumShown,
Size: size, Size: size,
Disclaimer: disclaimer,
Published: true, Published: true,
} }
} }
// GetFiles returns a list of all files living in the database. // GetFiles returns a list of all files living in the database.
func GetFiles() ([]*EFile, error) { func GetFiles() ([]*EFile, error) {
if rows, err := DBQuery("SELECT id_file, id_exercice, origin, path, name, cksum, cksum_shown, size, published FROM exercice_files"); err != nil { if rows, err := DBQuery("SELECT id_file, id_exercice, origin, path, name, cksum, cksum_shown, size, disclaimer, published FROM exercice_files"); err != nil {
return nil, err return nil, err
} else { } else {
defer rows.Close() defer rows.Close()
@ -73,7 +76,7 @@ func GetFiles() ([]*EFile, error) {
files := []*EFile{} files := []*EFile{}
for rows.Next() { for rows.Next() {
f := &EFile{} f := &EFile{}
if err := rows.Scan(&f.Id, &f.IdExercice, &f.origin, &f.Path, &f.Name, &f.Checksum, &f.ChecksumShown, &f.Size, &f.Published); err != nil { if err := rows.Scan(&f.Id, &f.IdExercice, &f.origin, &f.Path, &f.Name, &f.Checksum, &f.ChecksumShown, &f.Size, &f.Disclaimer, &f.Published); err != nil {
return nil, err return nil, err
} }
files = append(files, f) files = append(files, f)
@ -89,13 +92,13 @@ func GetFiles() ([]*EFile, error) {
// GetFile retrieves the file with the given id. // GetFile retrieves the file with the given id.
func GetFile(id int64) (f *EFile, err error) { func GetFile(id int64) (f *EFile, err error) {
f = &EFile{} f = &EFile{}
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) err = DBQueryRow("SELECT id_file, origin, path, name, cksum, cksum_shown, size, disclaimer, published FROM exercice_files WHERE id_file = ?", id).Scan(&f.Id, &f.origin, &f.Path, &f.Name, &f.Checksum, &f.ChecksumShown, &f.Size, &f.Disclaimer, &f.Published)
return return
} }
func (e *Exercice) GetFile(id int64) (f *EFile, err error) { func (e *Exercice) GetFile(id int64) (f *EFile, err error) {
f = &EFile{} f = &EFile{}
err = DBQueryRow("SELECT id_file, origin, path, name, cksum, cksum_shown, size, published FROM exercice_files WHERE id_file = ? AND id_exercice = ?", id, e.Id).Scan(&f.Id, &f.origin, &f.Path, &f.Name, &f.Checksum, &f.ChecksumShown, &f.Size, &f.Published) err = DBQueryRow("SELECT id_file, origin, path, name, cksum, cksum_shown, size, disclaimer, published FROM exercice_files WHERE id_file = ? AND id_exercice = ?", id, e.Id).Scan(&f.Id, &f.origin, &f.Path, &f.Name, &f.Checksum, &f.ChecksumShown, &f.Size, &f.Disclaimer, &f.Published)
return return
} }
@ -104,7 +107,7 @@ func GetFileByPath(path string) (*EFile, error) {
path = strings.TrimPrefix(path, FilesDir) path = strings.TrimPrefix(path, FilesDir)
f := &EFile{} f := &EFile{}
if err := DBQueryRow("SELECT id_file, origin, path, id_exercice, name, cksum, cksum_shown, size, published FROM exercice_files WHERE path = ?", path).Scan(&f.Id, &f.origin, &f.Path, &f.IdExercice, &f.Name, &f.Checksum, &f.ChecksumShown, &f.Size, &f.Published); err != nil { if err := DBQueryRow("SELECT id_file, origin, path, id_exercice, name, cksum, cksum_shown, size, disclaimer, published FROM exercice_files WHERE path = ?", path).Scan(&f.Id, &f.origin, &f.Path, &f.IdExercice, &f.Name, &f.Checksum, &f.ChecksumShown, &f.Size, &f.Disclaimer, &f.Published); err != nil {
return f, err return f, err
} }
@ -116,14 +119,14 @@ func (e *Exercice) GetFileByFilename(filename string) (f *EFile, err error) {
filename = path.Base(filename) filename = path.Base(filename)
f = &EFile{} f = &EFile{}
err = DBQueryRow("SELECT id_file, origin, path, id_exercice, name, cksum, cksum_shown, size, published FROM exercice_files WHERE id_exercice = ? AND origin LIKE ?", e.Id, "%/"+filename).Scan(&f.Id, &f.origin, &f.Path, &f.IdExercice, &f.Name, &f.Checksum, &f.ChecksumShown, &f.Size, &f.Published) err = DBQueryRow("SELECT id_file, origin, path, id_exercice, name, cksum, cksum_shown, size, disclaimer, published FROM exercice_files WHERE id_exercice = ? AND origin LIKE ?", e.Id, "%/"+filename).Scan(&f.Id, &f.origin, &f.Path, &f.IdExercice, &f.Name, &f.Checksum, &f.ChecksumShown, &f.Size, &f.Disclaimer, &f.Published)
return return
} }
// GetFiles returns a list of files coming with the challenge. // GetFiles returns a list of files coming with the challenge.
func (e *Exercice) GetFiles() ([]*EFile, error) { func (e *Exercice) GetFiles() ([]*EFile, error) {
if rows, err := DBQuery("SELECT id_file, origin, path, name, cksum, cksum_shown, size, published FROM exercice_files WHERE id_exercice = ?", e.Id); err != nil { if rows, err := DBQuery("SELECT id_file, origin, path, name, cksum, cksum_shown, size, disclaimer, published FROM exercice_files WHERE id_exercice = ?", e.Id); err != nil {
return nil, err return nil, err
} else { } else {
defer rows.Close() defer rows.Close()
@ -132,7 +135,7 @@ func (e *Exercice) GetFiles() ([]*EFile, error) {
for rows.Next() { for rows.Next() {
f := &EFile{} f := &EFile{}
f.IdExercice = e.Id f.IdExercice = e.Id
if err := rows.Scan(&f.Id, &f.origin, &f.Path, &f.Name, &f.Checksum, &f.ChecksumShown, &f.Size, &f.Published); err != nil { if err := rows.Scan(&f.Id, &f.origin, &f.Path, &f.Name, &f.Checksum, &f.ChecksumShown, &f.Size, &f.Disclaimer, &f.Published); err != nil {
return nil, err return nil, err
} }
files = append(files, f) files = append(files, f)
@ -150,7 +153,7 @@ func (e *Exercice) GetFileByPath(path string) (*EFile, error) {
path = strings.TrimPrefix(path, FilesDir) path = strings.TrimPrefix(path, FilesDir)
f := &EFile{} f := &EFile{}
if err := DBQueryRow("SELECT id_file, origin, path, name, cksum, cksum_shown, size, published FROM exercice_files WHERE id_exercice = ? AND path = ?", e.Id, path).Scan(&f.Id, &f.origin, &f.Path, &f.Name, &f.Checksum, &f.ChecksumShown, &f.Size, &f.Published); err != nil { if err := DBQueryRow("SELECT id_file, origin, path, name, cksum, cksum_shown, size, disclaimer, published FROM exercice_files WHERE id_exercice = ? AND path = ?", e.Id, path).Scan(&f.Id, &f.origin, &f.Path, &f.Name, &f.Checksum, &f.ChecksumShown, &f.Size, &f.Disclaimer, &f.Published); err != nil {
return nil, err return nil, err
} }
@ -229,13 +232,13 @@ func checkFileHash(filePath string, digest []byte) (dgst []byte, size int64, err
} }
// ImportFile registers (ou updates if it already exists in database) the file in database. // ImportFile registers (ou updates if it already exists in database) the file in database.
func (e *Exercice) ImportFile(filePath string, origin string, digest []byte, digestshown []byte, published bool) (interface{}, error) { func (e *Exercice) ImportFile(filePath string, origin string, digest []byte, digestshown []byte, disclaimer string, published bool) (interface{}, error) {
if result512, size, err := checkFileHash(filePath, digest); !OptionalDigest && err != nil { if result512, size, err := checkFileHash(filePath, digest); !OptionalDigest && err != nil {
return EFile{}, err return EFile{}, err
} else { } else {
dPath := strings.TrimPrefix(filePath, FilesDir) dPath := strings.TrimPrefix(filePath, FilesDir)
if f, err := e.GetFileByPath(dPath); err != nil { if f, err := e.GetFileByPath(dPath); err != nil {
return e.AddFile(dPath, origin, path.Base(filePath), result512, digestshown, size, published) return e.AddFile(dPath, origin, path.Base(filePath), result512, digestshown, size, disclaimer, published)
} else { } else {
// Don't need to update Path and Name, because they are related to dPath // Don't need to update Path and Name, because they are related to dPath
@ -244,6 +247,7 @@ func (e *Exercice) ImportFile(filePath string, origin string, digest []byte, dig
f.Checksum = result512 f.Checksum = result512
f.ChecksumShown = digestshown f.ChecksumShown = digestshown
f.Size = size f.Size = size
f.Disclaimer = disclaimer
f.Published = published f.Published = published
if _, err := f.Update(); err != nil { if _, err := f.Update(); err != nil {
@ -256,19 +260,19 @@ func (e *Exercice) ImportFile(filePath string, origin string, digest []byte, dig
} }
// AddFile creates and fills a new struct File and registers it into the database. // AddFile creates and fills a new struct File and registers it into the database.
func (e *Exercice) AddFile(path string, origin string, name string, checksum []byte, checksumshown []byte, size int64, published bool) (*EFile, error) { func (e *Exercice) AddFile(path string, origin string, name string, checksum []byte, checksumshown []byte, size int64, disclaimer string, published bool) (*EFile, error) {
if res, err := DBExec("INSERT INTO exercice_files (id_exercice, origin, path, name, cksum, cksum_shown, size, published) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", e.Id, origin, path, name, checksum, checksumshown, size, published); err != nil { if res, err := DBExec("INSERT INTO exercice_files (id_exercice, origin, path, name, cksum, cksum_shown, size, disclaimer, published) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", e.Id, origin, path, name, checksum, checksumshown, size, disclaimer, published); err != nil {
return nil, err return nil, err
} else if fid, err := res.LastInsertId(); err != nil { } else if fid, err := res.LastInsertId(); err != nil {
return nil, err return nil, err
} else { } else {
return &EFile{fid, origin, path, e.Id, name, checksum, checksumshown, size, published}, nil return &EFile{fid, origin, path, e.Id, name, checksum, checksumshown, size, disclaimer, published}, nil
} }
} }
// Update applies modifications back to the database. // Update applies modifications back to the database.
func (f *EFile) Update() (int64, error) { func (f *EFile) Update() (int64, error) {
if res, err := DBExec("UPDATE exercice_files SET id_exercice = ?, origin = ?, path = ?, name = ?, cksum = ?, cksum_shown = ?, size = ?, published = ? WHERE id_file = ?", f.IdExercice, f.origin, f.Path, f.Name, f.Checksum, f.ChecksumShown, f.Size, f.Published, f.Id); err != nil { if res, err := DBExec("UPDATE exercice_files SET id_exercice = ?, origin = ?, path = ?, name = ?, cksum = ?, cksum_shown = ?, size = ?, disclaimer = ?, published = ? WHERE id_file = ?", f.IdExercice, f.origin, f.Path, f.Name, f.Checksum, f.ChecksumShown, f.Size, f.Disclaimer, f.Published, f.Id); err != nil {
return 0, err return 0, err
} else if nb, err := res.RowsAffected(); err != nil { } else if nb, err := res.RowsAffected(); err != nil {
return 0, err return 0, err