mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
adding gres
This commit is contained in:
@ -20,21 +20,45 @@ func init() {
|
||||
)
|
||||
|
||||
var (
|
||||
// Default resource object.
|
||||
defaultResource = New()
|
||||
)
|
||||
|
||||
// Add unpacks and adds the <content> into the default resource object.
|
||||
// The unnecessary parameter <prefix> indicates the prefix
|
||||
// for each file storing into current resource object.
|
||||
func Add(content []byte, prefix ...string) error {
|
||||
return defaultResource.Add(content, prefix...)
|
||||
}
|
||||
|
||||
// Load loads, unpacks and adds the data from <path> into the default resource object.
|
||||
// The unnecessary parameter <prefix> indicates the prefix
|
||||
// for each file storing into current resource object.
|
||||
func Load(path string, prefix ...string) error {
|
||||
return defaultResource.Load(path, prefix...)
|
||||
}
|
||||
|
||||
// Get returns the file with given path.
|
||||
func Get(path string) *File {
|
||||
return defaultResource.Get(path)
|
||||
}
|
||||
|
||||
// Contains checks whether the <path> exists in the default resource object.
|
||||
func Contains(path string) bool {
|
||||
return defaultResource.Contains(path)
|
||||
}
|
||||
|
||||
// Scan returns the files under the given path, the parameter <path> should be a folder type.
|
||||
//
|
||||
// The pattern parameter <pattern> supports multiple file name patterns,
|
||||
// using the ',' symbol to separate multiple patterns.
|
||||
//
|
||||
// It scans directory recursively if given parameter <recursive> is true.
|
||||
func Scan(path string, pattern string, recursive ...bool) []*File {
|
||||
return defaultResource.Scan(path, pattern, recursive...)
|
||||
}
|
||||
|
||||
// Dump prints the files of the default resource object.
|
||||
func Dump() {
|
||||
defaultResource.Dump()
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@ package gres
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
@ -46,3 +47,13 @@ func (f *File) Content() ([]byte, error) {
|
||||
func (f *File) FileInfo() os.FileInfo {
|
||||
return f.zipFile.FileInfo()
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (f *File) MarshalJSON() ([]byte, error) {
|
||||
info := f.FileInfo()
|
||||
return json.Marshal(map[string]interface{}{
|
||||
"name": f.Name(),
|
||||
"size": info.Size(),
|
||||
"time": info.ModTime(),
|
||||
})
|
||||
}
|
||||
|
||||
@ -16,6 +16,9 @@ import (
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
)
|
||||
|
||||
// Pack packs the path specified by <srcPath> into bytes.
|
||||
// The unnecessary parameter <keyPrefix> indicates the prefix for each file
|
||||
// packed into the result bytes.
|
||||
func Pack(srcPath string, keyPrefix ...string) ([]byte, error) {
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
err := gcompress.ZipPathWriter(srcPath, buffer, keyPrefix...)
|
||||
@ -25,6 +28,9 @@ func Pack(srcPath string, keyPrefix ...string) ([]byte, error) {
|
||||
return buffer.Bytes(), nil
|
||||
}
|
||||
|
||||
// PackToFile packs the path specified by <srcPath> to target file <dstPath>.
|
||||
// The unnecessary parameter <keyPrefix> indicates the prefix for each file
|
||||
// packed into the result bytes.
|
||||
func PackToFile(srcPath, dstPath string, keyPrefix ...string) error {
|
||||
data, err := Pack(srcPath, keyPrefix...)
|
||||
if err != nil {
|
||||
@ -33,6 +39,11 @@ func PackToFile(srcPath, dstPath string, keyPrefix ...string) error {
|
||||
return gfile.PutBytes(dstPath, data)
|
||||
}
|
||||
|
||||
// PackToGoFile packs the path specified by <srcPath> to target go file <goFilePath>
|
||||
// with given package name <pkgName>.
|
||||
//
|
||||
// The unnecessary parameter <keyPrefix> indicates the prefix for each file
|
||||
// packed into the result bytes.
|
||||
func PackToGoFile(srcPath, goFilePath, pkgName string, keyPrefix ...string) error {
|
||||
data, err := Pack(srcPath, keyPrefix...)
|
||||
if err != nil {
|
||||
@ -43,6 +54,7 @@ func PackToGoFile(srcPath, goFilePath, pkgName string, keyPrefix ...string) erro
|
||||
)
|
||||
}
|
||||
|
||||
// Unpack unpacks the content specified by <path> to []*File.
|
||||
func Unpack(path string) ([]*File, error) {
|
||||
realPath, err := gfile.Search(path)
|
||||
if err != nil {
|
||||
@ -51,6 +63,7 @@ func Unpack(path string) ([]*File, error) {
|
||||
return UnpackContent(gfile.GetBytes(realPath))
|
||||
}
|
||||
|
||||
// UnpackContent unpacks the content to []*File.
|
||||
func UnpackContent(content []byte) ([]*File, error) {
|
||||
reader, err := zip.NewReader(bytes.NewReader(content), int64(len(content)))
|
||||
if err != nil {
|
||||
|
||||
@ -23,6 +23,7 @@ const (
|
||||
gDEFAULT_TREE_M = 100
|
||||
)
|
||||
|
||||
// New creates and returns a new resource object.
|
||||
func New() *Resource {
|
||||
return &Resource{
|
||||
tree: gtree.NewBTree(gDEFAULT_TREE_M, func(v1, v2 interface{}) int {
|
||||
@ -31,6 +32,9 @@ func New() *Resource {
|
||||
}
|
||||
}
|
||||
|
||||
// Add unpacks and adds the <content> into current resource object.
|
||||
// The unnecessary parameter <prefix> indicates the prefix
|
||||
// for each file storing into current resource object.
|
||||
func (r *Resource) Add(content []byte, prefix ...string) error {
|
||||
files, err := UnpackContent(content)
|
||||
if err != nil {
|
||||
@ -46,6 +50,9 @@ func (r *Resource) Add(content []byte, prefix ...string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Load loads, unpacks and adds the data from <path> into current resource object.
|
||||
// The unnecessary parameter <prefix> indicates the prefix
|
||||
// for each file storing into current resource object.
|
||||
func (r *Resource) Load(path string, prefix ...string) error {
|
||||
realPath, err := gfile.Search(path)
|
||||
if err != nil {
|
||||
@ -54,6 +61,7 @@ func (r *Resource) Load(path string, prefix ...string) error {
|
||||
return r.Add(gfile.GetBytes(realPath), prefix...)
|
||||
}
|
||||
|
||||
// Get returns the file with given path.
|
||||
func (r *Resource) Get(path string) *File {
|
||||
result := r.tree.Get(path)
|
||||
if result != nil {
|
||||
@ -62,6 +70,17 @@ func (r *Resource) Get(path string) *File {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Contains checks whether the <path> exists in current resource object.
|
||||
func (r *Resource) Contains(path string) bool {
|
||||
return r.Get(path) != nil
|
||||
}
|
||||
|
||||
// Scan returns the files under the given path, the parameter <path> should be a folder type.
|
||||
//
|
||||
// The pattern parameter <pattern> supports multiple file name patterns,
|
||||
// using the ',' symbol to separate multiple patterns.
|
||||
//
|
||||
// It scans directory recursively if given parameter <recursive> is true.
|
||||
func (r *Resource) Scan(path string, pattern string, recursive ...bool) []*File {
|
||||
if path != "/" {
|
||||
path = strings.TrimRight(path, "/\\")
|
||||
@ -80,7 +99,7 @@ func (r *Resource) Scan(path string, pattern string, recursive ...bool) []*File
|
||||
}
|
||||
if len(recursive) == 0 || !recursive[0] {
|
||||
if strings.IndexByte(name[length:], '/') != -1 {
|
||||
return false
|
||||
return true
|
||||
}
|
||||
}
|
||||
for _, p := range patterns {
|
||||
@ -94,10 +113,11 @@ func (r *Resource) Scan(path string, pattern string, recursive ...bool) []*File
|
||||
return files
|
||||
}
|
||||
|
||||
// Dump prints the files of current resource object.
|
||||
func (r *Resource) Dump() {
|
||||
r.tree.Iterator(func(key, value interface{}) bool {
|
||||
fmt.Printf("%7s %s\n", gfile.FormatSize(value.(*File).FileInfo().Size()), key)
|
||||
return true
|
||||
})
|
||||
fmt.Printf("TOTAL %d FILES", r.tree.Size())
|
||||
fmt.Printf("TOTAL FILES: %d\n", r.tree.Size())
|
||||
}
|
||||
|
||||
@ -7,11 +7,12 @@
|
||||
package gres_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/debug/gdebug"
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
"github.com/gogf/gf/os/gres"
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_Export(t *testing.T) {
|
||||
@ -19,7 +20,7 @@ func Test_Export(t *testing.T) {
|
||||
srcPath := gfile.Dir(gdebug.CallerFilePath()) + "/testdata/files"
|
||||
goFilePath := gfile.Dir(gdebug.CallerFilePath()) + "/testdata/testdata.go"
|
||||
pkgName := "testdata"
|
||||
err := gres.Export(srcPath, goFilePath, pkgName)
|
||||
err := gres.PackToGoFile(srcPath, goFilePath, pkgName)
|
||||
gtest.Assert(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
4
os/gres/testdata/files/root/css/style.css
vendored
Normal file
4
os/gres/testdata/files/root/css/style.css
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
* {
|
||||
font-size: 32px;
|
||||
text-align: center;
|
||||
}
|
||||
BIN
os/gres/testdata/files/root/image/logo.png
vendored
Normal file
BIN
os/gres/testdata/files/root/image/logo.png
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.4 KiB |
9
os/gres/testdata/files/root/index.html
vendored
Normal file
9
os/gres/testdata/files/root/index.html
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<head>
|
||||
<link rel="stylesheet" href="/css/style.css" type="text/css" />
|
||||
</head>
|
||||
<body>
|
||||
<div><img src="image/logo.png"></div>
|
||||
This is the index from gres.
|
||||
</body>
|
||||
</html>
|
||||
2
os/gres/testdata/testdata.go
vendored
2
os/gres/testdata/testdata.go
vendored
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user