-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetFileName.go
More file actions
46 lines (40 loc) · 1.14 KB
/
GetFileName.go
File metadata and controls
46 lines (40 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package devwatch
import (
"errors"
"path/filepath"
)
// GetFileName returns the filename from a path
// Example: "theme/index.html" -> "index.html"
func GetFileName(path string) (string, error) {
if path == "" {
return "", errors.New("GetFileName empty path")
}
// Check if path ends with a separator (either / or \)
if len(path) > 0 && (path[len(path)-1] == '/' || path[len(path)-1] == '\\') {
return "", errors.New("GetFileName invalid path: ends with separator")
}
// Normalize backslashes to slashes for cross-platform compatibility
normPath := path
for i := 0; i < len(normPath); i++ {
if normPath[i] == '\\' {
// Replace all backslashes with slashes
normPath = ""
for j := 0; j < len(path); j++ {
if path[j] == '\\' {
normPath += "/"
} else {
normPath += string(path[j])
}
}
break
}
}
fileName := filepath.Base(normPath)
if fileName == "." || fileName == string(filepath.Separator) {
return "", errors.New("GetFileName invalid path")
}
if len(normPath) > 0 && normPath[len(normPath)-1] == filepath.Separator {
return "", errors.New("GetFileName invalid path")
}
return fileName, nil
}