2020-05-03 14:40:59 +02:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RelativeName returns the file name relative to directory.
|
2020-05-31 14:42:41 +02:00
|
|
|
func RelativeName(fileName, dir string) string {
|
|
|
|
if fileName == dir {
|
2020-05-22 19:05:16 +02:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-05-31 14:42:41 +02:00
|
|
|
if index := strings.Index(fileName, dir); index == 0 {
|
|
|
|
if index := strings.LastIndex(dir, string(os.PathSeparator)); index == len(dir)-1 {
|
|
|
|
pos := len(dir)
|
2020-05-03 14:40:59 +02:00
|
|
|
return fileName[pos:]
|
2020-05-31 14:42:41 +02:00
|
|
|
} else if index := strings.LastIndex(dir, string(os.PathSeparator)); index != len(dir) {
|
|
|
|
pos := len(dir) + 1
|
2020-05-03 14:40:59 +02:00
|
|
|
return fileName[pos:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileName
|
|
|
|
}
|