From fa57e78c74a70a8fbcab43b64f3370d1207e37c1 Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Tue, 11 May 2021 01:41:58 +0800 Subject: [PATCH] backup add feature: upload to google drive --- app/Console/Commands/BackuAll.php | 12 ++++++++---- app/Console/Commands/Test.php | 2 +- app/Models/Invite.php | 3 ++- app/Providers/GoogleDriveServiceProvider.php | 2 +- app/Repositories/ToolRepository.php | 10 ++++++++-- config/filesystems.php | 4 ++-- include/globalfunctions.php | 5 +++-- public/takesignup.php | 1 + 8 files changed, 26 insertions(+), 13 deletions(-) diff --git a/app/Console/Commands/BackuAll.php b/app/Console/Commands/BackuAll.php index 2545123c..538f9f6e 100644 --- a/app/Console/Commands/BackuAll.php +++ b/app/Console/Commands/BackuAll.php @@ -12,14 +12,14 @@ class BackuAll extends Command * * @var string */ - protected $signature = 'backup:all'; + protected $signature = 'backup:all {--upload-to-google-drive}'; /** * The console command description. * * @var string */ - protected $description = 'Backup all data, include web root and database'; + protected $description = 'Backup all data, include web root and database. options: --upload-to-google-drive'; /** * Create a new command instance. @@ -38,9 +38,13 @@ class BackuAll extends Command */ public function handle() { + $uploadToGoogleDrive = $this->option('upload-to-google-drive'); $rep = new ToolRepository(); - $result = $rep->backupAll(); - $log = sprintf('[%s], %s, result: %s', REQUEST_ID, __METHOD__, var_export($result, true)); + $result = $rep->backupAll($uploadToGoogleDrive); + $log = sprintf( + '[%s], %s, uploadToGoogleDrive: %s, result: %s', + REQUEST_ID, __METHOD__, var_export($uploadToGoogleDrive, true), var_export($result, true) + ); $this->info($log); do_log($log); } diff --git a/app/Console/Commands/Test.php b/app/Console/Commands/Test.php index 298a3df7..229af31c 100644 --- a/app/Console/Commands/Test.php +++ b/app/Console/Commands/Test.php @@ -58,7 +58,7 @@ class Test extends Command // $rep->assignCronjob(); // $r = $rep->cronjobCheckout(); $disk = Storage::disk('google_dirve'); - $r = $disk->files(); + $r = $disk->put('/', base_path('composer.json')); dd($r); } diff --git a/app/Models/Invite.php b/app/Models/Invite.php index 787621a2..2bb96876 100644 --- a/app/Models/Invite.php +++ b/app/Models/Invite.php @@ -15,7 +15,8 @@ class Invite extends NexusModel ]; protected $fillable = [ - 'inviter', 'invitee', 'hash', 'time_invited', 'valid', 'invitee_register_uid', 'invitee_register_email', 'invitee_register_username' + 'inviter', 'invitee', 'hash', 'time_invited', 'valid', + 'invitee_register_uid', 'invitee_register_email', 'invitee_register_username' ]; public function getValidTextAttribute() diff --git a/app/Providers/GoogleDriveServiceProvider.php b/app/Providers/GoogleDriveServiceProvider.php index e8d464b1..c734baed 100644 --- a/app/Providers/GoogleDriveServiceProvider.php +++ b/app/Providers/GoogleDriveServiceProvider.php @@ -14,7 +14,7 @@ class GoogleDriveServiceProvider extends ServiceProvider */ public function boot() { - Storage::extend('google_dirve', function($app, $config) { + Storage::extend('google_drive', function($app, $config) { $client = new \Google_Client(); $client->setClientId($config['clientId']); $client->setClientSecret($config['clientSecret']); diff --git a/app/Repositories/ToolRepository.php b/app/Repositories/ToolRepository.php index b010e468..e459c261 100644 --- a/app/Repositories/ToolRepository.php +++ b/app/Repositories/ToolRepository.php @@ -2,6 +2,7 @@ namespace App\Repositories; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Storage; class ToolRepository extends BaseRepository { @@ -54,7 +55,7 @@ class ToolRepository extends BaseRepository return compact('result_code', 'filename'); } - public function backupAll() + public function backupAll($uploadToGoogleDrive = false) { $backupWeb = $this->backupWebRoot(); if ($backupWeb['result_code'] != 0) { @@ -76,7 +77,12 @@ class ToolRepository extends BaseRepository "command: %s, output: %s, result_code: %s, result: %s, filename: %s", $command, json_encode($output), $result_code, $result, $filename )); - return compact('result_code', 'filename'); + $upload_result = ''; + if ($uploadToGoogleDrive) { + $disk = Storage::disk('google_drive'); + $upload_result = $disk->put(basename($filename), fopen($filename, 'r')); + } + return compact('result_code', 'filename', 'upload_result'); } } diff --git a/config/filesystems.php b/config/filesystems.php index 877be5ea..aad9df99 100644 --- a/config/filesystems.php +++ b/config/filesystems.php @@ -51,8 +51,8 @@ return [ 'url' => env('AWS_URL'), 'endpoint' => env('AWS_ENDPOINT'), ], - 'google_dirve' => [ - 'driver' => 'google_dirve', + 'google_drive' => [ + 'driver' => 'google_drive', 'clientId' => env('GOOGLE_DRIVE_CLIENT_ID'), 'clientSecret' => env('GOOGLE_DRIVE_CLIENT_SECRET'), 'refreshToken' => env('GOOGLE_DRIVE_REFRESH_TOKEN'), diff --git a/include/globalfunctions.php b/include/globalfunctions.php index 27c82851..52c01d7d 100644 --- a/include/globalfunctions.php +++ b/include/globalfunctions.php @@ -92,7 +92,8 @@ function hash_where($name, $hash) { return "($name = " . sqlesc($hash) . " OR $name = " . sqlesc($shhash) . ")"; } - +//no need any more... +/* function strip_magic_quotes($arr) { foreach ($arr as $k => $v) @@ -119,7 +120,7 @@ if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) $_COOKIE = strip_magic_quotes($_COOKIE); } } - +*/ function get_langfolder_list() { diff --git a/public/takesignup.php b/public/takesignup.php index d04d1fe6..4b51693d 100644 --- a/public/takesignup.php +++ b/public/takesignup.php @@ -199,6 +199,7 @@ if ($type == 'invite') 'valid' => \App\Models\Invite::VALID_NO, 'invitee_register_uid' => $id, 'invitee_register_email' => $email, + 'invitee_register_username' => $wantusername, ]; \App\Models\Invite::query()->where('id', $inv['id'])->update($update);