Reverted and extended generateFilename function

This commit is contained in:
bytedream 2022-03-21 10:11:33 +01:00
parent 31e03c1e08
commit 98f5da3bf3

View file

@ -21,7 +21,7 @@ import (
var ( var (
// ahh i love windows :))) // ahh i love windows :)))
invalidWindowsChars = []string{"<", ">", ":", "\"", "/", "|", "\\", "?", "*"} invalidWindowsChars = []string{"\\", "<", ">", ":", "\"", "/", "|", "?", "*"}
invalidNotWindowsChars = []string{"/"} invalidNotWindowsChars = []string{"/"}
) )
@ -167,7 +167,29 @@ func terminalWidth() int {
return 60 return 60
} }
func generateFilename(name string) string { func generateFilename(name, directory string) string {
if runtime.GOOS != "windows" {
for _, char := range invalidNotWindowsChars {
name = strings.ReplaceAll(name, char, "")
}
out.Debug("Replaced invalid characters (not windows)")
} else {
for _, char := range invalidWindowsChars {
name = strings.ReplaceAll(name, char, "")
}
// this needs only to be done on windows lol :)
if directory != "" {
for _, char := range invalidWindowsChars[1:] {
directory = strings.ReplaceAll(directory, char, "")
}
}
out.Debug("Replaced invalid characters (windows)")
}
if directory != "" {
name = filepath.Join(directory, name)
}
filename, changed := freeFileName(name) filename, changed := freeFileName(name)
if changed { if changed {
out.Info("File %s already exists, changing name to %s", name, filename) out.Info("File %s already exists, changing name to %s", name, filename)
@ -276,7 +298,7 @@ type formatInformation struct {
Subtitle crunchyroll.LOCALE `json:"subtitle"` Subtitle crunchyroll.LOCALE `json:"subtitle"`
} }
func (fi formatInformation) Format(source string, removeInvalidChars bool) string { func (fi formatInformation) Format(source string) string {
fields := reflect.TypeOf(fi) fields := reflect.TypeOf(fi)
values := reflect.ValueOf(fi) values := reflect.ValueOf(fi)
@ -296,18 +318,6 @@ func (fi formatInformation) Format(source string, removeInvalidChars bool) strin
} }
} }
if removeInvalidChars {
if runtime.GOOS != "windows" {
for _, char := range invalidNotWindowsChars {
valueAsString = strings.ReplaceAll(valueAsString, char, "")
}
} else {
for _, char := range invalidWindowsChars {
valueAsString = strings.ReplaceAll(valueAsString, char, "")
}
}
}
source = strings.ReplaceAll(source, "{"+fields.Field(i).Tag.Get("json")+"}", valueAsString) source = strings.ReplaceAll(source, "{"+fields.Field(i).Tag.Get("json")+"}", valueAsString)
} }