cleanup.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env php
  2. <?php
  3. declare(strict_types=1);
  4. use App\App\Bootstrap;
  5. use App\Storage\FileSystem;
  6. require dirname(__DIR__) . '/src/autoload.php';
  7. Bootstrap::init(false);
  8. $app = Bootstrap::config('app');
  9. $draftDir = rtrim((string) $app['storage']['drafts'], '/');
  10. $submissionDir = rtrim((string) $app['storage']['submissions'], '/');
  11. $uploadDir = rtrim((string) $app['storage']['uploads'], '/');
  12. $draftDays = (int) ($app['retention']['draft_days'] ?? 14);
  13. $submissionDays = (int) ($app['retention']['submission_days'] ?? 90);
  14. $now = time();
  15. $deletedDrafts = 0;
  16. $deletedSubmissions = 0;
  17. $draftFiles = glob($draftDir . '/*.json') ?: [];
  18. foreach ($draftFiles as $file) {
  19. $raw = file_get_contents($file);
  20. $data = is_string($raw) ? json_decode($raw, true) : null;
  21. $updatedAt = strtotime((string) ($data['updated_at'] ?? ''));
  22. if ($updatedAt > 0 && ($now - $updatedAt) > ($draftDays * 86400)) {
  23. unlink($file);
  24. $deletedDrafts++;
  25. }
  26. }
  27. $submissionFiles = glob($submissionDir . '/*.json') ?: [];
  28. foreach ($submissionFiles as $file) {
  29. $raw = file_get_contents($file);
  30. $data = is_string($raw) ? json_decode($raw, true) : null;
  31. $submittedAt = strtotime((string) ($data['submitted_at'] ?? ''));
  32. if ($submittedAt > 0 && ($now - $submittedAt) > ($submissionDays * 86400)) {
  33. $applicationKey = (string) ($data['application_key'] ?? pathinfo($file, PATHINFO_FILENAME));
  34. unlink($file);
  35. FileSystem::removeTree($uploadDir . '/' . $applicationKey);
  36. $deletedSubmissions++;
  37. }
  38. }
  39. $message = sprintf(
  40. 'Cleanup abgeschlossen: drafts=%d, submissions=%d',
  41. $deletedDrafts,
  42. $deletedSubmissions
  43. );
  44. Bootstrap::log('cleanup', $message);
  45. if (PHP_SAPI === 'cli') {
  46. echo $message . PHP_EOL;
  47. }