2015-10-26 20:47:42 +01:00
< ? php
/**
* Pony . fm - A community for pony fan music .
2021-02-14 03:39:15 +01:00
* Copyright ( C ) 2015 Kelvin Zhang .
2015-10-26 20:47:42 +01:00
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU Affero General Public License for more details .
*
* You should have received a copy of the GNU Affero General Public License
* along with this program . If not , see < http :// www . gnu . org / licenses />.
*/
2021-02-14 03:34:58 +01:00
namespace App\Console\Commands ;
2015-10-26 20:47:42 +01:00
2021-02-14 03:39:15 +01:00
use App\Models\TrackFile ;
2015-10-26 20:47:42 +01:00
use Carbon\Carbon ;
2021-02-14 20:45:51 +01:00
use Illuminate\Support\Facades\File ;
2015-10-26 20:47:42 +01:00
use Illuminate\Console\Command ;
class ClearTrackCache extends Command
{
/**
* The name and signature of the console command .
*
* @ var string
*/
protected $signature = ' track - cache : clear
2015-10-27 18:29:51 +01:00
{ -- tracks = expired : Clear only [ expired ] ( default ) or [ all ] cached tracks . }
{ -- force : Skip all prompts . } ' ;
2015-10-26 20:47:42 +01:00
/**
* The console command description .
*
* @ var string
*/
protected $description = 'Clears cached tracks. Defaults to expired tracks. Usage: php artisan track-cache:clear [--tracks=expired|all]' ;
/**
* Create a new command instance .
*/
public function __construct ()
{
parent :: __construct ();
}
/**
* Execute the console command .
*
* @ return mixed
*/
public function handle ()
{
2015-11-01 17:49:28 +01:00
if ( $this -> option ( 'tracks' ) === 'all' ) {
// Get all cacheable track files
$trackFiles = TrackFile :: where ( 'is_cacheable' , true )
-> with ( 'track.album' )
-> get ();
2015-10-26 20:47:42 +01:00
} else {
2015-11-01 17:49:28 +01:00
// Get all expired track files
2015-10-26 20:47:42 +01:00
$trackFiles = TrackFile :: where ( 'is_cacheable' , true )
2015-11-01 17:49:28 +01:00
-> where ( 'expires_at' , '<=' , Carbon :: now ())
-> with ( 'track.album' )
2015-10-26 20:47:42 +01:00
-> get ();
}
2015-11-01 17:49:28 +01:00
// Delete above track files
2015-10-26 20:47:42 +01:00
if ( count ( $trackFiles ) === 0 ) {
$this -> info ( 'No tracks found. Exiting.' );
} else {
2021-02-14 03:39:15 +01:00
if ( $this -> option ( 'force' ) || $this -> confirm ( count ( $trackFiles ) . ' cacheable track files found. Proceed to delete their files if they exist? [y|N]' , false )) {
2015-10-26 20:47:42 +01:00
$count = 0 ;
foreach ( $trackFiles as $trackFile ) {
// Set expiration to null (so can be re-cached upon request)
2015-11-01 17:49:28 +01:00
$trackFile -> expires_at = null ;
2015-10-26 20:47:42 +01:00
$trackFile -> update ();
2015-11-01 17:49:28 +01:00
// Delete file if exists
2015-10-26 20:47:42 +01:00
if ( File :: exists ( $trackFile -> getFile ())) {
$count ++ ;
File :: delete ( $trackFile -> getFile ());
2021-02-14 03:39:15 +01:00
$this -> info ( 'Deleted ' . $trackFile -> getFile ());
2015-10-26 20:47:42 +01:00
}
}
2021-02-14 03:39:15 +01:00
$this -> info ( $count . ' files deleted. Deletion complete. Exiting.' );
2015-10-26 20:47:42 +01:00
} else {
$this -> info ( 'Deletion cancelled. Exiting.' );
}
}
}
2016-01-01 01:12:30 +01:00
}