error('Cloud storage not enabled. Exiting...'); return; } $deleteEnabled = config('media.delete_local_after_cloud'); if(!$deleteEnabled) { $this->error('Delete local storage after cloud upload is not enabled'); return; } $limit = $this->option('limit'); $hugeMode = $this->option('huge'); $log = $this->option('log-errors'); if($limit > 2000 && !$hugeMode) { $this->error('Limit exceeded, please use a limit under 2000 or run again with the --huge flag'); return; } $minId = Media::orderByDesc('id')->where('created_at', '<', now()->subHours(12))->first()->id; return $hugeMode ? $this->hugeMode($minId, $limit, $log) : $this->regularMode($minId, $limit, $log); } protected function regularMode($minId, $limit, $log) { $gc = Media::whereRemoteMedia(false) ->whereNotNull(['status_id', 'cdn_url', 'replicated_at']) ->whereNot('version', '4') ->where('id', '<', $minId) ->inRandomOrder() ->take($limit) ->get(); $totalSize = 0; $bar = $this->output->createProgressBar($gc->count()); $bar->start(); $cloudDisk = Storage::disk(config('filesystems.cloud')); $localDisk = Storage::disk('local'); foreach($gc as $media) { if( $cloudDisk->exists($media->media_path) ) { if( $localDisk->exists($media->media_path)) { $localDisk->delete($media->media_path); $media->version = 4; $media->save(); $totalSize = $totalSize + $media->size; } else { $media->version = 4; $media->save(); } } else { if($log) { Log::channel('media')->info('[GC] Local media not properly persisted to cloud storage', ['media_id' => $media->id]); } } $bar->advance(); } $bar->finish(); $this->line(' '); $this->info('Finished!'); if($totalSize) { $this->info('Cleared ' . $totalSize . ' bytes of media from local disk!'); } return 0; } protected function hugeMode($minId, $limit, $log) { $cloudDisk = Storage::disk(config('filesystems.cloud')); $localDisk = Storage::disk('local'); $bar = $this->output->createProgressBar($limit); $bar->start(); Media::whereRemoteMedia(false) ->whereNotNull(['status_id', 'cdn_url', 'replicated_at']) ->whereNot('version', '4') ->where('id', '<', $minId) ->chunk(50, function($medias) use($cloudDisk, $localDisk, $bar, $log) { foreach($medias as $media) { if($cloudDisk->exists($media->media_path)) { if( $localDisk->exists($media->media_path)) { $localDisk->delete($media->media_path); $media->version = 4; $media->save(); } else { $media->version = 4; $media->save(); } } else { if($log) { Log::channel('media')->info('[GC] Local media not properly persisted to cloud storage', ['media_id' => $media->id]); } } $bar->advance(); } }); $bar->finish(); $this->line(' '); $this->info('Finished!'); } }