github.com/cloudreve/Cloudreve/v3@v3.0.0-20240224133659-3edb00a6484c/pkg/util/path.go (about)

     1  package util
     2  
     3  import (
     4  	"os"
     5  	"path"
     6  	"path/filepath"
     7  	"strings"
     8  )
     9  
    10  // DotPathToStandardPath 将","分割的路径转换为标准路径
    11  func DotPathToStandardPath(path string) string {
    12  	return "/" + strings.Replace(path, ",", "/", -1)
    13  }
    14  
    15  // FillSlash 给路径补全`/`
    16  func FillSlash(path string) string {
    17  	if path == "/" {
    18  		return path
    19  	}
    20  	return path + "/"
    21  }
    22  
    23  // RemoveSlash 移除路径最后的`/`
    24  func RemoveSlash(path string) string {
    25  	if len(path) > 1 {
    26  		return strings.TrimSuffix(path, "/")
    27  	}
    28  	return path
    29  }
    30  
    31  // SplitPath 分割路径为列表
    32  func SplitPath(path string) []string {
    33  	if len(path) == 0 || path[0] != '/' {
    34  		return []string{}
    35  	}
    36  
    37  	if path == "/" {
    38  		return []string{"/"}
    39  	}
    40  
    41  	pathSplit := strings.Split(path, "/")
    42  	pathSplit[0] = "/"
    43  	return pathSplit
    44  }
    45  
    46  // FormSlash 将path中的反斜杠'\'替换为'/'
    47  func FormSlash(old string) string {
    48  	return path.Clean(strings.ReplaceAll(old, "\\", "/"))
    49  }
    50  
    51  // RelativePath 获取相对可执行文件的路径
    52  func RelativePath(name string) string {
    53  	if filepath.IsAbs(name) {
    54  		return name
    55  	}
    56  	e, _ := os.Executable()
    57  	return filepath.Join(filepath.Dir(e), name)
    58  }
    59