pkger/pkging/embed/embed.go

56 lines
952 B
Go
Raw Normal View History

2019-10-21 00:52:16 +03:00
package embed
import (
"bytes"
"compress/gzip"
"encoding/hex"
2019-10-25 17:19:27 +03:00
"io"
2019-10-21 00:52:16 +03:00
"github.com/markbates/pkger/here"
2019-10-21 00:52:16 +03:00
)
2019-10-25 17:19:27 +03:00
func Decode(src []byte) ([]byte, error) {
dst := make([]byte, hex.DecodedLen(len(src)))
_, err := hex.Decode(dst, src)
if err != nil {
return nil, err
}
2019-10-21 00:52:16 +03:00
2019-10-25 17:19:27 +03:00
r, err := gzip.NewReader(bytes.NewReader(dst))
if err != nil {
return nil, err
}
2019-10-21 00:52:16 +03:00
2019-10-25 17:19:27 +03:00
bb := &bytes.Buffer{}
if _, err := io.Copy(bb, r); err != nil {
return nil, err
}
return bb.Bytes(), nil
2019-10-21 00:52:16 +03:00
}
2019-10-25 17:19:27 +03:00
func Encode(b []byte) ([]byte, error) {
2019-10-21 00:52:16 +03:00
bb := &bytes.Buffer{}
gz := gzip.NewWriter(bb)
2019-10-25 17:19:27 +03:00
if _, err := gz.Write(b); err != nil {
2019-10-21 00:52:16 +03:00
return nil, err
}
2019-10-25 17:19:27 +03:00
if err := gz.Flush(); err != nil {
2019-10-21 00:52:16 +03:00
return nil, err
}
2019-10-25 17:19:27 +03:00
if err := gz.Close(); err != nil {
return nil, err
2019-10-21 00:52:16 +03:00
}
s := hex.EncodeToString(bb.Bytes())
2019-10-25 17:19:27 +03:00
return []byte(s), nil
}
type Data struct {
Infos map[string]here.Info `json:"infos"`
Files map[string]File `json:"files"`
Here here.Info `json:"here"`
2019-10-21 00:52:16 +03:00
}