48 lines
833 B
Go
48 lines
833 B
Go
package util
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// FileExists Checks if a file with the given file name exists
|
|
func FileExists(fileName string) bool {
|
|
if _, err := os.Stat(fileName); os.IsNotExist(err) {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// JoiningSlash joining http path elements
|
|
func JoiningSlash(elem ...string) string {
|
|
return joiningSlash(elem)
|
|
}
|
|
|
|
func joiningSlash(elem []string) string {
|
|
path := ""
|
|
for _, e := range elem {
|
|
if e != "" {
|
|
if path == "" {
|
|
path = e
|
|
} else {
|
|
path = singleJoiningSlash(path, e)
|
|
}
|
|
}
|
|
}
|
|
return path
|
|
}
|
|
|
|
func singleJoiningSlash(a, b string) string {
|
|
filepath.Join(a, b)
|
|
aslash := strings.HasSuffix(a, "/")
|
|
bslash := strings.HasPrefix(b, "/")
|
|
switch {
|
|
case aslash && bslash:
|
|
return a + b[1:]
|
|
case !aslash && !bslash:
|
|
return a + "/" + b
|
|
}
|
|
return a + b
|
|
}
|