Skip to content

Commit

Permalink
UHF-10737: Add command for cleaning previously added files
Browse files Browse the repository at this point in the history
  • Loading branch information
hyrsky committed Dec 18, 2024
1 parent 8f2faa2 commit aeef7b5
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/Drush/Commands/FileCleanerCommands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace Drupal\helfi_azure_fs\Drush\Commands;

use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Database;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\file\FileInterface;
use Drush\Attributes\Command;
use Drush\Commands\AutowireTrait;
use Drush\Commands\DrushCommands;

/**
* Drush commands for cleaning files.
*
* Clean files hat were marked permanent because of misconfigured settings.
* See UHF-10737.
*/
class FileCleanerCommands extends DrushCommands {

use AutowireTrait;

/**
* Constructs a new instance.
*/
public function __construct(
private readonly Connection $connection,
private readonly EntityTypeManagerInterface $entityTypeManager,
) {
parent::__construct();
}

/**
* Transliterates the existing filenames.
*
* @see \Drupal\file\FileUsage\DatabaseFileUsageBackend::listUsage()
*
* @return int
* The exit code.
*/
#[Command(name: 'helfi:clean:permanent-files')]
public function clean(array $options = ['no-dry-run' => FALSE]): int {
$query = $this->connection->select('file_managed', 'f');

// Left join because file_usage might not exist.
$query->leftJoin('file_usage', 'fu', 'f.fid = fu.fid AND fu.count > 0');
$query
->fields('f', ['fid'])
// Permanent only files since temporary files are deleted automatically.
->condition('f.status', FileInterface::STATUS_PERMANENT)
// Filter for rows for which file_usage was not found.
->isNull('fu.fid');

foreach ($query->execute() as $result) {
/** @var \Drupal\file\FileInterface $file */
$file = $this->entityTypeManager
->getStorage('file')
->load($result->fid);

$url = $file->createFileUrl(FALSE);
$this->io()->writeln("Marking {$url} for deletion");

// Temporary files are eventually cleaned by hook_cron.
if ($options['no-dry-run']) {
$file->setTemporary();
$file->save();
}
}

if (!$options['no-dry-run']) {
$this->io()->warning("Dry run: run with --no-dry-run to actually delete the files");
}

return self::EXIT_SUCCESS;
}

}

0 comments on commit aeef7b5

Please sign in to comment.