-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UHF-10737: Add command for cleaning previously added files
- Loading branch information
Showing
1 changed file
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |