Two changes to the static file generater: 1) It now imports source file data as literal string, saves runtime conversion cost; 2) image/x-icon will no longer be compressed
This commit is contained in:
@@ -23,7 +23,6 @@ import (
|
|||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/hex"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@@ -190,7 +189,6 @@ var (
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
"encoding/hex"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// {{ .GOVariableName }} returns static file
|
// {{ .GOVariableName }} returns static file
|
||||||
@@ -212,21 +210,10 @@ func {{ .GOVariableName }}() (
|
|||||||
panic(createErr)
|
panic(createErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
data, dataErr := hex.DecodeString(` + "`" + `{{ .Data }}` + "`" + `)
|
|
||||||
|
|
||||||
if dataErr != nil {
|
|
||||||
panic(dataErr)
|
|
||||||
}
|
|
||||||
|
|
||||||
shrinkToFit := make([]byte, len(data))
|
|
||||||
|
|
||||||
copy(shrinkToFit, data)
|
|
||||||
data = nil
|
|
||||||
|
|
||||||
return {{ .FileStart }}, {{ .FileEnd }},
|
return {{ .FileStart }}, {{ .FileEnd }},
|
||||||
{{ .CompressedStart }}, {{ .CompressedEnd }},
|
{{ .CompressedStart }}, {{ .CompressedEnd }},
|
||||||
"{{ .ContentHash }}", "{{ .CompressedHash }}",
|
"{{ .ContentHash }}", "{{ .CompressedHash }}",
|
||||||
created, shrinkToFit, "{{ .ContentType }}"
|
created, []byte({{ .Data }}), "{{ .ContentType }}"
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
)
|
)
|
||||||
@@ -281,8 +268,8 @@ func buildDataFile(w io.Writer, data interface{}) error {
|
|||||||
return tpl.Execute(w, data)
|
return tpl.Execute(w, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func byteToArrayStr(b []byte) string {
|
func byteToQuotedString(b []byte) string {
|
||||||
return hex.EncodeToString(b)
|
return fmt.Sprintf("%q", b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getMimeTypeByExtension(ext string) string {
|
func getMimeTypeByExtension(ext string) string {
|
||||||
@@ -306,33 +293,8 @@ func parseFile(
|
|||||||
panic(fmt.Sprintln("Cannot read file:", readErr))
|
panic(fmt.Sprintln("Cannot read file:", readErr))
|
||||||
}
|
}
|
||||||
|
|
||||||
compressed := bytes.NewBuffer(make([]byte, 0, 1024))
|
|
||||||
|
|
||||||
compresser, compresserBuildErr := gzip.NewWriterLevel(
|
|
||||||
compressed, gzip.BestCompression)
|
|
||||||
|
|
||||||
if compresserBuildErr != nil {
|
|
||||||
panic(fmt.Sprintln("Cannot build data compresser:", compresserBuildErr))
|
|
||||||
}
|
|
||||||
|
|
||||||
contentLen := len(content)
|
contentLen := len(content)
|
||||||
|
|
||||||
_, compressErr := compresser.Write(content)
|
|
||||||
|
|
||||||
if compressErr != nil {
|
|
||||||
panic(fmt.Sprintln("Cannot write compressed data:", compressErr))
|
|
||||||
}
|
|
||||||
|
|
||||||
compressErr = compresser.Flush()
|
|
||||||
|
|
||||||
if compressErr != nil {
|
|
||||||
panic(fmt.Sprintln("Cannot write compressed data:", compressErr))
|
|
||||||
}
|
|
||||||
|
|
||||||
content = append(content, compressed.Bytes()...)
|
|
||||||
|
|
||||||
goFileName := "Static" + strconv.FormatInt(int64(id), 10)
|
|
||||||
|
|
||||||
fileExtDotIdx := strings.LastIndex(name, ".")
|
fileExtDotIdx := strings.LastIndex(name, ".")
|
||||||
fileExt := ""
|
fileExt := ""
|
||||||
|
|
||||||
@@ -346,13 +308,43 @@ func parseFile(
|
|||||||
mimeType = "application/binary"
|
mimeType = "application/binary"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if strings.HasPrefix(mimeType, "image/x-icon") {
|
||||||
|
// Don't compress icons
|
||||||
|
} else {
|
||||||
|
compressed := bytes.NewBuffer(make([]byte, 0, 1024))
|
||||||
|
|
||||||
|
compresser, compresserBuildErr := gzip.NewWriterLevel(
|
||||||
|
compressed, gzip.BestCompression)
|
||||||
|
|
||||||
|
if compresserBuildErr != nil {
|
||||||
|
panic(fmt.Sprintln(
|
||||||
|
"Cannot build data compresser:", compresserBuildErr))
|
||||||
|
}
|
||||||
|
|
||||||
|
_, compressErr := compresser.Write(content)
|
||||||
|
|
||||||
|
if compressErr != nil {
|
||||||
|
panic(fmt.Sprintln("Cannot write compressed data:", compressErr))
|
||||||
|
}
|
||||||
|
|
||||||
|
compressErr = compresser.Flush()
|
||||||
|
|
||||||
|
if compressErr != nil {
|
||||||
|
panic(fmt.Sprintln("Cannot write compressed data:", compressErr))
|
||||||
|
}
|
||||||
|
|
||||||
|
content = append(content, compressed.Bytes()...)
|
||||||
|
}
|
||||||
|
|
||||||
|
goFileName := "Static" + strconv.FormatInt(int64(id), 10)
|
||||||
|
|
||||||
return parsedFile{
|
return parsedFile{
|
||||||
Name: name,
|
Name: name,
|
||||||
GOVariableName: strings.Title(goFileName),
|
GOVariableName: strings.Title(goFileName),
|
||||||
GOFileName: strings.ToLower(goFileName) + "_generated.go",
|
GOFileName: strings.ToLower(goFileName) + "_generated.go",
|
||||||
GOPackage: packageName,
|
GOPackage: packageName,
|
||||||
Path: filePath,
|
Path: filePath,
|
||||||
Data: byteToArrayStr(content),
|
Data: byteToQuotedString(content),
|
||||||
FileStart: 0,
|
FileStart: 0,
|
||||||
FileEnd: contentLen,
|
FileEnd: contentLen,
|
||||||
CompressedStart: contentLen,
|
CompressedStart: contentLen,
|
||||||
|
|||||||
Reference in New Issue
Block a user