gres.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. // Package gres provides resource management and packing/unpacking feature between files and bytes.
  7. package gres
  8. const (
  9. // Separator for directories.
  10. Separator = "/"
  11. )
  12. var (
  13. // Default resource object.
  14. defaultResource = Instance()
  15. )
  16. // Add unpacks and adds the `content` into the default resource object.
  17. // The unnecessary parameter `prefix` indicates the prefix
  18. // for each file storing into current resource object.
  19. func Add(content string, prefix ...string) error {
  20. return defaultResource.Add(content, prefix...)
  21. }
  22. // Load loads, unpacks and adds the data from `path` into the default resource object.
  23. // The unnecessary parameter `prefix` indicates the prefix
  24. // for each file storing into current resource object.
  25. func Load(path string, prefix ...string) error {
  26. return defaultResource.Load(path, prefix...)
  27. }
  28. // Get returns the file with given path.
  29. func Get(path string) *File {
  30. return defaultResource.Get(path)
  31. }
  32. // GetWithIndex searches file with `path`, if the file is directory
  33. // it then does index files searching under this directory.
  34. //
  35. // GetWithIndex is usually used for http static file service.
  36. func GetWithIndex(path string, indexFiles []string) *File {
  37. return defaultResource.GetWithIndex(path, indexFiles)
  38. }
  39. // GetContent directly returns the content of `path` in default resource object.
  40. func GetContent(path string) []byte {
  41. return defaultResource.GetContent(path)
  42. }
  43. // Contains checks whether the `path` exists in the default resource object.
  44. func Contains(path string) bool {
  45. return defaultResource.Contains(path)
  46. }
  47. // IsEmpty checks and returns whether the resource manager is empty.
  48. func IsEmpty() bool {
  49. return defaultResource.tree.IsEmpty()
  50. }
  51. // ScanDir returns the files under the given path, the parameter `path` should be a folder type.
  52. //
  53. // The pattern parameter `pattern` supports multiple file name patterns,
  54. // using the ',' symbol to separate multiple patterns.
  55. //
  56. // It scans directory recursively if given parameter `recursive` is true.
  57. func ScanDir(path string, pattern string, recursive ...bool) []*File {
  58. return defaultResource.ScanDir(path, pattern, recursive...)
  59. }
  60. // ScanDirFile returns all sub-files with absolute paths of given `path`,
  61. // It scans directory recursively if given parameter `recursive` is true.
  62. //
  63. // Note that it returns only files, exclusive of directories.
  64. func ScanDirFile(path string, pattern string, recursive ...bool) []*File {
  65. return defaultResource.ScanDirFile(path, pattern, recursive...)
  66. }
  67. // Export exports and saves specified path `src` and all its sub files to specified system path `dst` recursively.
  68. func Export(src, dst string, option ...ExportOption) error {
  69. return defaultResource.Export(src, dst, option...)
  70. }
  71. // Dump prints the files of the default resource object.
  72. func Dump() {
  73. defaultResource.Dump()
  74. }