github.com/gogf/gf/v2@v2.7.4/os/gres/gres.go (about)

     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  
     7  // Package gres provides resource management and packing/unpacking feature between files and bytes.
     8  package gres
     9  
    10  const (
    11  	// Separator for directories.
    12  	Separator = "/"
    13  )
    14  
    15  var (
    16  	// Default resource object.
    17  	defaultResource = Instance()
    18  )
    19  
    20  // Add unpacks and adds the `content` into the default resource object.
    21  // The unnecessary parameter `prefix` indicates the prefix
    22  // for each file storing into current resource object.
    23  func Add(content string, prefix ...string) error {
    24  	return defaultResource.Add(content, prefix...)
    25  }
    26  
    27  // Load loads, unpacks and adds the data from `path` into the default resource object.
    28  // The unnecessary parameter `prefix` indicates the prefix
    29  // for each file storing into current resource object.
    30  func Load(path string, prefix ...string) error {
    31  	return defaultResource.Load(path, prefix...)
    32  }
    33  
    34  // Get returns the file with given path.
    35  func Get(path string) *File {
    36  	return defaultResource.Get(path)
    37  }
    38  
    39  // GetWithIndex searches file with `path`, if the file is directory
    40  // it then does index files searching under this directory.
    41  //
    42  // GetWithIndex is usually used for http static file service.
    43  func GetWithIndex(path string, indexFiles []string) *File {
    44  	return defaultResource.GetWithIndex(path, indexFiles)
    45  }
    46  
    47  // GetContent directly returns the content of `path` in default resource object.
    48  func GetContent(path string) []byte {
    49  	return defaultResource.GetContent(path)
    50  }
    51  
    52  // Contains checks whether the `path` exists in the default resource object.
    53  func Contains(path string) bool {
    54  	return defaultResource.Contains(path)
    55  }
    56  
    57  // IsEmpty checks and returns whether the resource manager is empty.
    58  func IsEmpty() bool {
    59  	return defaultResource.tree.IsEmpty()
    60  }
    61  
    62  // ScanDir returns the files under the given path, the parameter `path` should be a folder type.
    63  //
    64  // The pattern parameter `pattern` supports multiple file name patterns,
    65  // using the ',' symbol to separate multiple patterns.
    66  //
    67  // It scans directory recursively if given parameter `recursive` is true.
    68  func ScanDir(path string, pattern string, recursive ...bool) []*File {
    69  	return defaultResource.ScanDir(path, pattern, recursive...)
    70  }
    71  
    72  // ScanDirFile returns all sub-files with absolute paths of given `path`,
    73  // It scans directory recursively if given parameter `recursive` is true.
    74  //
    75  // Note that it returns only files, exclusive of directories.
    76  func ScanDirFile(path string, pattern string, recursive ...bool) []*File {
    77  	return defaultResource.ScanDirFile(path, pattern, recursive...)
    78  }
    79  
    80  // Export exports and saves specified path `src` and all its sub files to specified system path `dst` recursively.
    81  func Export(src, dst string, option ...ExportOption) error {
    82  	return defaultResource.Export(src, dst, option...)
    83  }
    84  
    85  // Dump prints the files of the default resource object.
    86  func Dump() {
    87  	defaultResource.Dump()
    88  }