2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2018-05-03 13:35:08 +08:00
|
|
|
//
|
|
|
|
// 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,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-05-03 13:35:08 +08:00
|
|
|
|
2019-01-15 23:27:47 +08:00
|
|
|
// Package gspath implements file index and search for folders.
|
2019-06-19 09:06:52 +08:00
|
|
|
//
|
2019-10-12 23:56:03 +08:00
|
|
|
// It searches file internally with high performance in order by the directory adding sequence.
|
2020-03-16 20:46:03 +08:00
|
|
|
// Note that:
|
|
|
|
// If caching feature enabled, there would be a searching delay after adding/deleting files.
|
2018-05-03 13:35:08 +08:00
|
|
|
package gspath
|
|
|
|
|
|
|
|
import (
|
2021-06-26 16:23:54 +08:00
|
|
|
"context"
|
2019-06-19 09:06:52 +08:00
|
|
|
"os"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
2019-07-29 21:01:19 +08:00
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/container/garray"
|
|
|
|
"github.com/gogf/gf/v2/container/gmap"
|
2021-11-15 20:49:02 +08:00
|
|
|
"github.com/gogf/gf/v2/errors/gcode"
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
|
|
"github.com/gogf/gf/v2/internal/intlog"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/os/gfile"
|
|
|
|
"github.com/gogf/gf/v2/text/gstr"
|
2018-05-03 13:35:08 +08:00
|
|
|
)
|
|
|
|
|
2019-10-12 23:56:03 +08:00
|
|
|
// SPath manages the path searching feature.
|
2018-05-03 13:35:08 +08:00
|
|
|
type SPath struct {
|
2019-10-12 23:56:03 +08:00
|
|
|
paths *garray.StrArray // The searching directories array.
|
|
|
|
cache *gmap.StrStrMap // Searching cache map, it is not enabled if it's nil.
|
2018-05-03 13:35:08 +08:00
|
|
|
}
|
|
|
|
|
2019-10-12 23:56:03 +08:00
|
|
|
// SPathCacheItem is a cache item for searching.
|
2018-11-16 18:20:09 +08:00
|
|
|
type SPathCacheItem struct {
|
2019-10-12 23:56:03 +08:00
|
|
|
path string // Absolute path for file/dir.
|
|
|
|
isDir bool // Is directory or not.
|
2018-11-16 18:20:09 +08:00
|
|
|
}
|
|
|
|
|
2018-11-30 09:48:57 +08:00
|
|
|
var (
|
2019-10-12 23:56:03 +08:00
|
|
|
// Path to searching object mapping, used for instance management.
|
|
|
|
pathsMap = gmap.NewStrAnyMap(true)
|
2018-11-30 09:48:57 +08:00
|
|
|
)
|
|
|
|
|
2019-10-12 23:56:03 +08:00
|
|
|
// New creates and returns a new path searching manager.
|
2019-03-12 00:24:31 +08:00
|
|
|
func New(path string, cache bool) *SPath {
|
2019-06-19 09:06:52 +08:00
|
|
|
sp := &SPath{
|
2019-09-05 11:38:36 +08:00
|
|
|
paths: garray.NewStrArray(true),
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
if cache {
|
2019-07-23 23:20:27 +08:00
|
|
|
sp.cache = gmap.NewStrStrMap(true)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
if len(path) > 0 {
|
|
|
|
if _, err := sp.Add(path); err != nil {
|
2021-11-15 20:49:02 +08:00
|
|
|
// intlog.Print(err)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return sp
|
2018-11-30 09:48:57 +08:00
|
|
|
}
|
|
|
|
|
2022-11-07 17:55:49 +08:00
|
|
|
// Get creates and returns an instance of searching manager for given path.
|
2021-02-06 11:42:58 +08:00
|
|
|
// The parameter `cache` specifies whether using cache feature for this manager.
|
2019-10-12 23:56:03 +08:00
|
|
|
// If cache feature is enabled, it asynchronously and recursively scans the path
|
|
|
|
// and updates all sub files/folders to the cache using package gfsnotify.
|
2019-03-12 00:24:31 +08:00
|
|
|
func Get(root string, cache bool) *SPath {
|
2019-08-16 00:29:14 +08:00
|
|
|
if root == "" {
|
|
|
|
root = "/"
|
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
return pathsMap.GetOrSetFuncLock(root, func() interface{} {
|
|
|
|
return New(root, cache)
|
|
|
|
}).(*SPath)
|
2018-05-03 13:35:08 +08:00
|
|
|
}
|
|
|
|
|
2021-02-06 11:42:58 +08:00
|
|
|
// Search searches file `name` under path `root`.
|
2022-11-07 17:55:49 +08:00
|
|
|
// The parameter `root` should be an absolute path. It will not automatically
|
2021-02-06 11:42:58 +08:00
|
|
|
// convert `root` to absolute path for performance reason.
|
|
|
|
// The optional parameter `indexFiles` specifies the searching index files when the result is a directory.
|
|
|
|
// For example, if the result `filePath` is a directory, and `indexFiles` is [index.html, main.html], it will also
|
|
|
|
// search [index.html, main.html] under `filePath`. It returns the absolute file path if any of them found,
|
|
|
|
// or else it returns `filePath`.
|
2019-06-19 09:06:52 +08:00
|
|
|
func Search(root string, name string, indexFiles ...string) (filePath string, isDir bool) {
|
|
|
|
return Get(root, false).Search(name, indexFiles...)
|
2019-03-12 00:24:31 +08:00
|
|
|
}
|
|
|
|
|
2021-02-06 11:42:58 +08:00
|
|
|
// SearchWithCache searches file `name` under path `root` with cache feature enabled.
|
2022-11-07 17:55:49 +08:00
|
|
|
// The parameter `root` should be an absolute path. It will not automatically
|
2021-02-06 11:42:58 +08:00
|
|
|
// convert `root` to absolute path for performance reason.
|
|
|
|
// The optional parameter `indexFiles` specifies the searching index files when the result is a directory.
|
|
|
|
// For example, if the result `filePath` is a directory, and `indexFiles` is [index.html, main.html], it will also
|
|
|
|
// search [index.html, main.html] under `filePath`. It returns the absolute file path if any of them found,
|
|
|
|
// or else it returns `filePath`.
|
2019-06-19 09:06:52 +08:00
|
|
|
func SearchWithCache(root string, name string, indexFiles ...string) (filePath string, isDir bool) {
|
|
|
|
return Get(root, true).Search(name, indexFiles...)
|
2018-11-30 09:48:57 +08:00
|
|
|
}
|
|
|
|
|
2019-10-12 23:56:03 +08:00
|
|
|
// Set deletes all other searching directories and sets the searching directory for this manager.
|
2018-11-16 18:20:09 +08:00
|
|
|
func (sp *SPath) Set(path string) (realPath string, err error) {
|
2019-06-19 09:06:52 +08:00
|
|
|
realPath = gfile.RealPath(path)
|
|
|
|
if realPath == "" {
|
|
|
|
realPath, _ = sp.Search(path)
|
|
|
|
if realPath == "" {
|
|
|
|
realPath = gfile.RealPath(gfile.Pwd() + gfile.Separator + path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if realPath == "" {
|
2021-08-24 21:18:59 +08:00
|
|
|
return realPath, gerror.NewCodef(gcode.CodeInvalidParameter, `path "%s" does not exist`, path)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2019-10-12 23:56:03 +08:00
|
|
|
// The set path must be a directory.
|
2019-06-19 09:06:52 +08:00
|
|
|
if gfile.IsDir(realPath) {
|
|
|
|
realPath = strings.TrimRight(realPath, gfile.Separator)
|
|
|
|
if sp.paths.Search(realPath) != -1 {
|
|
|
|
for _, v := range sp.paths.Slice() {
|
|
|
|
sp.removeMonitorByPath(v)
|
|
|
|
}
|
|
|
|
}
|
2021-06-26 16:23:54 +08:00
|
|
|
intlog.Print(context.TODO(), "paths clear:", sp.paths)
|
2019-06-19 09:06:52 +08:00
|
|
|
sp.paths.Clear()
|
|
|
|
if sp.cache != nil {
|
|
|
|
sp.cache.Clear()
|
|
|
|
}
|
|
|
|
sp.paths.Append(realPath)
|
|
|
|
sp.updateCacheByPath(realPath)
|
|
|
|
sp.addMonitorByPath(realPath)
|
|
|
|
return realPath, nil
|
|
|
|
} else {
|
2021-08-24 21:18:59 +08:00
|
|
|
return "", gerror.NewCode(gcode.CodeInvalidParameter, path+" should be a folder")
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2018-05-03 13:35:08 +08:00
|
|
|
}
|
|
|
|
|
2019-10-12 23:56:03 +08:00
|
|
|
// Add adds more searching directory to the manager.
|
|
|
|
// The manager will search file in added order.
|
2018-11-16 18:20:09 +08:00
|
|
|
func (sp *SPath) Add(path string) (realPath string, err error) {
|
2019-06-19 09:06:52 +08:00
|
|
|
realPath = gfile.RealPath(path)
|
|
|
|
if realPath == "" {
|
|
|
|
realPath, _ = sp.Search(path)
|
|
|
|
if realPath == "" {
|
|
|
|
realPath = gfile.RealPath(gfile.Pwd() + gfile.Separator + path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if realPath == "" {
|
2021-08-24 21:18:59 +08:00
|
|
|
return realPath, gerror.NewCodef(gcode.CodeInvalidParameter, `path "%s" does not exist`, path)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2019-10-12 23:56:03 +08:00
|
|
|
// The added path must be a directory.
|
2019-06-19 09:06:52 +08:00
|
|
|
if gfile.IsDir(realPath) {
|
2021-11-15 20:49:02 +08:00
|
|
|
// fmt.Println("gspath:", realPath, sp.paths.Search(realPath))
|
2019-10-12 23:56:03 +08:00
|
|
|
// It will not add twice for the same directory.
|
2019-06-19 09:06:52 +08:00
|
|
|
if sp.paths.Search(realPath) < 0 {
|
|
|
|
realPath = strings.TrimRight(realPath, gfile.Separator)
|
|
|
|
sp.paths.Append(realPath)
|
|
|
|
sp.updateCacheByPath(realPath)
|
|
|
|
sp.addMonitorByPath(realPath)
|
|
|
|
}
|
|
|
|
return realPath, nil
|
|
|
|
} else {
|
2021-08-24 21:18:59 +08:00
|
|
|
return "", gerror.NewCode(gcode.CodeInvalidParameter, path+" should be a folder")
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2018-05-03 13:35:08 +08:00
|
|
|
}
|
|
|
|
|
2021-02-06 11:42:58 +08:00
|
|
|
// Search searches file `name` in the manager.
|
|
|
|
// The optional parameter `indexFiles` specifies the searching index files when the result is a directory.
|
|
|
|
// For example, if the result `filePath` is a directory, and `indexFiles` is [index.html, main.html], it will also
|
|
|
|
// search [index.html, main.html] under `filePath`. It returns the absolute file path if any of them found,
|
|
|
|
// or else it returns `filePath`.
|
2019-06-19 09:06:52 +08:00
|
|
|
func (sp *SPath) Search(name string, indexFiles ...string) (filePath string, isDir bool) {
|
2019-10-12 23:56:03 +08:00
|
|
|
// No cache enabled.
|
2019-06-19 09:06:52 +08:00
|
|
|
if sp.cache == nil {
|
|
|
|
sp.paths.LockFunc(func(array []string) {
|
|
|
|
path := ""
|
|
|
|
for _, v := range array {
|
2019-10-12 23:56:03 +08:00
|
|
|
path = gfile.Join(v, name)
|
2019-12-25 21:22:06 +08:00
|
|
|
if stat, err := os.Stat(path); stat != nil && !os.IsNotExist(err) {
|
2019-10-12 23:56:03 +08:00
|
|
|
path = gfile.Abs(path)
|
|
|
|
// Security check: the result file path must be under the searching directory.
|
|
|
|
if len(path) >= len(v) && path[:len(v)] == v {
|
|
|
|
filePath = path
|
|
|
|
isDir = stat.IsDir()
|
|
|
|
break
|
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if len(indexFiles) > 0 && isDir {
|
|
|
|
if name == "/" {
|
|
|
|
name = ""
|
|
|
|
}
|
|
|
|
path := ""
|
|
|
|
for _, file := range indexFiles {
|
|
|
|
path = filePath + gfile.Separator + file
|
|
|
|
if gfile.Exists(path) {
|
|
|
|
filePath = path
|
|
|
|
isDir = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2019-10-12 23:56:03 +08:00
|
|
|
// Using cache feature.
|
2019-06-19 09:06:52 +08:00
|
|
|
name = sp.formatCacheName(name)
|
|
|
|
if v := sp.cache.Get(name); v != "" {
|
|
|
|
filePath, isDir = sp.parseCacheValue(v)
|
|
|
|
if len(indexFiles) > 0 && isDir {
|
|
|
|
if name == "/" {
|
|
|
|
name = ""
|
|
|
|
}
|
|
|
|
for _, file := range indexFiles {
|
2021-12-02 20:55:41 +08:00
|
|
|
if v = sp.cache.Get(name + "/" + file); v != "" {
|
2019-06-19 09:06:52 +08:00
|
|
|
return sp.parseCacheValue(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2018-11-17 02:39:23 +08:00
|
|
|
}
|
|
|
|
|
2021-02-06 11:42:58 +08:00
|
|
|
// Remove deletes the `path` from cache files of the manager.
|
2022-11-07 17:55:49 +08:00
|
|
|
// The parameter `path` can be either an absolute path or just a relative file name.
|
2018-11-17 11:17:02 +08:00
|
|
|
func (sp *SPath) Remove(path string) {
|
2019-06-19 09:06:52 +08:00
|
|
|
if sp.cache == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if gfile.Exists(path) {
|
|
|
|
for _, v := range sp.paths.Slice() {
|
|
|
|
name := gstr.Replace(path, v, "")
|
|
|
|
name = sp.formatCacheName(name)
|
|
|
|
sp.cache.Remove(name)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
name := sp.formatCacheName(path)
|
|
|
|
sp.cache.Remove(name)
|
|
|
|
}
|
2018-11-17 11:17:02 +08:00
|
|
|
}
|
|
|
|
|
2019-10-12 23:56:03 +08:00
|
|
|
// Paths returns all searching directories.
|
2018-11-25 22:18:36 +08:00
|
|
|
func (sp *SPath) Paths() []string {
|
2019-06-19 09:06:52 +08:00
|
|
|
return sp.paths.Slice()
|
2018-11-25 22:18:36 +08:00
|
|
|
}
|
|
|
|
|
2019-10-12 23:56:03 +08:00
|
|
|
// AllPaths returns all paths cached in the manager.
|
2018-11-17 02:39:23 +08:00
|
|
|
func (sp *SPath) AllPaths() []string {
|
2019-06-19 09:06:52 +08:00
|
|
|
if sp.cache == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
paths := sp.cache.Keys()
|
|
|
|
if len(paths) > 0 {
|
|
|
|
sort.Strings(paths)
|
|
|
|
}
|
|
|
|
return paths
|
2018-05-31 12:07:31 +08:00
|
|
|
}
|
|
|
|
|
2019-10-12 23:56:03 +08:00
|
|
|
// Size returns the count of the searching directories.
|
2018-05-31 12:07:31 +08:00
|
|
|
func (sp *SPath) Size() int {
|
2019-06-19 09:06:52 +08:00
|
|
|
return sp.paths.Len()
|
2018-11-16 18:20:09 +08:00
|
|
|
}
|