From e6dc858061ffcf5b71e239202d419c217fd78afa Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Wed, 5 Feb 2020 00:44:52 -0700 Subject: [PATCH] Update StoryGC command --- app/Console/Commands/StoryGC.php | 41 ++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/app/Console/Commands/StoryGC.php b/app/Console/Commands/StoryGC.php index f6271bb15..82eda89f9 100644 --- a/app/Console/Commands/StoryGC.php +++ b/app/Console/Commands/StoryGC.php @@ -44,6 +44,47 @@ class StoryGC extends Command * @return mixed */ public function handle() + { + $this->directoryScan(); + $this->deleteViews(); + $this->deleteStories(); + } + + protected function directoryScan() + { + $day = now()->day; + + if($day !== 3) { + 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); + } + } + } + + protected function deleteDirectory($path) + { + Storage::deleteDirectory($path); + } + + protected function deleteViews() + { + StoryView::where('created_at', '<', now()->subDays(2))->delete(); + } + + protected function deleteStories() { $stories = Story::where('expires_at', '<', now())->take(50)->get();