summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/file/src/Hook/CronHook.php
blob: fdc12547f5d5aec0e429dbc73775b3cb314726c8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php

declare(strict_types=1);

namespace Drupal\file\Hook;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Hook\Attribute\Hook;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\file\FileInterface;
use Drupal\file\FileUsage\FileUsageInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

/**
 * Implements hook_cron().
 */
#[Hook('cron')]
class CronHook {

  public function __construct(
    private readonly EntityTypeManagerInterface $entityTypeManager,
    private readonly StreamWrapperManagerInterface $streamWrapperManager,
    private readonly ConfigFactoryInterface $configFactory,
    private readonly FileUsageInterface $fileUsage,
    private readonly TimeInterface $time,
    #[Autowire('@logger.channel.file')]
    private readonly LoggerInterface $logger,
  ) {}

  /**
   * Implements hook_cron().
   */
  public function __invoke(): void {
    $age = $this->configFactory->get('system.file')->get('temporary_maximum_age');
    $fileStorage = $this->entityTypeManager->getStorage('file');
    // Only delete temporary files if older than $age. Note that automatic
    // cleanup is disabled if $age set to 0.
    if ($age) {
      $fids = $fileStorage->getQuery()->accessCheck(FALSE)->condition('status', FileInterface::STATUS_PERMANENT, '<>')->condition('changed', $this->time->getRequestTime() - $age, '<')->range(0, 100)->execute();
      /** @var \Drupal\file\FileInterface[] $files */
      $files = $fileStorage->loadMultiple($fids);
      foreach ($files as $file) {
        $references = $this->fileUsage->listUsage($file);
        if (empty($references)) {
          if (!file_exists($file->getFileUri())) {
            if (!$this->streamWrapperManager->isValidUri($file->getFileUri())) {
              $this->logger->warning('Temporary file "%path" that was deleted during garbage collection did not exist on the filesystem. This could be caused by a missing stream wrapper.', ['%path' => $file->getFileUri()]);
            }
            else {
              $this->logger->warning('Temporary file "%path" that was deleted during garbage collection did not exist on the filesystem.', ['%path' => $file->getFileUri()]);
            }
          }
          // Delete the file entity. If the file does not exist, this will
          // generate a second notice in the watchdog.
          $file->delete();
        }
        else {
          $this->logger->info('Did not delete temporary file "%path" during garbage collection because it is in use by the following modules: %modules.', [
            '%path' => $file->getFileUri(),
            '%modules' => implode(', ', array_keys($references)),
          ]);
        }
      }
    }
  }

}