mirror of
https://github.com/cnotch/ipchub.git
synced 2025-10-30 02:11:53 +08:00
init commit
This commit is contained in:
37
utils/path.go
Executable file
37
utils/path.go
Executable file
@@ -0,0 +1,37 @@
|
||||
// Copyright (c) 2019,CAOHONGJU All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// CanonicalPath 获取合法的path
|
||||
func CanonicalPath(p string) string {
|
||||
p = strings.ToLower(strings.TrimSpace(p))
|
||||
|
||||
if p == "" {
|
||||
return "/"
|
||||
}
|
||||
|
||||
if p[0] != '/' {
|
||||
p = "/" + p
|
||||
}
|
||||
|
||||
np := path.Clean(p)
|
||||
// path.Clean removes trailing slash except for root;
|
||||
// put the trailing slash back if necessary.
|
||||
if p[len(p)-1] == '/' && np != "/" {
|
||||
// Fast path for common case of p being the string we want:
|
||||
if len(p) == len(np)+1 && strings.HasPrefix(p, np) {
|
||||
np = p
|
||||
} else {
|
||||
np += "/"
|
||||
}
|
||||
}
|
||||
|
||||
return np
|
||||
}
|
||||
Reference in New Issue
Block a user