Skip to content

Commit

Permalink
Create DeleteOldPackageFiles.php
Browse files Browse the repository at this point in the history
  • Loading branch information
bobimicroweber committed May 29, 2024
1 parent 0e3d196 commit 62f33a9
Showing 1 changed file with 101 additions and 0 deletions.
101 changes: 101 additions & 0 deletions app/Console/Commands/DeleteOldPackageFiles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace App\Console\Commands;

use App\Models\Package;
use Illuminate\Console\Command;
use Illuminate\Support\Str;

class DeleteOldPackageFiles extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'package-builder:delete-old-package-files';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Reduce disk space by deleting old package files';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{

$getPackages = Package::where('clone_status', Package::CLONE_STATUS_SUCCESS)->get();
if ($getPackages == null) {
$this->error('No packages. Time: ' . date('Y-m-d H:i:s'));
return 0;
}

$filesForDelete = [];
$whitelistedFiles = [];
foreach ($getPackages as $package) {

$packageJson = json_decode($package->package_json, true);
foreach ($packageJson as $packageName=>$packageVersions) {
$packageWhitelistedVersions = [];
foreach ($packageVersions as $packageVersion) {
$packageNameSlug = str_replace('/', '-', $packageName);
$packageWhitelistedVersions[] = [
'version' => $packageVersion['version'],
'packageFolder' => $packageNameSlug,
'versionFolder'=> str_replace('.', '', $packageVersion['version'])
];
}
$metaPackagePath = (base_path() . '/public/meta/' . $packageNameSlug);
if (!is_dir($metaPackagePath)) {
$this->info('No such directory: ' . $metaPackagePath);
continue;
}
$metaPackagePathVersions = scandir($metaPackagePath);
foreach ($metaPackagePathVersions as $metaPackagePathVersion) {
if ($metaPackagePathVersion == '.' || $metaPackagePathVersion == '..') {
continue;
}
$isVersionWhitelisted = false;
foreach ($packageWhitelistedVersions as $packageWhitelistedVersion) {
if ($metaPackagePathVersion == $packageWhitelistedVersion['versionFolder']) {
$isVersionWhitelisted = true;
break;
}
}
if ($isVersionWhitelisted) {
$whitelistedFiles[] = $metaPackagePath . '/' . $metaPackagePathVersion;
continue;
}

$filesForDelete[] = $metaPackagePath . '/' . $metaPackagePathVersion;

}
}

if (!empty($filesForDelete)) {
foreach ($filesForDelete as $fileForDelete) {
$this->info('Delete: ' . $fileForDelete);
shell_exec('rm -rf ' . $fileForDelete);
}
}
}

return 0;
}
}

0 comments on commit 62f33a9

Please sign in to comment.