v1.0.9 improved file move and folder deletion logic
This commit is contained in:
89
main.go
89
main.go
@@ -93,7 +93,6 @@ func getDir() (string, error) {
|
||||
}
|
||||
|
||||
func mainPath(directory string) error {
|
||||
|
||||
if err := testEnv(); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -109,8 +108,8 @@ func mainPath(directory string) error {
|
||||
// read tags
|
||||
tags, _ := getTagsInDir(directory)
|
||||
|
||||
// move files
|
||||
if tags != nil {
|
||||
// move tagged folder
|
||||
if printLogs {
|
||||
log.Println("found tags, moving based on tags")
|
||||
}
|
||||
@@ -119,6 +118,7 @@ func mainPath(directory string) error {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
// move untagged folder
|
||||
if printLogs {
|
||||
log.Println("no tags found, moving to notags folder")
|
||||
}
|
||||
@@ -127,6 +127,17 @@ func mainPath(directory string) error {
|
||||
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
|
||||
}
|
||||
@@ -165,10 +176,7 @@ func testEnvFilenameLimit() bool {
|
||||
return true
|
||||
}
|
||||
_, err := strconv.Atoi(limit)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func getEnvFilenameLimit() int {
|
||||
@@ -238,8 +246,31 @@ func doMoveNotags(directory string) error {
|
||||
newRoot := os.Getenv("LIBRARY_NOTAGS_ROOT")
|
||||
_, folderName := path.Split(directory)
|
||||
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 {
|
||||
@@ -270,7 +301,7 @@ func doMoveTags(directory string, albumTags tag.Metadata) error {
|
||||
continue
|
||||
}
|
||||
|
||||
err = doMoveTagged(fname, tags)
|
||||
err = doMoveTaggedFile(fname, tags)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -289,33 +320,12 @@ func doMoveTags(directory string, albumTags tag.Metadata) error {
|
||||
|
||||
fname := path.Join(directory, file.Name())
|
||||
|
||||
err = doMoveTaggedPathonly(fname, albumTags)
|
||||
err = doMoveTaggedFileExtras(fname, albumTags)
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -340,7 +350,7 @@ type LibTemplateFile struct {
|
||||
DiscCount int
|
||||
}
|
||||
|
||||
func doMoveTagged(filePath string, fileTags tag.Metadata) error {
|
||||
func doMoveTaggedFile(filePath string, fileTags tag.Metadata) error {
|
||||
libRoot := os.Getenv("LIBRARY_ROOT")
|
||||
libTemplatePath := os.Getenv("LIBRARY_FILETEMPLATE_PATH")
|
||||
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)
|
||||
|
||||
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")
|
||||
libTemplatePath := os.Getenv("LIBRARY_FILETEMPLATE_PATH")
|
||||
_, fileName := path.Split(filePath)
|
||||
@@ -418,7 +428,7 @@ func doMoveTaggedPathonly(filePath string, albumTags tag.Metadata) error {
|
||||
|
||||
libFile := path.Join(libRoot, bufPath.String(), fileName)
|
||||
|
||||
return move(filePath, libFile)
|
||||
return moveFile(filePath, libFile)
|
||||
}
|
||||
|
||||
func ensureDirectoryPath(filePath string) error {
|
||||
@@ -426,7 +436,7 @@ func ensureDirectoryPath(filePath string) error {
|
||||
return os.MkdirAll(directoryPath, 0777)
|
||||
}
|
||||
|
||||
func move(from, to string) error {
|
||||
func moveFile(from, to string) error {
|
||||
to = cleanPath(to)
|
||||
if _, err := os.Stat(to); err == nil {
|
||||
return fmt.Errorf("file already exists: %s", to)
|
||||
@@ -439,14 +449,11 @@ func move(from, to string) error {
|
||||
if printLogs {
|
||||
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 moveFile(from, to)
|
||||
}
|
||||
return nil
|
||||
|
||||
return moveFileWrite(from, to)
|
||||
}
|
||||
|
||||
func moveFile(source, destination string) (err error) {
|
||||
func moveFileWrite(source, destination string) (err error) {
|
||||
src, err := os.Open(source)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
BIN
music-rename
BIN
music-rename
Binary file not shown.
Reference in New Issue
Block a user