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

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