server/admin/sync/themes.go

132 lines
3.7 KiB
Go
Raw Normal View History

2017-12-08 20:01:42 +00:00
package sync
import (
"fmt"
"path"
"regexp"
2017-12-08 20:01:42 +00:00
"strings"
"srs.epita.fr/fic-server/libfic"
"github.com/julienschmidt/httprouter"
2018-08-17 19:18:10 +00:00
"gopkg.in/russross/blackfriday.v2"
2017-12-08 20:01:42 +00:00
)
2018-05-11 23:08:37 +00:00
// getThemes returns all theme directories in the base directory.
2017-12-08 20:01:42 +00:00
func getThemes(i Importer) ([]string, error) {
var themes []string
if dirs, err := i.listDir("/"); err != nil {
return nil, err
} else {
for _, dir := range dirs {
if _, err := i.listDir(dir); err == nil {
themes = append(themes, dir)
}
}
}
return themes, nil
}
2018-05-11 23:08:37 +00:00
// getAuthors parses the AUTHORS file.
2017-12-08 20:01:42 +00:00
func getAuthors(i Importer, tname string) ([]string, error) {
if authors, err := getFileContent(i, path.Join(tname, "AUTHORS.txt")); err != nil {
return nil, err
} else {
var ret []string
re := regexp.MustCompile("^([^<]+)(?: +<(.*)>)?$")
for _, a := range strings.Split(strings.TrimSpace(authors), "\n") {
grp := re.FindStringSubmatch(a)
if len(grp) < 2 || grp[2] == "" {
ret = append(ret, a)
} else {
ret = append(ret, fmt.Sprintf("<a href=\"%s\">%s</a>", grp[2], grp[1]))
}
}
return ret, nil
2017-12-08 20:01:42 +00:00
}
}
2018-05-11 23:08:37 +00:00
// SyncThemes imports new or updates existing themes.
2017-12-08 20:01:42 +00:00
func SyncThemes(i Importer) []string {
var errs []string
if themes, err := getThemes(i); err != nil {
errs = append(errs, err.Error())
} else {
for _, tdir := range themes {
2017-12-21 21:17:41 +00:00
var authors []string
var intro string
2018-11-25 02:35:54 +00:00
var image string
2017-12-21 21:17:41 +00:00
var theme fic.Theme
var tname string
2017-12-21 21:17:41 +00:00
// Extract theme's label
if f := strings.Index(tdir, "-"); f >= 0 {
tname = tdir[f+1:]
} else {
tname = tdir
}
if authors, err = getAuthors(i, tdir); err != nil {
2017-12-08 20:01:42 +00:00
errs = append(errs, fmt.Sprintf("%q: unable to get AUTHORS: %s", tname, err))
continue
} else if intro, err = getFileContent(i, path.Join(tdir, "overview.txt")); err != nil {
errs = append(errs, fmt.Sprintf("%q: unable to get theme's overview: %s", tname, err))
2017-12-21 21:17:41 +00:00
} else if theme, err = fic.GetThemeByName(tname); err != nil {
2018-11-25 02:35:54 +00:00
if _, err := fic.CreateTheme(tname, fic.ToURLid(tname), tdir, strings.Join(authors, ", "), intro, image); err != nil {
errs = append(errs, fmt.Sprintf("%q: an error occurs during add: %s", tdir, err))
2017-12-08 20:01:42 +00:00
continue
}
2017-12-21 21:17:41 +00:00
}
2017-12-08 20:01:42 +00:00
2018-08-17 19:18:10 +00:00
// Format authors
2017-12-21 21:17:41 +00:00
authors_str := strings.Join(authors, ", ")
2018-08-17 19:18:10 +00:00
// Format overview (markdown)
intro = string(blackfriday.Run([]byte(intro)))
2018-11-25 02:35:54 +00:00
if i.exists(path.Join(tdir, "heading.jpg")) {
image = path.Join(tdir, "heading.jpg")
} else if i.exists(path.Join(tdir, "heading.png")) {
image = path.Join(tdir, "heading.png")
}
if len(image) > 0 {
if _, err := i.importFile(image,
func(filePath string, origin string) (interface{}, error) {
image = strings.TrimPrefix(filePath, fic.FilesDir)
return nil, nil
}); err != nil {
errs = append(errs, fmt.Sprintf("%q: unable to import heading image: %s", tdir, err))
continue
}
}
if theme.Name != tname || theme.Authors != authors_str || theme.Intro != intro || theme.Image != image {
2017-12-21 21:17:41 +00:00
theme.Name = tname
theme.Authors = authors_str
theme.Intro = intro
2018-11-25 02:35:54 +00:00
theme.Image = image
theme.Path = tdir
2017-12-21 21:17:41 +00:00
if _, err := theme.Update(); err != nil {
errs = append(errs, fmt.Sprintf("%q: an error occurs during update: %s", tdir, err))
2017-12-21 21:17:41 +00:00
continue
2017-12-08 20:01:42 +00:00
}
}
}
}
return errs
}
2018-05-11 23:08:37 +00:00
// ApiListRemoteThemes is an accessor letting foreign packages to access remote themes list.
2017-12-08 20:01:42 +00:00
func ApiListRemoteThemes(_ httprouter.Params, _ []byte) (interface{}, error) {
return getThemes(GlobalImporter)
}
2018-05-11 23:08:37 +00:00
// ApiListRemoteTheme is an accessor letting foreign packages to access remote main theme attributes.
2017-12-08 20:01:42 +00:00
func ApiGetRemoteTheme(ps httprouter.Params, _ []byte) (interface{}, error) {
return getAuthors(GlobalImporter, ps.ByName("thid"))
}