Skip to content

Commit

Permalink
tec: Clean empty directories when cleaning media
Browse files Browse the repository at this point in the history
  • Loading branch information
marienfressinaud committed Oct 10, 2023
1 parent 220832d commit 87346a9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/cli/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ public function clean(): mixed
if (!$file_names_to_delete) {
yield Response::text(200, "Nothing to delete under {$subdir_name}/.");
}

yield Response::text(200, "Cleaning empty dirs under {$subdir_name}/...");

$this->cleanTreeEmptyDirectories("{$path_cards}/{$subdir_name}");
$this->cleanTreeEmptyDirectories("{$path_covers}/{$subdir_name}");
$this->cleanTreeEmptyDirectories("{$path_large}/{$subdir_name}");

yield Response::text(200, "Cleaned empty dirs under {$subdir_name}/.");
}

if ($count_deleted > 0) {
Expand All @@ -101,4 +109,37 @@ public function clean(): mixed
yield Response::text(200, 'No files deleted.');
}
}

private function cleanTreeEmptyDirectories(string $directory): bool
{
if (!is_dir($directory)) {
return false;
}

$files = scandir($directory);

if ($files === false) {
return false;
}

$files = array_diff($files, ['.', '..']);

foreach ($files as $file) {
$filepath = "{$directory}/{$file}";

if (is_dir($filepath)) {
$can_delete_dir = $this->cleanTreeEmptyDirectories($filepath);
} else {
$can_delete_dir = false;
}

if (!$can_delete_dir) {
return false;
}
}

rmdir($directory);

return true;
}
}
11 changes: 11 additions & 0 deletions tests/cli/MediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,16 @@ public function testCleanDeletesUnusedFiles(): void
$this->assertResponseCode($response, 200);
$this->assertResponseEquals($response, "Deleted file {$image_filename}");
$response->next();
$response->next();
$response->next();
$this->assertResponseCode($response, 200);
$this->assertResponseEquals($response, '1 files deleted.');
$this->assertFalse(file_exists($card_filepath));
$this->assertFalse(file_exists($cover_filepath));
$this->assertFalse(file_exists($large_filepath));
$this->assertFalse(file_exists(dirname($card_filepath)));
$this->assertFalse(file_exists(dirname($cover_filepath)));
$this->assertFalse(file_exists(dirname($large_filepath)));
}

public function testCleanKeepsFilesUsedByLinks(): void
Expand Down Expand Up @@ -127,6 +132,8 @@ public function testCleanKeepsFilesUsedByLinks(): void
$this->assertResponseCode($response, 200);
$this->assertResponseEquals($response, "Nothing to delete under {$subdir_name}/.");
$response->next();
$response->next();
$response->next();
$this->assertResponseCode($response, 200);
$this->assertResponseEquals($response, 'No files deleted.');
$this->assertTrue(file_exists($card_filepath));
Expand Down Expand Up @@ -171,6 +178,8 @@ public function testCleanKeepsFilesUsedByCollections(): void
$this->assertResponseCode($response, 200);
$this->assertResponseEquals($response, "Nothing to delete under {$subdir_name}/.");
$response->next();
$response->next();
$response->next();
$this->assertResponseCode($response, 200);
$this->assertResponseEquals($response, 'No files deleted.');
$this->assertTrue(file_exists($card_filepath));
Expand Down Expand Up @@ -215,6 +224,8 @@ public function testCleanKeepsFilesUsedByTopics(): void
$this->assertResponseCode($response, 200);
$this->assertResponseEquals($response, "Nothing to delete under {$subdir_name}/.");
$response->next();
$response->next();
$response->next();
$this->assertResponseCode($response, 200);
$this->assertResponseEquals($response, 'No files deleted.');
$this->assertTrue(file_exists($card_filepath));
Expand Down

0 comments on commit 87346a9

Please sign in to comment.