2018-09-29 21:33:52 -07:00
< ? php
namespace App\Console\Commands ;
use Illuminate\Console\Command ;
use Illuminate\Support\Facades\Storage ;
class MoveUploadsToNewDisk extends Command
{
/**
* The name and signature of the console command .
*
* @ var string
*/
protected $signature = 'snipeit:move-uploads {delete_local?}' ;
/**
* The console command description .
*
* @ var string
*/
2020-08-28 14:10:28 -07:00
protected $description = 'This will move your locally uploaded files to whatever your current disk is.' ;
2018-09-29 21:33:52 -07:00
/**
* Create a new command instance .
*
* @ return void
*/
public function __construct ()
{
parent :: __construct ();
}
/**
* Execute the console command .
*
* @ return mixed
*/
public function handle ()
{
if ( config ( 'filesystems.default' ) == 'local' ) {
$this -> error ( 'Your current disk is set to local so we cannot proceed.' );
2020-08-28 14:10:28 -07:00
$this -> warn ( " Please configure your .env settings for S3. \n Change your PUBLIC_FILESYSTEM_DISK value to 's3_public' and your PRIVATE_FILESYSTEM_DISK to s3_private. " );
2018-09-29 21:33:52 -07:00
return false ;
}
$delete_local = $this -> argument ( 'delete_local' );
2020-11-12 21:46:06 -08:00
$public_uploads [ 'accessories' ] = glob ( 'public/accessories' . " /*.* " );
$public_uploads [ 'assets' ] = glob ( 'public/assets' . " /*.* " );
$public_uploads [ 'avatars' ] = glob ( 'public/avatars' . " /*.* " );
$public_uploads [ 'categories' ] = glob ( 'public/categories' . " /*.* " );
$public_uploads [ 'companies' ] = glob ( 'public/companies' . " /*.* " );
$public_uploads [ 'components' ] = glob ( 'public/components' . " /*.* " );
$public_uploads [ 'consumables' ] = glob ( 'public/consumables' . " /*.* " );
$public_uploads [ 'departments' ] = glob ( 'public/departments' . " /*.* " );
$public_uploads [ 'locations' ] = glob ( 'public/locations' . " /*.* " );
$public_uploads [ 'manufacturers' ] = glob ( 'public/manufacturers' . " /*.* " );
$public_uploads [ 'suppliers' ] = glob ( 'public/suppliers' . " /*.* " );
$public_uploads [ 'assetmodels' ] = glob ( 'public/models' . " /*.* " );
2018-09-29 21:33:52 -07:00
// iterate files
foreach ( $public_uploads as $public_type => $public_upload )
{
$type_count = 0 ;
2020-11-12 21:46:06 -08:00
$this -> info ( " - There are " . count ( $public_upload ) . ' PUBLIC ' . $public_type . ' files.' );
2018-09-29 21:33:52 -07:00
for ( $i = 0 ; $i < count ( $public_upload ); $i ++ ) {
$type_count ++ ;
$filename = basename ( $public_upload [ $i ]);
try {
2020-11-12 21:46:06 -08:00
Storage :: disk ( 'public' ) -> put ( 'uploads/' . public_type . '/' . $filename , file_get_contents ( $public_upload [ $i ]));
$new_url = Storage :: disk ( 'public' ) -> url ( 'uploads/' . $public_type . '/' . $filename , $filename );
2018-09-29 21:33:52 -07:00
$this -> info ( $type_count . '. PUBLIC: ' . $filename . ' was copied to ' . $new_url );
} catch ( \Exception $e ) {
\Log :: debug ( $e );
$this -> error ( $e );
}
}
}
2020-11-12 21:46:06 -08:00
$logos = glob ( " public/uploads/setting*.* " );
$this -> info ( " - There are " . count ( $logos ) . ' files that might be logos.' );
2020-08-28 14:10:28 -07:00
$type_count = 0 ;
2018-09-29 21:33:52 -07:00
2020-11-12 21:46:06 -08:00
foreach ( $logos as $logo ) {
$this -> info ( $logo );
2018-09-29 21:33:52 -07:00
$type_count ++ ;
2020-11-12 21:46:06 -08:00
$filename = basename ( $logo );
Storage :: disk ( 'public' ) -> put ( 'uploads/' . $filename , file_get_contents ( $logo ));
$this -> info ( $type_count . '. LOGO: ' . $filename . ' was copied to ' . env ( 'PUBLIC_AWS_URL' ) . '/uploads/' . $filename );
2018-09-29 21:33:52 -07:00
}
$private_uploads [ 'assets' ] = glob ( 'storage/private_uploads/assets' . " /*.* " );
$private_uploads [ 'signatures' ] = glob ( 'storage/private_uploads/signatures' . " /*.* " );
$private_uploads [ 'audits' ] = glob ( 'storage/private_uploads/audits' . " /*.* " );
$private_uploads [ 'assetmodels' ] = glob ( 'storage/private_uploads/assetmodels' . " /*.* " );
$private_uploads [ 'imports' ] = glob ( 'storage/private_uploads/imports' . " /*.* " );
$private_uploads [ 'licenses' ] = glob ( 'storage/private_uploads/licenses' . " /*.* " );
$private_uploads [ 'users' ] = glob ( 'storage/private_uploads/users' . " /*.* " );
2020-08-28 14:10:28 -07:00
$private_uploads [ 'backups' ] = glob ( 'storage/private_uploads/users' . " /*.* " );
2018-09-29 21:33:52 -07:00
foreach ( $private_uploads as $private_type => $private_upload )
{
2020-11-12 21:46:06 -08:00
$this -> info ( " - There are " . count ( $private_upload ) . ' PRIVATE ' . $private_type . ' files.' );
2018-09-29 21:33:52 -07:00
$type_count = 0 ;
for ( $x = 0 ; $x < count ( $private_upload ); $x ++ ) {
$type_count ++ ;
$filename = basename ( $private_upload [ $x ]);
try {
2020-11-12 14:47:38 -08:00
Storage :: put ( $private_type . '/' . $filename , file_get_contents ( $private_upload [ $i ]));
2018-09-29 21:33:52 -07:00
$new_url = Storage :: url ( $private_type . '/' . $filename , $filename );
$this -> info ( $type_count . '. PRIVATE: ' . $filename . ' was copied to ' . $new_url );
} catch ( \Exception $e ) {
\Log :: debug ( $e );
$this -> error ( $e );
}
}
}
if ( $delete_local == 'true' ) {
$public_delete_count = 0 ;
$private_delete_count = 0 ;
$this -> info ( " \n \n " );
$this -> error ( '!!!!!!!!!!!!!!!!!!!!!!!!!!!!! WARNING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' );
$this -> warn ( " \n THIS WILL DELETE ALL OF YOUR LOCAL UPLOADED FILES. \n \n This cannot be undone, so you should take a backup of your system before you proceed. \n " );
$this -> error ( '!!!!!!!!!!!!!!!!!!!!!!!!!!!!! WARNING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' );
if ( $this -> confirm ( " Do you wish to continue? " )) {
foreach ( $public_uploads as $public_type => $public_upload ) {
for ( $i = 0 ; $i < count ( $public_upload ); $i ++ ) {
$filename = $public_upload [ $i ];
try {
unlink ( $filename );
$public_delete_count ++ ;
} catch ( \Exception $e ) {
\Log :: debug ( $e );
$this -> error ( $e );
}
}
}
foreach ( $private_uploads as $private_type => $private_upload )
{
for ( $i = 0 ; $i < count ( $private_upload ); $i ++ ) {
$filename = $private_upload [ $i ];
try {
unlink ( $filename );
$private_delete_count ++ ;
} catch ( \Exception $e ) {
\Log :: debug ( $e );
$this -> error ( $e );
}
}
}
2020-02-11 22:09:46 -08:00
$this -> info ( $public_delete_count . " PUBLIC local files and " . $private_delete_count . " PRIVATE local files were deleted from your filesystem. " );
2018-09-29 21:33:52 -07:00
}
}
}
}