Add package to trash files

This commit is contained in:
2025-07-23 17:54:12 +03:00
parent b787e81823
commit bf33a52555
4 changed files with 19 additions and 25 deletions

3
.gitignore vendored
View File

@@ -1 +1,2 @@
photorg
temp/
photorg

2
go.mod
View File

@@ -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

2
go.sum
View File

@@ -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=

37
main.go
View File

@@ -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)
}