1
0
mirror of https://github.com/cjango/dcat-vue.git synced 2025-12-06 22:40:03 +08:00

同步存储

This commit is contained in:
weiwait
2023-02-11 10:46:29 +08:00
parent 9bda6574be
commit 4f4b21a290
4 changed files with 134 additions and 0 deletions

View File

@@ -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;
}
}

View File

@@ -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');

View File

@@ -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', <<<JS
let form = $('#{$this->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
}
}

View File

@@ -86,4 +86,7 @@ return [
'2.6.1' => [
'一些优化',
],
'2.7.0' => [
'同步存储',
],
];