fic: Exercice can have heading.jpg
This commit is contained in:
parent
f366d6b8c1
commit
abe5ad61d4
5 changed files with 42 additions and 11 deletions
|
@ -3,6 +3,7 @@ package sync
|
|||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"image"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
|
@ -213,6 +214,14 @@ func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*
|
|||
}
|
||||
}
|
||||
|
||||
if i.Exists(path.Join(epath, "heading.jpg")) {
|
||||
e.Image = path.Join(epath, "heading.jpg")
|
||||
} else if i.Exists(path.Join(epath, "heading.png")) {
|
||||
e.Image = path.Join(epath, "heading.png")
|
||||
} else if theme.Image == "" {
|
||||
errs = append(errs, NewExerciceError(e, fmt.Errorf("heading.jpg: No such file")))
|
||||
}
|
||||
|
||||
// Parse challenge.txt
|
||||
var md toml.MetaData
|
||||
p, md, err = parseExerciceParams(i, epath)
|
||||
|
@ -340,6 +349,25 @@ func SyncExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*f
|
|||
}
|
||||
|
||||
if e != nil {
|
||||
if len(e.Image) > 0 {
|
||||
if _, err := i.importFile(e.Image,
|
||||
func(filePath string, origin string) (interface{}, error) {
|
||||
if err := resizePicture(filePath, image.Rect(0, 0, 500, 300)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
e.Image = strings.TrimPrefix(filePath, fic.FilesDir)
|
||||
|
||||
// If the theme has no image yet, use the first exercice's image found
|
||||
theme.Image = e.Image
|
||||
_, err := theme.Update()
|
||||
|
||||
return nil, err
|
||||
}); err != nil {
|
||||
errs = append(errs, NewExerciceError(e, fmt.Errorf("unable to import heading image: %w", err)))
|
||||
}
|
||||
}
|
||||
|
||||
// Create or update the exercice
|
||||
err = theme.SaveNamedExercice(e)
|
||||
if err != nil {
|
||||
|
|
Reference in a new issue