aboutsummaryrefslogtreecommitdiff
path: root/script
diff options
context:
space:
mode:
authorgreatroar <61184462+greatroar@users.noreply.github.com>2020-05-25 08:51:27 +0200
committerGitHub <noreply@github.com>2020-05-25 08:51:27 +0200
commitbaa38eea7a848ce693d8187e1a7a3ddb6a7fa55a (patch)
tree99f030122f2daacaad2a93f32a057f8fa8894a2e /script
parentc3b5eba205071f774886e82a961e72a15a2fb1ee (diff)
downloadsyncthing-baa38eea7a848ce693d8187e1a7a3ddb6a7fa55a.tar.gz
syncthing-baa38eea7a848ce693d8187e1a7a3ddb6a7fa55a.zip
lib/assets: Allow assets to remain uncompressed (#6661)
Diffstat (limited to 'script')
-rw-r--r--script/genassets.go54
1 files changed, 40 insertions, 14 deletions
diff --git a/script/genassets.go b/script/genassets.go
index 373c9c5fc..f41c17187 100644
--- a/script/genassets.go
+++ b/script/genassets.go
@@ -15,6 +15,7 @@ import (
"fmt"
"go/format"
"io"
+ "io/ioutil"
"os"
"path/filepath"
"strconv"
@@ -27,20 +28,35 @@ var tpl = template.Must(template.New("assets").Parse(`// Code generated by genas
package auto
-const Generated int64 = {{.Generated}}
+import (
+ "time"
+
+ "github.com/syncthing/syncthing/lib/assets"
+)
+
+func Assets() map[string]assets.Asset {
+ var ret = make(map[string]assets.Asset, {{.Assets | len}})
+ t := time.Unix({{.Generated}}, 0)
-func Assets() map[string]string {
- var assets = make(map[string]string, {{.Assets | len}})
{{range $asset := .Assets}}
- assets["{{$asset.Name}}"] = {{$asset.Data}}{{end}}
- return assets
+ ret["{{$asset.Name}}"] = assets.Asset{
+ Content: {{$asset.Data}},
+ Gzipped: {{$asset.Gzipped}},
+ Length: {{$asset.Length}},
+ Filename: {{$asset.Name | printf "%q"}},
+ Modified: t,
+ }
+{{end}}
+ return ret
}
`))
type asset struct {
- Name string
- Data string
+ Name string
+ Data string
+ Length int
+ Gzipped bool
}
var assets []asset
@@ -57,22 +73,32 @@ func walkerFor(basePath string) filepath.WalkFunc {
}
if info.Mode().IsRegular() {
- fd, err := os.Open(name)
+ data, err := ioutil.ReadFile(name)
if err != nil {
return err
}
+ length := len(data)
var buf bytes.Buffer
- gw := gzip.NewWriter(&buf)
- io.Copy(gw, fd)
- fd.Close()
- gw.Flush()
+ gw, _ := gzip.NewWriterLevel(&buf, gzip.BestCompression)
+ gw.Write(data)
gw.Close()
+ // Only replace asset by gzipped version if it is smaller.
+ // In practice, this means HTML, CSS, SVG etc. get compressed,
+ // while PNG and WOFF files are left uncompressed.
+ // lib/assets detects gzip and sets headers/decompresses.
+ gzipped := buf.Len() < len(data)
+ if gzipped {
+ data = buf.Bytes()
+ }
+
name, _ = filepath.Rel(basePath, name)
assets = append(assets, asset{
- Name: filepath.ToSlash(name),
- Data: fmt.Sprintf("%q", buf.String()),
+ Name: filepath.ToSlash(name),
+ Data: fmt.Sprintf("%q", string(data)),
+ Length: length,
+ Gzipped: gzipped,
})
}