From f8001653cde18b20fae1c61aa0fc14054a85d3e6 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Fri, 10 Dec 2021 18:55:47 +0100 Subject: [PATCH] sync: Parse resolution.md --- admin/api/exercice.go | 2 +- admin/sync/exercices.go | 38 ++++++++++++++++++++++++++++------ libfic/db.go | 4 +++- libfic/exercice.go | 46 +++++++++++++++++++++++------------------ 4 files changed, 62 insertions(+), 28 deletions(-) diff --git a/admin/api/exercice.go b/admin/api/exercice.go index 23bb952b..7015a429 100644 --- a/admin/api/exercice.go +++ b/admin/api/exercice.go @@ -338,7 +338,7 @@ func createExercice(theme *fic.Theme, body []byte) (interface{}, error) { } } - return theme.AddExercice(ue.Title, ue.URLId, ue.Path, ue.Statement, ue.Overview, ue.Headline, depend, ue.Gain, ue.VideoURI, ue.Finished) + return theme.AddExercice(ue.Title, ue.URLId, ue.Path, ue.Statement, ue.Overview, ue.Headline, depend, ue.Gain, ue.VideoURI, ue.Resolution, ue.SeeAlso, ue.Finished) } type uploadedHint struct { diff --git a/admin/sync/exercices.go b/admin/sync/exercices.go index 6b25115f..9dd7a9f0 100644 --- a/admin/sync/exercices.go +++ b/admin/sync/exercices.go @@ -190,14 +190,11 @@ func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]* } } - // Handle video + // Handle resolutions + resolutionFound := false + e.VideoURI = path.Join(epath, "resolution.mp4") if !i.exists(e.VideoURI) { - if LogMissingResolution { - log.Printf("%q: resolution.mp4: no video file found at %s", edir, e.VideoURI) - } else { - errs = append(errs, fmt.Sprintf("%q: resolution.mp4: no video file found at %s", edir, e.VideoURI)) - } e.VideoURI = "" } else if size, err := getFileSize(i, e.VideoURI); err != nil { errs = append(errs, fmt.Sprintf("%q: resolution.mp4: ", edir, err)) @@ -205,6 +202,35 @@ func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]* } else if size == 0 { errs = append(errs, fmt.Sprintf("%q: resolution.mp4: The file is empty!", edir)) e.VideoURI = "" + } else { + resolutionFound = true + } + + writeup := path.Join(epath, "resolution.md") + if !i.exists(writeup) { + writeup = path.Join(epath, "resolution.txt") + } + + if i.exists(writeup) { + if size, err := getFileSize(i, writeup); err != nil { + errs = append(errs, fmt.Sprintf("%q: resolution.md: %s", edir, err.Error())) + } else if size == 0 { + errs = append(errs, fmt.Sprintf("%q: resolution.md: The file is empty!", edir)) + } else if e.Resolution, err = getFileContent(i, writeup); err != nil { + errs = append(errs, fmt.Sprintf("%q: resolution.md: %s", edir, err.Error())) + } else if e.Resolution, err = ProcessMarkdown(i, e.Overview, epath); err != nil { + errs = append(errs, fmt.Sprintf("%q: resolution.md: error during markdown processing: %s", edir, err.Error())) + } else { + resolutionFound = true + } + } + + if !resolutionFound { + if LogMissingResolution { + log.Printf("%q: no resolution video or text file found in %s", edir, epath, epath) + } else { + errs = append(errs, fmt.Sprintf("%q: no resolution video or text file found in %s", edir, epath)) + } } return diff --git a/libfic/db.go b/libfic/db.go index ddc13f94..634166ae 100644 --- a/libfic/db.go +++ b/libfic/db.go @@ -143,8 +143,10 @@ CREATE TABLE IF NOT EXISTS exercices( depend INTEGER, gain INTEGER NOT NULL, coefficient_cur FLOAT NOT NULL DEFAULT 1.0, - video_uri VARCHAR(255) NOT NULL, finished TEXT NOT NULL, + video_uri VARCHAR(255) NOT NULL, + resolution TEXT NOT NULL, + seealso TEXT NOT NULL, FOREIGN KEY(id_theme) REFERENCES themes(id_theme), FOREIGN KEY(depend) REFERENCES exercices(id_exercice) ) DEFAULT CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci; diff --git a/libfic/exercice.go b/libfic/exercice.go index 3c2c1845..792b7412 100644 --- a/libfic/exercice.go +++ b/libfic/exercice.go @@ -49,12 +49,16 @@ type Exercice struct { Coefficient float64 `json:"coefficient"` // VideoURI is the link to the resolution video VideoURI string `json:"videoURI"` + // Resolution is a write-up (in HTML) + Resolution string `json:"resolution"` + // SeeAlso is a collection of links (HTML formated) + SeeAlso string `json:"seealso"` } // GetExercice retrieves the challenge with the given id. func GetExercice(id int64) (*Exercice, error) { var e Exercice - if err := DBQueryRow("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, finished FROM exercices WHERE id_exercice = ?", id).Scan(&e.Id, &e.IdTheme, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Finished); err != nil { + if err := DBQueryRow("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, resolution, seealso, finished FROM exercices WHERE id_exercice = ?", id).Scan(&e.Id, &e.IdTheme, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Resolution, &e.SeeAlso, &e.Finished); err != nil { return nil, err } @@ -64,7 +68,7 @@ func GetExercice(id int64) (*Exercice, error) { // GetExercice retrieves the challenge with the given id. func (t *Theme) GetExercice(id int) (*Exercice, error) { e := &Exercice{} - if err := DBQueryRow("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, finished FROM exercices WHERE id_theme = ? AND id_exercice = ?", t.Id, id).Scan(&e.Id, &e.IdTheme, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Finished); err != nil { + if err := DBQueryRow("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, resolution, seealso, finished FROM exercices WHERE id_theme = ? AND id_exercice = ?", t.Id, id).Scan(&e.Id, &e.IdTheme, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Resolution, &e.SeeAlso, &e.Finished); err != nil { return nil, err } @@ -74,7 +78,7 @@ func (t *Theme) GetExercice(id int) (*Exercice, error) { // GetExerciceByTitle retrieves the challenge with the given title. func (t *Theme) GetExerciceByTitle(title string) (*Exercice, error) { e := &Exercice{} - if err := DBQueryRow("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, finished FROM exercices WHERE id_theme = ? AND title = ?", t.Id, title).Scan(&e.Id, &e.IdTheme, &e.Title, &t.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Finished); err != nil { + if err := DBQueryRow("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, resolution, seealso, finished FROM exercices WHERE id_theme = ? AND title = ?", t.Id, title).Scan(&e.Id, &e.IdTheme, &e.Title, &t.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Resolution, &e.SeeAlso, &e.Finished); err != nil { return nil, err } @@ -83,7 +87,7 @@ func (t *Theme) GetExerciceByTitle(title string) (*Exercice, error) { // GetExercices returns the list of all challenges present in the database. func GetExercices() ([]*Exercice, error) { - if rows, err := DBQuery("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, finished FROM exercices"); err != nil { + if rows, err := DBQuery("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, resolution, seealso, finished FROM exercices"); err != nil { return nil, err } else { defer rows.Close() @@ -91,7 +95,7 @@ func GetExercices() ([]*Exercice, error) { exos := []*Exercice{} for rows.Next() { e := &Exercice{} - if err := rows.Scan(&e.Id, &e.IdTheme, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Finished); err != nil { + if err := rows.Scan(&e.Id, &e.IdTheme, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Resolution, &e.SeeAlso, &e.Finished); err != nil { return nil, err } exos = append(exos, e) @@ -106,7 +110,7 @@ func GetExercices() ([]*Exercice, error) { // GetExercices returns the list of all challenges in the Theme. func (t *Theme) GetExercices() ([]*Exercice, error) { - if rows, err := DBQuery("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, finished FROM exercices WHERE id_theme = ?", t.Id); err != nil { + if rows, err := DBQuery("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, resolution, seealso, finished FROM exercices WHERE id_theme = ?", t.Id); err != nil { return nil, err } else { defer rows.Close() @@ -114,7 +118,7 @@ func (t *Theme) GetExercices() ([]*Exercice, error) { exos := []*Exercice{} for rows.Next() { e := &Exercice{} - if err := rows.Scan(&e.Id, &e.IdTheme, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Finished); err != nil { + if err := rows.Scan(&e.Id, &e.IdTheme, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Resolution, &e.SeeAlso, &e.Finished); err != nil { return nil, err } exos = append(exos, e) @@ -163,7 +167,7 @@ func (t *Theme) addExercice(e *Exercice) (err error) { cc = fmt.Sprintf("%f", e.Coefficient) } - if res, err := DBExec("INSERT INTO exercices (id_theme, title, url_id, path, statement, overview, finished, headline, issue, depend, gain, video_uri, issue_kind, coefficient_cur) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, "+ik+", "+cc+")", t.Id, e.Title, e.URLId, e.Path, e.Statement, e.Overview, e.Finished, e.Headline, e.Issue, e.Depend, e.Gain, e.VideoURI); err != nil { + if res, err := DBExec("INSERT INTO exercices (id_theme, title, url_id, path, statement, overview, finished, headline, issue, depend, gain, video_uri, resolution, seealso, issue_kind, coefficient_cur) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, "+ik+", "+cc+")", t.Id, e.Title, e.URLId, e.Path, e.Statement, e.Overview, e.Finished, e.Headline, e.Issue, e.Depend, e.Gain, e.VideoURI, e.Resolution, e.SeeAlso); err != nil { return err } else if eid, err := res.LastInsertId(); err != nil { return err @@ -175,23 +179,25 @@ func (t *Theme) addExercice(e *Exercice) (err error) { } // AddExercice creates and fills a new struct Exercice and registers it into the database. -func (t *Theme) AddExercice(title string, urlId string, path string, statement string, overview string, headline string, depend *Exercice, gain int64, videoURI string, finished string) (e *Exercice, err error) { +func (t *Theme) AddExercice(title string, urlId string, path string, statement string, overview string, headline string, depend *Exercice, gain int64, videoURI string, resolution string, seealso string, finished string) (e *Exercice, err error) { var dpd *int64 = nil if depend != nil { dpd = &depend.Id } e = &Exercice{ - Title: title, - URLId: urlId, - Path: path, - Statement: statement, - Overview: overview, - Headline: headline, - Depend: dpd, - Finished: finished, - Gain: gain, - VideoURI: videoURI, + Title: title, + URLId: urlId, + Path: path, + Statement: statement, + Overview: overview, + Headline: headline, + Depend: dpd, + Finished: finished, + Gain: gain, + VideoURI: videoURI, + Resolution: resolution, + SeeAlso: seealso, } err = t.addExercice(e) @@ -201,7 +207,7 @@ func (t *Theme) AddExercice(title string, urlId string, path string, statement s // Update applies modifications back to the database. func (e *Exercice) Update() (int64, error) { - if res, err := DBExec("UPDATE exercices SET title = ?, url_id = ?, path = ?, statement = ?, overview = ?, headline = ?, issue = ?, issue_kind = ?, depend = ?, gain = ?, coefficient_cur = ?, video_uri = ?, finished = ? WHERE id_exercice = ?", e.Title, e.URLId, e.Path, e.Statement, e.Overview, e.Headline, e.Issue, e.IssueKind, e.Depend, e.Gain, e.Coefficient, e.VideoURI, e.Finished, e.Id); err != nil { + if res, err := DBExec("UPDATE exercices SET title = ?, url_id = ?, path = ?, statement = ?, overview = ?, headline = ?, issue = ?, issue_kind = ?, depend = ?, gain = ?, coefficient_cur = ?, video_uri = ?, resolution = ?, seealso = ?, finished = ? WHERE id_exercice = ?", e.Title, e.URLId, e.Path, e.Statement, e.Overview, e.Headline, e.Issue, e.IssueKind, e.Depend, e.Gain, e.Coefficient, e.VideoURI, e.Resolution, e.SeeAlso, e.Finished, e.Id); err != nil { return 0, err } else if nb, err := res.RowsAffected(); err != nil { return 0, err