8fe2f162090bdc7759badf6375b09ff50c5d2f1f
[fic/server.git] / admin / sync / themes.go
1 package sync
2
3 import (
4         "fmt"
5         "math/rand"
6         "path"
7         "regexp"
8         "strings"
9
10         "srs.epita.fr/fic-server/libfic"
11         "github.com/julienschmidt/httprouter"
12         "gopkg.in/russross/blackfriday.v2"
13 )
14
15 // getThemes returns all theme directories in the base directory.
16 func getThemes(i Importer) ([]string, error) {
17         var themes []string
18
19         if dirs, err := i.listDir("/"); err != nil {
20                 return nil, err
21         } else {
22                 for _, dir := range dirs {
23                         if _, err := i.listDir(dir); err == nil {
24                                 themes = append(themes, dir)
25                         }
26                 }
27         }
28
29         return themes, nil
30 }
31
32 // getAuthors parses the AUTHORS file.
33 func getAuthors(i Importer, tname string) ([]string, error) {
34         if authors, err := getFileContent(i, path.Join(tname, "AUTHORS.txt")); err != nil {
35                 return nil, err
36         } else {
37                 var ret []string
38                 re := regexp.MustCompile("^([^<]+)(?: +<(.*)>)?$")
39                 for _, a := range strings.Split(strings.TrimSpace(authors), "\n") {
40                         grp := re.FindStringSubmatch(a)
41                         if len(grp) < 2 || grp[2] == "" {
42                                 ret = append(ret, a)
43                         } else {
44                                 ret = append(ret, fmt.Sprintf("<a href=\"%s\">%s</a>", grp[2], grp[1]))
45                         }
46                 }
47                 return ret, nil
48         }
49 }
50
51 // SyncThemes imports new or updates existing themes.
52 func SyncThemes(i Importer) []string {
53         var errs []string
54
55         if themes, err := getThemes(i); err != nil {
56                 errs = append(errs, err.Error())
57         } else {
58                 rand.Shuffle(len(themes), func(i, j int) {
59                         themes[i], themes[j] = themes[j], themes[i]
60                 })
61
62                 for _, tdir := range themes {
63                         var authors []string
64                         var intro string
65                         var headline string
66                         var image string
67                         var theme fic.Theme
68                         var tname string
69
70                         // Extract theme's label
71                         if f := strings.Index(tdir, "-"); f >= 0 {
72                                 tname = tdir[f+1:]
73                         } else {
74                                 tname = tdir
75                         }
76
77                         if authors, err = getAuthors(i, tdir); err != nil {
78                                 errs = append(errs, fmt.Sprintf("%q: unable to get AUTHORS: %s", tname, err))
79                                 continue
80                         } else if intro, err = getFileContent(i, path.Join(tdir, "overview.txt")); err != nil {
81                                 errs = append(errs, fmt.Sprintf("%q: unable to get theme's overview: %s", tname, err))
82                         } else {
83                                 // Split headline from intro
84                                 ovrvw := strings.Split(intro, "\n")
85                                 headline = ovrvw[0]
86                                 if len(ovrvw) > 1 {
87                                         intro = strings.Join(ovrvw[1:], "\n")
88                                 }
89
90                                 if theme, err = fic.GetThemeByName(tname); err != nil {
91                                         if _, err := fic.CreateTheme(tname, fic.ToURLid(tname), tdir, strings.Join(authors, ", "), intro, headline, image); err != nil {
92                                                 errs = append(errs, fmt.Sprintf("%q: an error occurs during add: %s", tdir, err))
93                                                 continue
94                                         }
95                                 }
96                         }
97
98                         // Format authors
99                         authors_str := strings.Join(authors, ", ")
100
101                         // Format overview (markdown)
102                         intro, err = ProcessMarkdown(i, intro, tdir)
103                         if err != nil {
104                                 errs = append(errs, fmt.Sprintf("%q: overview.txt: an error occurs during markdown formating: %s", tdir, err))
105                         }
106                         headline = string(blackfriday.Run([]byte(headline)))
107
108                         if i.exists(path.Join(tdir, "heading.jpg")) {
109                                 image = path.Join(tdir, "heading.jpg")
110                         } else if i.exists(path.Join(tdir, "heading.png")) {
111                                 image = path.Join(tdir, "heading.png")
112                         }
113
114                         if len(image) > 0 {
115                                 if _, err := i.importFile(image,
116                                         func(filePath string, origin string) (interface{}, error) {
117                                                 image = strings.TrimPrefix(filePath, fic.FilesDir)
118                                                 return nil, nil
119                                         }); err != nil {
120                                                 errs = append(errs, fmt.Sprintf("%q: unable to import heading image: %s", tdir, err))
121                                                 continue
122                                         }
123                         }
124
125                         if theme.Name != tname || theme.Authors != authors_str || theme.Headline != headline || theme.Intro != intro || theme.Image != image {
126                                 theme.Name = tname
127                                 theme.Authors = authors_str
128                                 theme.Intro = intro
129                                 theme.Headline = headline
130                                 theme.Image = image
131                                 theme.Path = tdir
132                                 if _, err := theme.Update(); err != nil {
133                                         errs = append(errs, fmt.Sprintf("%q: an error occurs during update: %s", tdir, err))
134                                         continue
135                                 }
136                         }
137                 }
138         }
139
140         return errs
141 }
142
143 // ApiListRemoteThemes is an accessor letting foreign packages to access remote themes list.
144 func ApiListRemoteThemes(_ httprouter.Params, _ []byte) (interface{}, error) {
145         return getThemes(GlobalImporter)
146 }
147
148 // ApiListRemoteTheme is an accessor letting foreign packages to access remote main theme attributes.
149 func ApiGetRemoteTheme(ps httprouter.Params, _ []byte) (interface{}, error) {
150         return getAuthors(GlobalImporter, ps.ByName("thid"))
151 }