2020-04-06 22:09:45 +02:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
2020-05-08 12:01:22 +02:00
|
|
|
"strconv"
|
2020-04-06 22:09:45 +02:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-07-11 16:46:29 +02:00
|
|
|
// StripExt removes the file type extension from a file name (if any).
|
|
|
|
func StripExt(name string) string {
|
|
|
|
if end := strings.LastIndex(name, "."); end != -1 {
|
|
|
|
name = name[:end]
|
|
|
|
}
|
|
|
|
|
|
|
|
return name
|
|
|
|
}
|
2020-04-06 22:09:45 +02:00
|
|
|
|
2020-07-11 16:46:29 +02:00
|
|
|
// StripKnownExt removes a known file type extension from a file name (if any).
|
|
|
|
func StripKnownExt(name string) string {
|
|
|
|
if FileExt.Known(name) {
|
|
|
|
name = StripExt(name)
|
2020-04-06 22:09:45 +02:00
|
|
|
}
|
|
|
|
|
2020-07-11 16:46:29 +02:00
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2020-07-14 11:00:49 +02:00
|
|
|
// StripSequence removes common sequence patterns at the end of file names.
|
|
|
|
func StripSequence(name string) string {
|
2020-06-29 11:10:24 +02:00
|
|
|
// Strip numeric extensions like .00000, .00001, .4542353245,.... (at least 5 digits).
|
2020-07-11 16:46:29 +02:00
|
|
|
if dot := strings.LastIndex(name, "."); dot != -1 && len(name[dot+1:]) >= 5 {
|
|
|
|
if i, err := strconv.Atoi(name[dot+1:]); err == nil && i >= 0 {
|
|
|
|
name = name[:dot]
|
2020-05-08 12:01:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 11:10:24 +02:00
|
|
|
// Other common sequential naming schemes.
|
2020-07-11 16:46:29 +02:00
|
|
|
if end := strings.Index(name, "("); end != -1 {
|
2020-06-29 11:10:24 +02:00
|
|
|
// Copies created by Chrome & Windows, example: IMG_1234 (2).
|
2020-07-11 16:46:29 +02:00
|
|
|
name = name[:end]
|
|
|
|
} else if end := strings.Index(name, " copy"); end != -1 {
|
2020-06-29 11:10:24 +02:00
|
|
|
// Copies created by OS X, example: IMG_1234 copy 2.
|
2020-07-11 16:46:29 +02:00
|
|
|
name = name[:end]
|
2020-04-06 22:09:45 +02:00
|
|
|
}
|
|
|
|
|
2020-07-11 16:46:29 +02:00
|
|
|
name = strings.TrimSpace(name)
|
2020-06-01 09:45:24 +02:00
|
|
|
|
2020-07-11 16:46:29 +02:00
|
|
|
return name
|
2020-04-06 22:09:45 +02:00
|
|
|
}
|
|
|
|
|
2020-07-14 11:00:49 +02:00
|
|
|
// BasePrefix returns the filename base without any extensions and path.
|
|
|
|
func BasePrefix(fileName string, stripSequence bool) string {
|
|
|
|
name := StripKnownExt(StripExt(filepath.Base(fileName)))
|
|
|
|
|
|
|
|
if !stripSequence {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
return StripSequence(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RelPrefix returns the relative filename.
|
|
|
|
func RelPrefix(fileName, dir string, stripSequence bool) string {
|
|
|
|
if name := RelName(fileName, dir); name != "" {
|
|
|
|
return AbsPrefix(name, stripSequence)
|
2020-05-31 14:42:41 +02:00
|
|
|
}
|
|
|
|
|
2020-07-14 11:00:49 +02:00
|
|
|
return BasePrefix(fileName, stripSequence)
|
2020-05-31 14:42:41 +02:00
|
|
|
}
|
|
|
|
|
2020-07-14 11:00:49 +02:00
|
|
|
// AbsPrefix returns the directory and base filename without any extensions.
|
|
|
|
func AbsPrefix(fileName string, stripSequence bool) string {
|
|
|
|
return filepath.Join(filepath.Dir(fileName), BasePrefix(fileName, stripSequence))
|
2020-04-06 22:09:45 +02:00
|
|
|
}
|