This commit is contained in:
Lars Behrends
2025-10-19 22:05:21 +02:00
parent 0f95458466
commit 552bb72370
8 changed files with 560 additions and 70 deletions

View File

@@ -750,8 +750,112 @@ class StashSyncService extends BaseSyncService
}
}
protected function getDeletedCount(): int
protected function executeCleanup(): void
{
return 0; // Stash doesn't provide deletion info in this context
$this->logProgress("Starting cleanup - detecting deleted media in Stash...");
// Clean up scenes
$this->cleanupScenes();
// Clean up movies
$this->cleanupMovies();
$this->logProgress("Cleanup completed. Deleted {$this->deletedCount} items.");
}
private function cleanupScenes(): void
{
$this->logProgress("Checking for deleted scenes...");
try {
// Get all scenes from Stash
$stashScenes = $this->getStashScenes(0, 1000); // Get up to 1000 scenes for cleanup
$stashSceneIds = array_column($stashScenes, 'id');
$this->logProgress("Found " . count($stashSceneIds) . " scenes in Stash");
// Get all scenes from local database for this source
$stmt = $this->pdo->prepare("
SELECT id, metadata FROM adult_videos WHERE source_id = :source_id
");
$stmt->execute(['source_id' => $this->source['id']]);
$localScenes = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$this->logProgress("Found " . count($localScenes) . " scenes in local database");
$deletedCount = 0;
foreach ($localScenes as $localScene) {
$metadata = json_decode($localScene['metadata'], true);
$stashId = $metadata['stash_id'] ?? null;
if ($stashId && !in_array($stashId, $stashSceneIds)) {
// Scene exists in local DB but not in Stash - delete it
$this->deleteAdultVideo($localScene['id']);
$deletedCount++;
$this->deletedCount++;
}
}
$this->logProgress("Deleted {$deletedCount} scenes that no longer exist in Stash");
} catch (Exception $e) {
$this->logProgress("Error during scene cleanup: " . $e->getMessage());
}
}
private function cleanupMovies(): void
{
$this->logProgress("Checking for deleted movies...");
try {
// Get all movies from Stash
$stashMovies = $this->getStashMovies();
$stashMovieIds = array_column($stashMovies, 'id');
$this->logProgress("Found " . count($stashMovieIds) . " movies in Stash");
// Get all movies from local database for this source
$stmt = $this->pdo->prepare("
SELECT id, metadata FROM adult_videos WHERE source_id = :source_id
");
$stmt->execute(['source_id' => $this->source['id']]);
$localVideos = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$this->logProgress("Found " . count($localVideos) . " videos in local database");
$deletedCount = 0;
foreach ($localVideos as $localVideo) {
$metadata = json_decode($localVideo['metadata'], true);
$stashMovieId = $metadata['stash_movie_id'] ?? null;
if ($stashMovieId && !in_array($stashMovieId, $stashMovieIds)) {
// Movie exists in local DB but not in Stash - delete it
$this->deleteAdultVideo($localVideo['id']);
$deletedCount++;
$this->deletedCount++;
}
}
$this->logProgress("Deleted {$deletedCount} movies that no longer exist in Stash");
} catch (Exception $e) {
$this->logProgress("Error during movie cleanup: " . $e->getMessage());
}
}
private function deleteAdultVideo(int $videoId): void
{
try {
// Delete actor relationships first
$stmt = $this->pdo->prepare("DELETE FROM actor_adult_video WHERE adult_video_id = :adult_video_id");
$stmt->execute(['adult_video_id' => $videoId]);
// Delete the video
$stmt = $this->pdo->prepare("DELETE FROM adult_videos WHERE id = :id");
$stmt->execute(['id' => $videoId]);
$this->logProgress("Deleted adult video with ID: {$videoId}");
} catch (Exception $e) {
$this->logProgress("Error deleting adult video {$videoId}: " . $e->getMessage());
}
}
}