MCPcopy
hub / github.com/syncthing/syncthing / walkerFor

Function walkerFor

script/genassets.go:64–107  ·  view source on GitHub ↗
(basePath string)

Source from the content-addressed store, hash-verified

62var assets []asset
63
64func walkerFor(basePath string) filepath.WalkFunc {
65 return func(name string, info os.FileInfo, err error) error {
66 if err != nil {
67 return err
68 }
69
70 if strings.HasPrefix(filepath.Base(name), ".") {
71 // Skip dotfiles
72 return nil
73 }
74
75 if info.Mode().IsRegular() {
76 data, err := os.ReadFile(name)
77 if err != nil {
78 return err
79 }
80 length := len(data)
81
82 var buf bytes.Buffer
83 gw, _ := gzip.NewWriterLevel(&buf, gzip.BestCompression)
84 gw.Write(data)
85 gw.Close()
86
87 // Only replace asset by gzipped version if it is smaller.
88 // In practice, this means HTML, CSS, SVG etc. get compressed,
89 // while PNG and WOFF files are left uncompressed.
90 // lib/assets detects gzip and sets headers/decompresses.
91 gzipped := buf.Len() < len(data)
92 if gzipped {
93 data = buf.Bytes()
94 }
95
96 name, _ = filepath.Rel(basePath, name)
97 assets = append(assets, asset{
98 Name: filepath.ToSlash(name),
99 Data: fmt.Sprintf("%q", string(data)),
100 Length: length,
101 Gzipped: gzipped,
102 })
103 }
104
105 return nil
106 }
107}
108
109type templateVars struct {
110 Assets []asset

Callers 1

mainFunction · 0.70

Calls 6

LenMethod · 0.80
BytesMethod · 0.80
IsRegularMethod · 0.65
ModeMethod · 0.65
CloseMethod · 0.65
WriteMethod · 0.45

Tested by

no test coverage detected