v1.0.9 improved file move and folder deletion logic

This commit is contained in:
zomo
2025-11-14 21:52:14 -06:00
parent ca76fea1da
commit 4de0cee7c6
2 changed files with 48 additions and 41 deletions

89
main.go
View File

@@ -93,7 +93,6 @@ func getDir() (string, error) {
} }
func mainPath(directory string) error { func mainPath(directory string) error {
if err := testEnv(); err != nil { if err := testEnv(); err != nil {
return err return err
} }
@@ -109,8 +108,8 @@ func mainPath(directory string) error {
// read tags // read tags
tags, _ := getTagsInDir(directory) tags, _ := getTagsInDir(directory)
// move files
if tags != nil { if tags != nil {
// move tagged folder
if printLogs { if printLogs {
log.Println("found tags, moving based on tags") log.Println("found tags, moving based on tags")
} }
@@ -119,6 +118,7 @@ func mainPath(directory string) error {
return err return err
} }
} else { } else {
// move untagged folder
if printLogs { if printLogs {
log.Println("no tags found, moving to notags folder") log.Println("no tags found, moving to notags folder")
} }
@@ -127,6 +127,17 @@ func mainPath(directory string) error {
return err return err
} }
} }
// delete folder
time.Sleep(1 * time.Second)
files, err = os.ReadDir(directory)
if err != nil {
return err
}
if len(files) == 0 {
return os.Remove(directory)
}
return nil return nil
} }
@@ -165,10 +176,7 @@ func testEnvFilenameLimit() bool {
return true return true
} }
_, err := strconv.Atoi(limit) _, err := strconv.Atoi(limit)
if err != nil { return err == nil
return false
}
return true
} }
func getEnvFilenameLimit() int { func getEnvFilenameLimit() int {
@@ -238,8 +246,31 @@ func doMoveNotags(directory string) error {
newRoot := os.Getenv("LIBRARY_NOTAGS_ROOT") newRoot := os.Getenv("LIBRARY_NOTAGS_ROOT")
_, folderName := path.Split(directory) _, folderName := path.Split(directory)
toDirectory := path.Join(newRoot, folderName) toDirectory := path.Join(newRoot, folderName)
files, err := os.ReadDir(directory)
if err != nil {
return err
}
return move(directory, toDirectory) for _, file := range files {
if file.IsDir() {
continue
}
if file.Name()[0] == '.' {
continue
}
filefrom := path.Join(directory, file.Name())
fileto := path.Join(toDirectory, file.Name())
err = moveFile(filefrom, fileto)
if err != nil {
return err
}
}
return nil
} }
func doMoveTags(directory string, albumTags tag.Metadata) error { func doMoveTags(directory string, albumTags tag.Metadata) error {
@@ -270,7 +301,7 @@ func doMoveTags(directory string, albumTags tag.Metadata) error {
continue continue
} }
err = doMoveTagged(fname, tags) err = doMoveTaggedFile(fname, tags)
if err != nil { if err != nil {
return err return err
} }
@@ -289,33 +320,12 @@ func doMoveTags(directory string, albumTags tag.Metadata) error {
fname := path.Join(directory, file.Name()) fname := path.Join(directory, file.Name())
err = doMoveTaggedPathonly(fname, albumTags) err = doMoveTaggedFileExtras(fname, albumTags)
if err != nil { if err != nil {
return err return err
} }
} }
// delete folder
files, err = os.ReadDir(directory)
if err != nil {
return err
}
if len(files) == 0 {
return os.Remove(directory)
}
// some files remain, wait a second to see if they go away
time.Sleep(1 * time.Second)
files, err = os.ReadDir(directory)
if err != nil {
return err
}
if len(files) == 0 {
return os.Remove(directory)
}
return nil return nil
} }
@@ -340,7 +350,7 @@ type LibTemplateFile struct {
DiscCount int DiscCount int
} }
func doMoveTagged(filePath string, fileTags tag.Metadata) error { func doMoveTaggedFile(filePath string, fileTags tag.Metadata) error {
libRoot := os.Getenv("LIBRARY_ROOT") libRoot := os.Getenv("LIBRARY_ROOT")
libTemplatePath := os.Getenv("LIBRARY_FILETEMPLATE_PATH") libTemplatePath := os.Getenv("LIBRARY_FILETEMPLATE_PATH")
libTemplateFile := os.Getenv("LIBRARY_FILETEMPLATE_FILE") libTemplateFile := os.Getenv("LIBRARY_FILETEMPLATE_FILE")
@@ -393,10 +403,10 @@ func doMoveTagged(filePath string, fileTags tag.Metadata) error {
libFile := path.Join(libRoot, bufPath.String(), filename + fileExt) libFile := path.Join(libRoot, bufPath.String(), filename + fileExt)
return move(filePath, libFile) return moveFile(filePath, libFile)
} }
func doMoveTaggedPathonly(filePath string, albumTags tag.Metadata) error { func doMoveTaggedFileExtras(filePath string, albumTags tag.Metadata) error {
libRoot := os.Getenv("LIBRARY_ROOT") libRoot := os.Getenv("LIBRARY_ROOT")
libTemplatePath := os.Getenv("LIBRARY_FILETEMPLATE_PATH") libTemplatePath := os.Getenv("LIBRARY_FILETEMPLATE_PATH")
_, fileName := path.Split(filePath) _, fileName := path.Split(filePath)
@@ -418,7 +428,7 @@ func doMoveTaggedPathonly(filePath string, albumTags tag.Metadata) error {
libFile := path.Join(libRoot, bufPath.String(), fileName) libFile := path.Join(libRoot, bufPath.String(), fileName)
return move(filePath, libFile) return moveFile(filePath, libFile)
} }
func ensureDirectoryPath(filePath string) error { func ensureDirectoryPath(filePath string) error {
@@ -426,7 +436,7 @@ func ensureDirectoryPath(filePath string) error {
return os.MkdirAll(directoryPath, 0777) return os.MkdirAll(directoryPath, 0777)
} }
func move(from, to string) error { func moveFile(from, to string) error {
to = cleanPath(to) to = cleanPath(to)
if _, err := os.Stat(to); err == nil { if _, err := os.Stat(to); err == nil {
return fmt.Errorf("file already exists: %s", to) return fmt.Errorf("file already exists: %s", to)
@@ -439,14 +449,11 @@ func move(from, to string) error {
if printLogs { if printLogs {
log.Printf("moving \"%s\" => \"%s\"\n", from, to) log.Printf("moving \"%s\" => \"%s\"\n", from, to)
} }
// os.Rename gives error "invalid cross-device link"
if err := os.Rename(from, to); err != nil { return moveFileWrite(from, to)
return moveFile(from, to)
}
return nil
} }
func moveFile(source, destination string) (err error) { func moveFileWrite(source, destination string) (err error) {
src, err := os.Open(source) src, err := os.Open(source)
if err != nil { if err != nil {
return err return err

Binary file not shown.