Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finishes out the 'october:util purge uploads' placeholder function #4029

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions modules/system/console/OctoberUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Lang;
use File;
use Config;
use System\Models\File as FileModel;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -294,8 +295,46 @@ protected function utilPurgeUploads()
if (!$this->confirmToProceed('This will PERMANENTLY DELETE files in the uploads directory that do not exist in the "system_files" table.')) {
return;
}

// @todo

$totalCount = 0;
$uploadsPath = Config::get('filesystems.disks.local.root', storage_path('app'));
$uploadsPath .= '/uploads';

/*
* Recursive function to scan the directory for files and ensure they exist in system_files.
*/
$purgeFunc = function ($targetDir) use (&$purgeFunc, &$totalCount) {
if ($files = File::glob($targetDir.'/*')) {
foreach ($files as $file) {
if (!is_file($file)) {
continue;
}

if (FileModel::where('disk_name', basename($file))->count()) {
continue;
}
austinderrick marked this conversation as resolved.
Show resolved Hide resolved

$this->info('Purged: '. basename($file));
$totalCount++;
@unlink($file);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@austinderrick Could you check if the file is writable and can be deleted before saying it is purged and trying to unlink it, and indicate to the user if it could not be deleted? It is currently saying it has purged files that may not have actually been removed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, if you don't mind, could you also make the same change to the utilPurgeThumbs() method? :)

}
}

if ($dirs = File::directories($targetDir)) {
foreach ($dirs as $dir) {
$purgeFunc($dir);
}
}
};

$purgeFunc($uploadsPath);

if ($totalCount > 0) {
$this->comment(sprintf('Successfully deleted %s files', $totalCount));
}
else {
$this->comment('No files found to delete');
}
}

protected function utilPurgeOrphans()
Expand Down