From a75aab5cbb3a44f4f7e1a58c1e0545e6bbf50cb7 Mon Sep 17 00:00:00 2001 From: Travis Ralston <travpc@gmail.com> Date: Sun, 16 Jul 2023 17:56:35 -0600 Subject: [PATCH] Improve asset file compression/on-disk size --- cmd/compile_assets/main.go | 38 ++++++++++++++++++++------------------ common/assets/process.go | 6 +++--- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/cmd/compile_assets/main.go b/cmd/compile_assets/main.go index 4cf8761c..e7f7563d 100644 --- a/cmd/compile_assets/main.go +++ b/cmd/compile_assets/main.go @@ -3,7 +3,7 @@ package main import ( "bytes" "compress/gzip" - "encoding/hex" + "encoding/base64" "flag" "fmt" "os" @@ -19,28 +19,32 @@ func main() { outputFile := flag.String("output", "./common/assets/assets.bin.go", "The output Go file to dump the files into") flag.Parse() - fmt.Println("Reading migrations into memory...") - migrations := readDir(*migrationsPath, "migrations") - templates := readDir(*templatesPath, "templates") - assets := readDir(*assetsPath, "assets") + fmt.Println("Reading assets into memory...") fileMap := make(map[string][]byte) - for k, v := range migrations { - fileMap[k] = v - } - for k, v := range templates { - fileMap[k] = v - } - for k, v := range assets { - fileMap[k] = v + appendFn := func(m map[string][]byte) { + for k, v := range m { + fileMap[k] = v + } } + appendFn(readDir(*migrationsPath, "migrations")) + appendFn(readDir(*templatesPath, "templates")) + appendFn(readDir(*assetsPath, "assets")) + fmt.Println("Writing assets go file") str := "package " + path.Base(path.Dir(*outputFile)) + "\n\n" - str += "// !! THIS FILE IS AUTOMATICALLY GENERATED. You can edit it, but it will be overwritten over time.\n\n" - str += "var compressedFiles = map[string]string{\n" + str += "// ============================================================================\n" + str += "// !! THIS FILE IS AUTOMATICALLY GENERATED DURING THE RELEASE/BUILD PROCESS. !!\n" + str += "// !! You can try to overwrite it, but your changes are likely to be lost. !!\n" + str += "// ============================================================================\n" + str += "\n" + str += "// Format version: 1 (hex-encoded gzip)\n" + str += "// Format version: 2 (base64-encoded gzip)\n" + str += "// This file: 2\n\n" + str += "var f2CompressedFiles = map[string]string{\n" for f, b := range fileMap { - b64 := hex.EncodeToString(b) + b64 := base64.StdEncoding.EncodeToString(b) str += fmt.Sprintf("\t\"%s\": \"%s\",\n", f, b64) } str += "}\n" @@ -63,8 +67,6 @@ func readDir(dir string, pathName string) map[string][]byte { continue } fname := path.Join(dir, f.Name()) - fmt.Println("Reading ", fname) - b, err := os.ReadFile(fname) if err != nil { panic(err) diff --git a/common/assets/process.go b/common/assets/process.go index d32a6ea5..e1de6a7f 100644 --- a/common/assets/process.go +++ b/common/assets/process.go @@ -3,7 +3,7 @@ package assets import ( "bytes" "compress/gzip" - "encoding/hex" + "encoding/base64" "io" "os" "path" @@ -84,13 +84,13 @@ func Cleanup() { } func extractPrefixTo(pathName string, destination string) { - for f, h := range compressedFiles { + for f, b64 := range f2CompressedFiles { if !strings.HasPrefix(f, pathName) { continue } logrus.Infof("Decoding %s", f) - b, err := hex.DecodeString(h) + b, err := base64.StdEncoding.DecodeString(b64) if err != nil { panic(err) } -- GitLab