mirror of
https://gitee.com/johng/gf
synced 2026-07-07 06:15:15 +08:00
adding gres package for resource feature
This commit is contained in:
68
os/gres/gres.go
Normal file
68
os/gres/gres.go
Normal file
@ -0,0 +1,68 @@
|
||||
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gres
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/container/gtree"
|
||||
"github.com/gogf/gf/encoding/gcompress"
|
||||
"github.com/gogf/gf/internal/utilbytes"
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Resource struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
var (
|
||||
resTree = gtree.NewBTree(10, func(v1, v2 interface{}) int {
|
||||
return strings.Compare(v1.(string), v2.(string))
|
||||
})
|
||||
)
|
||||
|
||||
func Add(content []byte) error {
|
||||
reader, err := zip.NewReader(bytes.NewReader(content), int64(len(content)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, file := range reader.File {
|
||||
resTree.Set(file.Name, file)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func Dump() {
|
||||
resTree.Iterator(func(key, value interface{}) bool {
|
||||
fmt.Printf("%7s %s\n", gfile.FormatSize(value.(*zip.File).FileInfo().Size()), key)
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
||||
func Export(srcPath, goFilePath, pkgName string, keyPrefix ...string) error {
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
err := gcompress.ZipPathWriter(srcPath, buffer, keyPrefix...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return gfile.PutContents(
|
||||
goFilePath,
|
||||
fmt.Sprintf(
|
||||
`package %s
|
||||
|
||||
import "github.com/gogf/gf/os/gres"
|
||||
|
||||
func init() {
|
||||
if err := gres.Add(%s); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
`, pkgName, utilbytes.Export(buffer.Bytes())),
|
||||
)
|
||||
}
|
||||
16
os/gres/gres_file.go
Normal file
16
os/gres/gres_file.go
Normal file
@ -0,0 +1,16 @@
|
||||
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gres
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
)
|
||||
|
||||
// File implements os.FileInfo interface for a given path and size.
|
||||
type File struct {
|
||||
*zip.File
|
||||
}
|
||||
116
os/gres/gres_http.go
Normal file
116
os/gres/gres_http.go
Normal file
@ -0,0 +1,116 @@
|
||||
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gres
|
||||
|
||||
//var (
|
||||
// defaultFileTimestamp = time.Now()
|
||||
//)
|
||||
//
|
||||
//// HttpFile implements http.File interface for a no-directory file with content.
|
||||
//type HttpFile struct {
|
||||
// *bytes.Reader
|
||||
// io.Closer
|
||||
// File
|
||||
//}
|
||||
//
|
||||
//func NewHttpFile(name string, content []byte, timestamp time.Time) *HttpFile {
|
||||
// if timestamp.IsZero() {
|
||||
// timestamp = defaultFileTimestamp
|
||||
// }
|
||||
// return &HttpFile{
|
||||
// bytes.NewReader(content),
|
||||
// ioutil.NopCloser(nil),
|
||||
// File{name, false, int64(len(content)), timestamp},
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//func (f *HttpFile) Readdir(count int) ([]os.FileInfo, error) {
|
||||
// return nil, errors.New("not a directory")
|
||||
//}
|
||||
//
|
||||
//func (f *HttpFile) Size() int64 {
|
||||
// return f.S
|
||||
//}
|
||||
//
|
||||
//func (f *HttpFile) Stat() (os.FileInfo, error) {
|
||||
// return f.FileInfo(), nil
|
||||
//}
|
||||
//
|
||||
//// HttpDirectory implements http.File interface for a directory
|
||||
//type HttpDirectory struct {
|
||||
// HttpFile
|
||||
// ChildrenRead int
|
||||
// Children []os.FileInfo
|
||||
//}
|
||||
//
|
||||
//func NewHttpDirectory(name string, children []string, fs *HttpFileSystem) *HttpDirectory {
|
||||
// infos := make([]os.FileInfo, 0, len(children))
|
||||
// for _, child := range children {
|
||||
// _, err := fs.List(filepath.Join(name, child))
|
||||
// infos = append(infos, &File{child, err == nil, 0, time.Time{}})
|
||||
// }
|
||||
// return &HttpDirectory{
|
||||
// HttpFile{
|
||||
// bytes.NewReader(nil),
|
||||
// ioutil.NopCloser(nil),
|
||||
// File{name, true, 0, time.Time{}},
|
||||
// },
|
||||
// 0,
|
||||
// infos,
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//func (f *HttpDirectory) Readdir(count int) ([]os.FileInfo, error) {
|
||||
// if count <= 0 {
|
||||
// return f.Children, nil
|
||||
// }
|
||||
// if f.ChildrenRead+count > len(f.Children) {
|
||||
// count = len(f.Children) - f.ChildrenRead
|
||||
// }
|
||||
// rv := f.Children[f.ChildrenRead : f.ChildrenRead+count]
|
||||
// f.ChildrenRead += count
|
||||
// return rv, nil
|
||||
//}
|
||||
//
|
||||
//func (f *HttpDirectory) Stat() (os.FileInfo, error) {
|
||||
// return f.FileInfo(), nil
|
||||
//}
|
||||
//
|
||||
//// HttpFileSystem implements http.FileSystem, allowing embedded files to be served from net/http package.
|
||||
//type HttpFileSystem struct {
|
||||
// Data func(path string) ([]byte, error) // Data should return content of file in path if exists
|
||||
// List func(path string) ([]string, error) // List should return list of files in the path
|
||||
// Info func(path string) (os.FileInfo, error) // Info should return the info of file in path if exists
|
||||
// Prefix string // Prefix would be prepended to http requests
|
||||
//}
|
||||
//
|
||||
//func (fs *HttpFileSystem) Open(name string) (http.File, error) {
|
||||
// name = path.Join(fs.Prefix, name)
|
||||
// if len(name) > 0 && name[0] == '/' {
|
||||
// name = name[1:]
|
||||
// }
|
||||
// if b, err := fs.Data(name); err == nil {
|
||||
// timestamp := defaultFileTimestamp
|
||||
// if fs.Info != nil {
|
||||
// if info, err := fs.Info(name); err == nil {
|
||||
// timestamp = info.ModTime()
|
||||
// }
|
||||
// }
|
||||
// return NewHttpFile(name, b, timestamp), nil
|
||||
// }
|
||||
// if children, err := fs.List(name); err == nil {
|
||||
// return NewHttpDirectory(name, children, fs), nil
|
||||
// } else {
|
||||
// // If the error is not found, return an error that will
|
||||
// // result in a 404 error. Otherwise the server returns
|
||||
// // a 500 error for files not found.
|
||||
// if strings.Contains(err.Error(), "not found") {
|
||||
// return nil, os.ErrNotExist
|
||||
// }
|
||||
// return nil, err
|
||||
// }
|
||||
//}
|
||||
25
os/gres/gres_test.go
Normal file
25
os/gres/gres_test.go
Normal file
@ -0,0 +1,25 @@
|
||||
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gres_test
|
||||
|
||||
import (
|
||||
"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) {
|
||||
gtest.Case(t, func() {
|
||||
srcPath := gfile.Dir(gdebug.CallerFilePath()) + "/testdata/files"
|
||||
goFilePath := gfile.Dir(gdebug.CallerFilePath()) + "/testdata/testdata.go"
|
||||
pkgName := "testdata"
|
||||
err := gres.Export(srcPath, goFilePath, pkgName)
|
||||
gtest.Assert(err, nil)
|
||||
})
|
||||
}
|
||||
1
os/gres/testdata/files/dir1/test1
vendored
Normal file
1
os/gres/testdata/files/dir1/test1
vendored
Normal file
@ -0,0 +1 @@
|
||||
test1 content
|
||||
1
os/gres/testdata/files/dir2/test2
vendored
Normal file
1
os/gres/testdata/files/dir2/test2
vendored
Normal file
@ -0,0 +1 @@
|
||||
test2 content
|
||||
9
os/gres/testdata/testdata.go
vendored
Normal file
9
os/gres/testdata/testdata.go
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
package testdata
|
||||
|
||||
import "github.com/gogf/gf/os/gres"
|
||||
|
||||
func init() {
|
||||
if err := gres.Add([]byte{80,75,3,4,20,0,8,0,0,0,147,110,10,79,0,0,0,0,0,0,0,0,0,0,0,0,5,0,9,0,47,100,105,114,49,85,84,5,0,1,38,204,78,93,80,75,7,8,0,0,0,0,0,0,0,0,0,0,0,0,80,75,3,4,20,0,8,0,0,0,147,110,10,79,0,0,0,0,0,0,0,0,0,0,0,0,11,0,9,0,47,100,105,114,49,47,116,101,115,116,49,85,84,5,0,1,38,204,78,93,116,101,115,116,49,32,99,111,110,116,101,110,116,10,80,75,7,8,130,50,74,209,14,0,0,0,14,0,0,0,80,75,3,4,20,0,8,0,0,0,154,110,10,79,0,0,0,0,0,0,0,0,0,0,0,0,5,0,9,0,47,100,105,114,50,85,84,5,0,1,53,204,78,93,80,75,7,8,0,0,0,0,0,0,0,0,0,0,0,0,80,75,3,4,20,0,8,0,0,0,154,110,10,79,0,0,0,0,0,0,0,0,0,0,0,0,11,0,9,0,47,100,105,114,50,47,116,101,115,116,50,85,84,5,0,1,53,204,78,93,116,101,115,116,50,32,99,111,110,116,101,110,116,10,80,75,7,8,129,137,125,58,14,0,0,0,14,0,0,0,80,75,1,2,20,3,20,0,8,0,0,0,147,110,10,79,0,0,0,0,0,0,0,0,0,0,0,0,5,0,9,0,0,0,0,0,0,0,16,0,255,65,0,0,0,0,47,100,105,114,49,85,84,5,0,1,38,204,78,93,80,75,1,2,20,3,20,0,8,0,0,0,147,110,10,79,130,50,74,209,14,0,0,0,14,0,0,0,11,0,9,0,0,0,0,0,0,0,0,0,182,129,60,0,0,0,47,100,105,114,49,47,116,101,115,116,49,85,84,5,0,1,38,204,78,93,80,75,1,2,20,3,20,0,8,0,0,0,154,110,10,79,0,0,0,0,0,0,0,0,0,0,0,0,5,0,9,0,0,0,0,0,0,0,16,0,255,65,140,0,0,0,47,100,105,114,50,85,84,5,0,1,53,204,78,93,80,75,1,2,20,3,20,0,8,0,0,0,154,110,10,79,129,137,125,58,14,0,0,0,14,0,0,0,11,0,9,0,0,0,0,0,0,0,0,0,182,129,200,0,0,0,47,100,105,114,50,47,116,101,115,116,50,85,84,5,0,1,53,204,78,93,80,75,5,6,0,0,0,0,4,0,4,0,252,0,0,0,24,1,0,0,0,0}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user