From bf33a525556c4c2074774c00ccff57f4116c5f0c Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Wed, 23 Jul 2025 17:54:12 +0300 Subject: [PATCH] Add package to trash files --- .gitignore | 3 ++- go.mod | 2 ++ go.sum | 2 ++ main.go | 37 +++++++++++++------------------------ 4 files changed, 19 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index 8fb43d7..33b190b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -photorg \ No newline at end of file +temp/ +photorg diff --git a/go.mod b/go.mod index 8d182a4..a5507df 100644 --- a/go.mod +++ b/go.mod @@ -6,3 +6,5 @@ require ( github.com/barasher/go-exiftool v1.10.0 github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd ) + +require github.com/rkoesters/xdg v0.0.1 diff --git a/go.sum b/go.sum index 9fcf36c..c5c604b 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rkoesters/xdg v0.0.1 h1:RmfYxghVvIsb4d51u5LtNOcwqY5r3P44u6o86qqvBMA= +github.com/rkoesters/xdg v0.0.1/go.mod h1:5DcbjvJkY00fIOKkaBnylbC/rmc1NNJP5dmUcnlcm7U= github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd h1:CmH9+J6ZSsIjUK3dcGsnCnO41eRBOnY12zwkn5qVwgc= github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd/go.mod h1:hPqNNc0+uJM6H+SuU8sEs5K5IQeKccPqeSjfgcKGgPk= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/main.go b/main.go index 12ae46f..aaa236d 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,7 @@ import ( "time" "github.com/barasher/go-exiftool" + "github.com/rkoesters/xdg/trash" "github.com/rwcarlsen/goexif/exif" ) @@ -116,14 +117,23 @@ func organizePhotos(config Config) error { } if config.DryRun { - fmt.Printf("[DRY RUN] Would move: %s -> %s\n", moveOp.SourcePath, moveOp.DestPath) + if moveOp.DropSource { + fmt.Printf("[DRY RUN] Would trash: %s\n", moveOp.SourcePath) + } else { + fmt.Printf("[DRY RUN] Would move: %s -> %s\n", moveOp.SourcePath, moveOp.DestPath) + } + return nil } if err := moveFile(moveOp); err != nil { log.Printf("Error moving file %s: %v", moveOp.SourcePath, err) } else { - fmt.Printf("Moved: %s -> %s\n", moveOp.SourcePath, moveOp.DestPath) + if moveOp.DropSource { + fmt.Printf("Trash: %s\n", moveOp.SourcePath) + } else { + fmt.Printf("Moved: %s -> %s\n", moveOp.SourcePath, moveOp.DestPath) + } } return nil @@ -348,30 +358,9 @@ func moveFile(moveOp *MoveOperation) error { // Проверяем, нужно ли удалить исходный файл (если файлы идентичны) if moveOp.DropSource { // Файлы идентичны, удаляем исходный в корзину - return moveToTrash(moveOp.SourcePath) + return trash.Trash(moveOp.SourcePath) } // Перемещаем файл return os.Rename(moveOp.SourcePath, moveOp.DestPath) } - -func moveToTrash(path string) error { - // Простая реализация перемещения в корзину для Linux - homeDir, err := os.UserHomeDir() - if err != nil { - return fmt.Errorf("cannot get home directory: %v", err) - } - - trashDir := filepath.Join(homeDir, ".local/share/Trash/files") - if err := os.MkdirAll(trashDir, 0755); err != nil { - return fmt.Errorf("cannot create trash directory: %v", err) - } - - fileName := filepath.Base(path) - trashPath := filepath.Join(trashDir, fileName) - - // Если файл с таким именем уже есть в корзине, добавляем суффикс - trashPath = generateUniqueFileName(trashPath) - - return os.Rename(path, trashPath) -}