Add package to trash files
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1 +1,2 @@
|
||||
photorg
|
||||
temp/
|
||||
photorg
|
||||
|
2
go.mod
2
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
|
||||
|
2
go.sum
2
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=
|
||||
|
37
main.go
37
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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user