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 84
| package scripting
import ( "context" "path/filepath" "strings"
"github.com/samber/lo"
"{{{ .Package }}}/app/lib/filesystem" "{{{ .Package }}}/app/lib/filter" "{{{ .Package }}}/app/lib/search/result" "{{{ .Package }}}/app/util" )
type Service struct { FS filesystem.FileLoader `json:"-"` Path string `json:"path,omitempty"` }
func NewService(fs filesystem.FileLoader, pth string) *Service { return &Service{FS: fs, Path: pth} }
func (s *Service) ListScripts(logger util.Logger) []string { files, _ := s.FS.ListFilesRecursive(s.Path, nil, logger) return lo.FilterMap(files, func(x string, _ int) (string, bool) { return x, strings.HasSuffix(x, ".js") }) }
func (s *Service) ListScriptSizes(logger util.Logger) ([]string, map[string]int) { files := s.ListScripts(logger) return files, lo.SliceToMap(files, func(scr string) (string, int) { return scr, s.Size(scr) }) }
func (s *Service) LoadScript(pth string, logger util.Logger) (string, string, error) { logger.Infof("loading script [%s]", pth) filePath := filepath.Join(s.Path, pth) b, err := s.FS.ReadFile(filePath) if err != nil { pth += ".js" b, err = s.FS.ReadFile(filePath + ".js") if err != nil { return "", "", err } } sc := string(b) return pth, sc, nil }
func (s *Service) SaveScript(pth string, content string, logger util.Logger) error { logger.Infof("saving script [%s]", pth) filePath := filepath.Join(s.Path, pth) return s.FS.WriteFile(filePath, []byte(content), filesystem.DefaultMode, true) }
func (s *Service) DeleteScript(pth string, logger util.Logger) error { filePath := filepath.Join(s.Path, pth) return s.FS.Remove(filePath, logger) }
func (s *Service) Size(scr string) int { filePath := filepath.Join(s.Path, scr) st, err := s.FS.Stat(filePath) if err != nil { return 0 } return int(st.Size) }
func (s *Service) SearchScripts(_ context.Context, _ filter.ParamSet, q string, logger util.Logger) (result.Results, error) { f := func(fn string, _ int) (*result.Result, bool) { fn, scr, _ := s.LoadScript(fn, logger) res := result.NewResult("script", fn, "/admin/scripting/"+fn, fn, "file-code", scr, scr, q) if len(res.Matches) > 0 { return res, true } return nil, false } return lo.FilterMap(s.ListScripts(logger), f), nil }
|