Files
gf/os/gres/gres.go

117 lines
3.6 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
2024-12-15 21:54:47 +08:00
import (
"io/fs"
"github.com/gogf/gf/v2/os/gres/internal/defines"
"github.com/gogf/gf/v2/os/gres/internal/fs_mixed"
"github.com/gogf/gf/v2/os/gres/internal/fs_res"
"github.com/gogf/gf/v2/os/gres/internal/fs_std"
)
type (
FS = defines.FS
File = defines.File
// Deprecated: used PackOption instead.
Option = defines.PackOption
PackOption = defines.PackOption
ExportOption = defines.ExportOption
)
var (
2024-12-09 23:43:29 +08:00
// Default resource file system.
2024-12-15 21:54:47 +08:00
defaultFS = fs_res.NewFS()
2024-12-09 23:43:29 +08:00
2019-08-13 21:06:11 +08:00
// Default resource object.
defaultResource = Instance()
)
2024-12-15 21:54:47 +08:00
func NewResFS() *fs_res.FS {
return fs_res.NewFS()
}
func NewStdFS(fs fs.FS) *fs_std.FS {
return fs_std.NewFS(fs)
}
func NewMixedFS(resFS *fs_res.FS, stdFs fs.FS) *fs_mixed.FS {
return fs_mixed.NewFS(resFS, stdFs)
}
// Add unpacks and adds the `content` into the default resource object.
// The unnecessary parameter `prefix` indicates the prefix
2019-08-13 21:06:11 +08:00
// for each file storing into current resource object.
func Add(content string, prefix ...string) error {
2024-12-09 23:43:29 +08:00
return defaultFS.Add(content, prefix...)
}
// Load loads, unpacks and adds the data from `path` into the default resource object.
// The unnecessary parameter `prefix` indicates the prefix
2019-08-13 21:06:11 +08:00
// for each file storing into current resource object.
2019-08-13 13:45:01 +08:00
func Load(path string, prefix ...string) error {
2024-12-09 23:43:29 +08:00
return defaultFS.Load(path, prefix...)
}
2019-08-13 21:06:11 +08:00
// Get returns the file with given path.
2024-12-09 23:43:29 +08:00
func Get(path string) File {
2019-08-13 21:06:11 +08:00
return defaultResource.Get(path)
}
// GetWithIndex searches file with `path`, if the file is directory
2019-08-14 22:18:31 +08:00
// it then does index files searching under this directory.
//
// GetWithIndex is usually used for http static file service.
2024-12-09 23:43:29 +08:00
func GetWithIndex(path string, indexFiles []string) File {
2019-08-14 22:18:31 +08:00
return defaultResource.GetWithIndex(path, indexFiles)
}
// GetContent directly returns the content of `path` in default resource object.
2019-08-16 00:29:14 +08:00
func GetContent(path string) []byte {
return defaultResource.GetContent(path)
}
// Contains checks whether the `path` exists in the default resource object.
2019-08-13 21:06:11 +08:00
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 {
2024-12-09 22:35:40 +08:00
return defaultResource.IsEmpty()
2019-08-19 21:02:44 +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,
2019-08-13 21:06:11 +08:00
// using the ',' symbol to separate multiple patterns.
//
// It scans directory recursively if given parameter `recursive` is true.
2024-12-09 23:43:29 +08:00
func ScanDir(path string, pattern string, recursive ...bool) []File {
2019-08-31 18:04:12 +08:00
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.
2019-08-31 18:04:12 +08:00
//
// Note that it returns only files, exclusive of directories.
2024-12-09 23:43:29 +08:00
func ScanDirFile(path string, pattern string, recursive ...bool) []File {
2019-08-31 18:04:12 +08:00
return defaultResource.ScanDirFile(path, pattern, recursive...)
}
2019-08-13 13:45:01 +08:00
2021-12-14 23:01:20 +08:00
// Export exports and saves specified path `src` and all its sub files to specified system path `dst` recursively.
func Export(src, dst string, option ...ExportOption) error {
return defaultResource.Export(src, dst, option...)
2021-12-14 23:01:20 +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()
}