Skip to content
Permalink
Browse files

Remove pngcrush command, rework static loading adding an unique ident…

…ifier for each non gziped file
  • Loading branch information...
Raggaer committed Apr 1, 2019
1 parent 08edd40 commit 3e6d596a77195d2952760fdc6f4a37f650884b40
Showing with 6 additions and 18 deletions.
  1. +0 −2 README.md
  2. +6 −16 static.go
@@ -17,8 +17,6 @@ Import on your code and start using it:
import "github.com/raggaer/gin-static"
```

**If pngcrush is available it will be used to crush `.png` images**

### Example

```go
@@ -6,7 +6,6 @@ import (
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
@@ -87,6 +86,7 @@ func LoadStaticAssets(dir string) error {
minifier.AddFunc("text/css", css.Minify)
minifier.AddFunc("text/js", js.Minify)
AssetMap.rw.Lock()
i := 0
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
@@ -103,25 +103,11 @@ func LoadStaticAssets(dir string) error {
strings.HasSuffix(path, ".svg") ||
strings.HasSuffix(path, ".jpg") ||
strings.HasSuffix(path, ".gif") {
fname = filepath.Join("static", info.Name())
fname = filepath.Join("static", strconv.Itoa(i)+"_"+info.Name())
} else {
fname = filepath.Join("static", strconv.FormatInt(info.ModTime().Unix(), 10)+"_"+info.Name()+".gz")
}

// Check if file is .png
if strings.HasSuffix(path, ".png") && gin.Mode() != gin.DebugMode {
cmd := exec.Command("pngcrush", "-reduce", "-d", "static/", path)
_, err := cmd.CombinedOutput()
if err == nil {
AssetMap.Assets["/"+filepath.ToSlash(path)] = &StaticAsset{
Mod: info.ModTime(),
Name: fname,
Hash: xid.New().String(),
}
return nil
}
}

dst, err := os.OpenFile(fname, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0777)
if err != nil {
return err
@@ -150,6 +136,8 @@ func LoadStaticAssets(dir string) error {
}
gzipWriter := gzip.NewWriter(dst)
if _, err := gzipWriter.Write(gzipBuffer.Bytes()); err != nil {
src.Close()
dst.Close()
return err
}
gzipWriter.Close()
@@ -160,6 +148,7 @@ func LoadStaticAssets(dir string) error {
}
dst.Close()
src.Close()
i++
return nil
}

@@ -176,6 +165,7 @@ func LoadStaticAssets(dir string) error {
}
dst.Close()
src.Close()
i++
return nil
})
AssetMap.rw.Unlock()

0 comments on commit 3e6d596

Please sign in to comment.
You can’t perform that action at this time.