From 4f4b21a2905d2e49658b7615a475b9f2e102c8e1 Mon Sep 17 00:00:00 2001 From: weiwait Date: Sat, 11 Feb 2023 10:46:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E5=AD=98=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Http/Controllers/DcatVueController.php | 49 ++++++++++++++ src/Http/routes.php | 4 ++ src/Setting.php | 78 ++++++++++++++++++++++ version.php | 3 + 4 files changed, 134 insertions(+) diff --git a/src/Http/Controllers/DcatVueController.php b/src/Http/Controllers/DcatVueController.php index a72aaf3..fbffc7d 100644 --- a/src/Http/Controllers/DcatVueController.php +++ b/src/Http/Controllers/DcatVueController.php @@ -120,4 +120,53 @@ class DcatVueController extends Controller { return Http::get("https://apis.map.qq.com/ws/geocoder/v1/?location={$request['lat']},{$request['lng']}&key=ZZQBZ-WE6E2-FCMUZ-CBUZ7-ZW5I3-I7BIX&get_poi=1")->body(); } + + public function syncStorage(Request $request) + { + $request->validate([ + 'source' => 'required|string', + 'target' => 'required|string', + 'timeout' => 'required|integer', + 'max' => 'required|integer', + ]); + + $start = now(); + + try { + $source = Storage::disk($request['source']); + $source->put('/sync-touch', 'touch'); + $source->delete('sync-touch'); + + $target = Storage::disk($request['target']); + $target->put('/sync-touch', 'touch'); + $target->delete('sync-touch'); + } catch (\Throwable $exception) { + return 2; + } + + try { + $sources = $source->allFiles('/'); + $targets = $target->allFiles('/'); + + $files = array_diff($sources, $targets); + + foreach ($files as $file) { + if (now()->diffInSeconds($start) > $request['timeout']) { + return 0; + } + + if ($source->size($file) > ($request['max'] * 1024 * 1024)) { + return 1; + } + + $target->put($file, $source->get($file)); + } + + return 1; + } catch (\Throwable $exception) { + + } + + return 0; + } } diff --git a/src/Http/routes.php b/src/Http/routes.php index c0ba000..2a13071 100644 --- a/src/Http/routes.php +++ b/src/Http/routes.php @@ -27,3 +27,7 @@ Route::get('weiwait/distpicker/ll2address', [Controllers\DcatVueController::clas // 替换原生登录 Route::get('auth/login', [Controllers\DcatAuthController::class, 'getLogin']); Route::post('auth/login', [Controllers\DcatAuthController::class, 'postLogin']); + +// 同步存储 +Route::post('weiwait/storage/sync', [Controllers\DcatVueController::class, 'syncStorage']) + ->name('weiwait.storage.sync'); diff --git a/src/Setting.php b/src/Setting.php index 0308600..0c34645 100644 --- a/src/Setting.php +++ b/src/Setting.php @@ -2,14 +2,18 @@ namespace Weiwait\DcatVue; +use Dcat\Admin\Admin; use Dcat\Admin\Extend\Setting as Form; use Dcat\Admin\Form\NestedForm; use Dcat\Admin\Http\JsonResponse; +use Illuminate\Support\Facades\Artisan; class Setting extends Form { public function form() { + $this->disableResetButton(); + $this->tab('站点', function (\Dcat\Admin\Widgets\Form $form) { $form->switch('weiwait_auth.enable_captcha', DcatVueServiceProvider::trans('auth.enable_captcha')) ->default(true); @@ -19,12 +23,81 @@ class Setting extends Form $form->text('path', DcatVueServiceProvider::trans('auth.footers.path')); }); }); + + $this->tab('同步存储', function (\Dcat\Admin\Widgets\Form $form) { + $options = [ + 'public' => '本地(public)', + 'oss' => '阿里(oss)', + 'qiniu' => '七牛(qiniu)', + 'cos' => '腾讯(cos)', + ]; + + $form->select('sync.source', '来源')->options($options); + $form->select('sync.target', '目标')->options($options); + $form->number('sync.timeout', '超时/秒')->default(20); + $form->number('sync.max', '大小/兆')->default(20) + ->help('脚本无法长时间运行, 限制单位件大小'); + + $sync = route('dcat.admin.weiwait.storage.sync'); + $form->button('开始同步')->on('click', <<getElementId()}') + let source = form.find("select[name='sync[source]']").val() + let target = form.find("select[name='sync[target]']").val() + let timeout = form.find("input[name='sync[timeout]']").val() + let max = form.find("input[name='sync[max]']").val() + + if (!source || !target || source === target) { + !source && Dcat.warning('请选择同步来源'); + !target && Dcat.warning('请选择同步目标'); + source && source === target && Dcat.warning('不要进行一些特别聪明的行为'); + return + } + + this.style.visibility = 'hidden' + Dcat.info('同步中,请不要执行其它操作') + const synchronizing = () => { + $.ajax({ + type: 'POST', + url: '$sync', + data: { + source, + target, + timeout, + max + }, + success: res => { + console.log(res) + if (0 === ~~res) { + Dcat.info('同步仍在继续,请不要执行其它操作') + synchronizing() + } + if (1 === ~~res) { + Dcat.swal.success('同步成功') + this.style.visibility = 'visible' + } + if (2 === ~~res) { + Dcat.swal.error('请先确认两者配置') + this.style.visibility = 'visible' + } + }, + error: e => { + console.log(e) + this.style.visibility = 'visible' + } + }) + } + + synchronizing() + JS); + }); } public function handle(array $input): JsonResponse { admin_setting(['weiwait_auth' => $input['weiwait_auth']]); + is_file(app()->getCachedConfigPath()) && Artisan::call('config:cache'); + return $this->response()->success('保存成功')->refresh(); } @@ -34,4 +107,9 @@ class Setting extends Form 'weiwait_auth' => admin_setting_array('weiwait_auth'), ]; } + + public function addAjaxScript() + { + parent::addAjaxScript(); // TODO: Change the autogenerated stub + } } diff --git a/version.php b/version.php index fedc71d..a0827be 100644 --- a/version.php +++ b/version.php @@ -86,4 +86,7 @@ return [ '2.6.1' => [ '一些优化', ], + '2.7.0' => [ + '同步存储', + ], ];