Files
WMS/Modules/M3/Console/DeleteOldLabels.php
T
2026-09-16 14:28:17 +01:00

109 lines
3.3 KiB
PHP

<?php
namespace Modules\M3\Console;
use Illuminate\Console\Command;
use Modules\M3\Http\Controllers\Controller;
use DB;
use Config;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use Log;
use Str;
use Storage;
use File;
class DeleteOldLabels extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'command:DeleteOldLabelsCmd';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Delete Old Labels Command';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle(): void
{
//$this->info('Start CallCloseAPI...');
ini_set('max_execution_time', Config::get('arrays.PHPTimeOutCL'));
ini_set('max_input_time', Config::get('arrays.PHPTimeOutCL'));
ini_set('default_socket_timeout', Config::get('arrays.PHPTimeOutCL'));
ini_set('memory_limit', '1024M');
try {
$this->info("Start Delete Old Labels Command...");
Log::channel('dailyCron')->info("Cron...Start Delete Old Labels Command...");
$labelsDirectoryTmp = '/www/sentm3live/htdocs/labels/';
$subFolders = ['CarrierLabel/','ContentLabel/','PackingList/']; // ,'CarrierLabel/','ContentLabel','PackingList',''
$daysOld = 180; // Number of days to consider a file old : 180 =Six Months
$filesToDeleteArr = [];
foreach ($subFolders as $key => $subFolder) {
$labelsDirectory = $labelsDirectoryTmp.$subFolder;
$this->info("Getting Files From Directory: ".$labelsDirectory);
Log::channel('dailyCron')->info("Getting Files From Directory: ".$labelsDirectory);
exec('find '.$labelsDirectory.' -ctime +180 -type f -exec rm {} \;');
# exec('find '.$labelsDirectory.' -ctime +190 -type f -print | wc -l');
}
$labelsFolder = $labelsDirectoryTmp;
# Get Files from the specified Directory
$filesToDelete = File::allFiles($labelsFolder);
foreach ($filesToDelete as $file) {
$lastAccessTime = new \DateTime("@".$file->getATime());
$now = new \DateTime();
$diff = $now->diff($lastAccessTime)->days;
if ($diff >= $daysOld) {
$this->info("File: ".$file." is Days Old: ".$diff);
// Log::channel('dailyCron')->info("File: ".$file." is Days Old: ".$diff);
# Add the File to Array
$filesToDeleteArr[] = $file;
}
}
// $this->info("Files to Delete...");
// dump($filesToDeleteArr);
# Delete Old Files
File::delete($filesToDeleteArr);
Log::channel('dailyCron')->info("Cron...End Delete Old Labels Command...");
$this->info("End Delete Old Labels Command...");
}
catch (\Throwable $th) {
// Log::error($th);
Log::channel('dailyCron')->error($th);
}
}
}