feature: updated GetFirstIndexedNonExistingFilePath logic
parent
18f289fe90
commit
bbe979b97b
|
@ -17,15 +17,26 @@ func IsFileExists(fileLocation string) bool {
|
|||
|
||||
func GetFirstIndexedNonExistingFilePath(parentDir string, fileBaseName string) string {
|
||||
filePath := filepath.Join(parentDir, fileBaseName)
|
||||
index := 0
|
||||
|
||||
if !IsFileExists(filePath) {
|
||||
return filePath
|
||||
}
|
||||
|
||||
fileIndex := 0
|
||||
fileBaseNameWithoutExtension := GetSimpleNameWithoutExtension(fileBaseName)
|
||||
fileExtension := GetExtension(fileBaseName)
|
||||
if fileExtension != "" {
|
||||
fileExtension = "." + fileExtension
|
||||
}
|
||||
|
||||
for {
|
||||
if !IsFileExists(filePath) {
|
||||
break
|
||||
}
|
||||
|
||||
index++
|
||||
fileIndex++
|
||||
|
||||
filePath = filepath.Join(parentDir, fmt.Sprintf("%s-%d", fileBaseName, index))
|
||||
filePath = filepath.Join(parentDir, fmt.Sprintf("%s-%d%s", fileBaseNameWithoutExtension, fileIndex, fileExtension))
|
||||
}
|
||||
|
||||
return filePath
|
||||
|
|
|
@ -26,6 +26,10 @@ func GetSimpleName(filePath string) string {
|
|||
|
||||
func GetExtension(filePath string) string {
|
||||
split := strings.Split(filePath, ".")
|
||||
if len(split) == 1 {
|
||||
return ""
|
||||
}
|
||||
|
||||
return split[len(split)-1]
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue