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

Finish the "october:util purge uploads" artisan command #4518

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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
53 changes: 52 additions & 1 deletion modules/system/console/OctoberUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use System\Classes\UpdateManager;
use System\Classes\CombineAssets;
use Exception;
use System\Models\File as FileModel;
use System\Models\Parameter;

/**
Expand Down Expand Up @@ -294,7 +295,57 @@ protected function utilPurgeUploads()
return;
}

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

Choose a reason for hiding this comment

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

@bennothommo just an idea, maybe it would be useful to take uploads path from configs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@w20k Good idea, will change it around soon.

Copy link
Contributor

Choose a reason for hiding this comment

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

@bennothommo did you implement that suggestion?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@LukeTowers not yet, still on the to-do list.


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

if (File::isDirectoryEmpty($dir) && is_writeable($dir)) {
rmdir($dir);
$this->info('Removed folder: '. str_replace($uploadsPath, '', $dir));
}
}
}

foreach ($files as $file) {
if (!is_file($file)) {
continue;
}

if (!is_writeable($file)) {
$this->warn('Unable to purge file: ' . str_replace($uploadsPath, '', $file));
continue;
}

if (FileModel::where('disk_name', basename($file))->count()) {
$this->warn('Skipped file in use: '. str_replace($uploadsPath, '', $file));
continue;
}

// Skip .gitignore files
if ($file === '.gitignore') {
w20k marked this conversation as resolved.
Show resolved Hide resolved
continue;
}

$this->info('Purged: '. str_replace($uploadsPath, '', $file));
$totalCount++;
unlink($file);
}
}
};

$purgeFunc($uploadsPath);

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

protected function utilPurgeOrphans()
Expand Down