From 8cca5de5d0cd6b18c328a18de286eeb3b2c75b84 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Sun, 23 Jun 2024 17:37:10 +0200 Subject: pre-compress static files --- internal/builder/builder.go | 6 +----- internal/builder/files.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) (limited to 'internal/builder') diff --git a/internal/builder/builder.go b/internal/builder/builder.go index 4f305db..f3e61dc 100644 --- a/internal/builder/builder.go +++ b/internal/builder/builder.go @@ -17,7 +17,6 @@ import ( "github.com/a-h/templ" mapset "github.com/deckarep/golang-set/v2" - cp "github.com/otiai10/copy" "github.com/pkg/errors" ) @@ -102,10 +101,7 @@ func build(outDir string, config config.Config) (*Result, error) { return nil, errors.WithMessage(err, "could not create public directory") } - err := cp.Copy("static", publicDir, cp.Options{ - PreserveTimes: true, - PermissionControl: cp.AddPermission(0755), - }) + err := copyRecursive("static", publicDir) if err != nil { return nil, errors.WithMessage(err, "could not copy static files") } diff --git a/internal/builder/files.go b/internal/builder/files.go index 57d4d2e..20fedf9 100644 --- a/internal/builder/files.go +++ b/internal/builder/files.go @@ -3,7 +3,9 @@ package builder import ( "compress/gzip" "io" + "io/fs" "os" + "path/filepath" "github.com/andybalholm/brotli" ) @@ -81,3 +83,30 @@ func openFileAndVariants(filename string) (io.WriteCloser, error) { return openFileWrite(filename) } } + +func copyRecursive(src, dst string) error { + return filepath.WalkDir(src, func(path string, d fs.DirEntry, err error) error { + rel, err := filepath.Rel(src, path) + if err != nil { + return err + } + if d.IsDir() { + return mkdirp(dst, rel) + } else { + sf, err := os.Open(path) + if err != nil { + return err + } + defer sf.Close() + df, err := openFileAndVariants(filepath.Join(dst, rel)) + if err != nil { + return err + } + defer df.Close() + if _, err := io.Copy(df, sf); err != nil { + return err + } + } + return nil + }) +} -- cgit 1.4.1