gf/os/gres/gres.go

84 lines
2.8 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). 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.
2019-08-14 22:03:52 +08:00
// Package gres provides resource management and packing/unpacking feature between files and bytes.
package gres
2019-08-31 18:04:12 +08:00
const (
// Separator for directories.
Separator = "/"
)
var (
2019-08-13 21:06:11 +08:00
// Default resource object.
defaultResource = Instance()
)
2019-08-13 21:06:11 +08:00
// 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 string, prefix ...string) error {
2019-08-13 13:45:01 +08:00
return defaultResource.Add(content, prefix...)
}
2019-08-13 21:06:11 +08:00
// 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.
2019-08-13 13:45:01 +08:00
func Load(path string, prefix ...string) error {
return defaultResource.Load(path, prefix...)
}
2019-08-13 21:06:11 +08:00
// Get returns the file with given path.
func Get(path string) *File {
return defaultResource.Get(path)
}
2019-08-14 22:18:31 +08:00
// GetWithIndex searches file with <path>, if the file is directory
// it then does index files searching under this directory.
//
// GetWithIndex is usually used for http static file service.
func GetWithIndex(path string, indexFiles []string) *File {
return defaultResource.GetWithIndex(path, indexFiles)
}
2019-08-16 00:29:14 +08:00
// GetContent directly returns the content of <path> in default resource object.
func GetContent(path string) []byte {
return defaultResource.GetContent(path)
}
2019-08-13 21:06:11 +08:00
// Contains checks whether the <path> exists in the default resource object.
func Contains(path string) bool {
return defaultResource.Contains(path)
}
2019-08-19 21:02:44 +08:00
// IsEmpty checks and returns whether the resource manager is empty.
func IsEmpty() bool {
return defaultResource.tree.IsEmpty()
}
2019-08-31 18:04:12 +08:00
// ScanDir returns the files under the given path, the parameter <path> should be a folder type.
2019-08-13 21:06:11 +08:00
//
// 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.
2019-08-31 18:04:12 +08:00
func ScanDir(path string, pattern string, recursive ...bool) []*File {
return defaultResource.ScanDir(path, pattern, recursive...)
}
// ScanDirFile returns all sub-files with absolute paths of given <path>,
// It scans directory recursively if given parameter <recursive> is true.
//
// Note that it returns only files, exclusive of directories.
func ScanDirFile(path string, pattern string, recursive ...bool) []*File {
return defaultResource.ScanDirFile(path, pattern, recursive...)
}
2019-08-13 13:45:01 +08:00
2019-08-13 21:06:11 +08:00
// Dump prints the files of the default resource object.
2019-08-13 13:45:01 +08:00
func Dump() {
defaultResource.Dump()
}