-
Notifications
You must be signed in to change notification settings - Fork 4
/
files.go
83 lines (76 loc) · 2.02 KB
/
files.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package module
import (
"strings"
"github.com/pkg/errors"
"github.com/samber/lo"
"projectforge.dev/projectforge/app/file"
"projectforge.dev/projectforge/app/lib/filesystem"
"projectforge.dev/projectforge/app/util"
)
func (s *Service) GetFilenames(mods Modules, logger util.Logger) ([]string, error) {
ret := map[string]bool{}
for _, mod := range mods {
loader := s.GetFilesystem(mod.Key)
fs, err := loader.ListFilesRecursive("", nil, logger)
if err != nil {
return nil, err
}
lo.ForEach(fs, func(f string, _ int) {
ret[f] = true
})
}
keys := lo.Keys(ret)
return util.ArraySorted(keys), nil
}
func (s *Service) GetFiles(mods Modules, addHeader bool, logger util.Logger) (file.Files, error) {
ret := map[string]*file.File{}
for _, mod := range mods {
err := s.loadFiles(mod, addHeader, ret, logger)
if err != nil {
return nil, errors.Wrapf(err, "unable to load module [%s]", mod.Key)
}
}
return lo.Map(util.ArraySorted(lo.Keys(ret)), func(k string, _ int) *file.File {
return ret[k]
}), nil
}
func (s *Service) loadFiles(mod *Module, addHeader bool, ret map[string]*file.File, logger util.Logger) error {
loader := s.GetFilesystem(mod.Key)
fs, err := loader.ListFilesRecursive("", nil, logger)
if err != nil {
return err
}
for _, f := range fs {
if f == configFilename {
continue
}
if strings.HasSuffix(f, ".png") || strings.HasSuffix(f, ".ico") || strings.HasSuffix(f, ".icns") {
continue
}
mode, b, err := fileContent(loader, f)
if err != nil {
return err
}
pkg := ""
if f != "" {
spl := strings.Split(f, "/")
if len(spl) > 1 {
pkg = spl[len(spl)-2]
}
}
fl := file.NewFile(f, mode, b, addHeader, pkg, logger)
ret[fl.FullPath()] = fl
}
return nil
}
func fileContent(files filesystem.FileLoader, path string) (filesystem.FileMode, []byte, error) {
stat, err := files.Stat(path)
if err != nil {
return 0, nil, errors.Wrapf(err, "file [%s] not found", path)
}
b, err := files.ReadFile(path)
if err != nil {
return 0, nil, err
}
return stat.Mode, b, nil
}