2020-01-01 04:58:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
use Illuminate\Console\Command;
|
2021-04-28 02:02:33 +00:00
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
use App\Story;
|
|
|
|
use App\StoryView;
|
2020-01-01 04:58:21 +00:00
|
|
|
|
|
|
|
class StoryGC extends Command
|
|
|
|
{
|
2021-04-28 02:02:33 +00:00
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $signature = 'story:gc';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = 'Clear expired Stories';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new command instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
$this->directoryScan();
|
|
|
|
$this->deleteViews();
|
|
|
|
$this->deleteStories();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function directoryScan()
|
|
|
|
{
|
2021-05-01 01:59:56 +00:00
|
|
|
$day = now()->day;
|
2021-04-28 02:02:33 +00:00
|
|
|
|
2021-05-01 01:59:56 +00:00
|
|
|
if($day !== 3) {
|
2021-04-28 02:02:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$monthHash = substr(hash('sha1', date('Y').date('m')), 0, 12);
|
|
|
|
|
|
|
|
$t1 = Storage::directories('public/_esm.t1');
|
|
|
|
$t2 = Storage::directories('public/_esm.t2');
|
|
|
|
|
|
|
|
$dirs = array_merge($t1, $t2);
|
|
|
|
|
|
|
|
foreach($dirs as $dir) {
|
|
|
|
$hash = last(explode('/', $dir));
|
|
|
|
if($hash != $monthHash) {
|
|
|
|
$this->info('Found directory to delete: ' . $dir);
|
|
|
|
$this->deleteDirectory($dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$mh = hash('sha256', date('Y').'-.-'.date('m'));
|
|
|
|
$monthHash = date('Y').date('m').substr($mh, 0, 6).substr($mh, 58, 6);
|
|
|
|
$dirs = Storage::directories('public/_esm.t3');
|
|
|
|
|
|
|
|
foreach($dirs as $dir) {
|
|
|
|
$hash = last(explode('/', $dir));
|
|
|
|
if($hash != $monthHash) {
|
|
|
|
$this->info('Found directory to delete: ' . $dir);
|
|
|
|
$this->deleteDirectory($dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function deleteDirectory($path)
|
|
|
|
{
|
|
|
|
Storage::deleteDirectory($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function deleteViews()
|
|
|
|
{
|
|
|
|
StoryView::where('created_at', '<', now()->subMinutes(1441))->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function deleteStories()
|
|
|
|
{
|
|
|
|
$stories = Story::where('created_at', '>', now()->subMinutes(30))
|
|
|
|
->whereNull('active')
|
|
|
|
->get();
|
|
|
|
|
|
|
|
foreach($stories as $story) {
|
|
|
|
if(Storage::exists($story->path) == true) {
|
|
|
|
Storage::delete($story->path);
|
|
|
|
}
|
|
|
|
DB::transaction(function() use($story) {
|
|
|
|
StoryView::whereStoryId($story->id)->delete();
|
|
|
|
$story->delete();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
$stories = Story::where('created_at', '<', now()
|
|
|
|
->subMinutes(1441))
|
|
|
|
->get();
|
|
|
|
|
|
|
|
if($stories->count() == 0) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($stories as $story) {
|
|
|
|
if(Storage::exists($story->path) == true) {
|
|
|
|
Storage::delete($story->path);
|
|
|
|
}
|
|
|
|
DB::transaction(function() use($story) {
|
|
|
|
StoryView::whereStoryId($story->id)->delete();
|
|
|
|
$story->delete();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-01-01 04:58:21 +00:00
|
|
|
}
|