You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Controllers/Admin/ObjectStorageController.php

95 lines
3.8 KiB
PHTML

<?php
namespace App\Controllers\Admin;
use App\Controllers\AdminController;
use App\Services\Cloud\CloudError;
use App\Services\Cloud\CloudInput;
use App\Services\Cloud\CloudObjectStorageConfig;
final class ObjectStorageController extends AdminController
{
public function index($request, $response, $args)
{
$state = null;
$error = '';
$status = 200;
try {
$state = CloudObjectStorageConfig::view();
} catch (CloudError $exception) {
$error = $exception->getMessage();
$status = $exception->httpStatus;
} catch (\Throwable $exception) {
error_log('Object storage settings read failed: ' . get_class($exception));
$error = '对象存储配置暂时无法读取,请检查数据库状态。';
$status = 503;
}
$smarty = $this->view()->assign([
'object_storage' => $state,
'object_storage_error' => $error,
]);
$response->getBody()->write($smarty->fetch('admin/object_storage.tpl'));
return $response->withStatus($status)
->withHeader('Cache-Control', 'private, no-store');
}
public function save($request, $response, $args)
{
try {
$state = CloudObjectStorageConfig::save(CloudInput::object($request));
return $this->json($response, 1, '对象存储配置已保存,请执行连接验证。', 200, $state);
} catch (CloudError $exception) {
return $this->json($response, 0, $exception->getMessage(), $exception->httpStatus);
} catch (\Throwable $exception) {
error_log('Object storage settings save failed: ' . get_class($exception));
return $this->json($response, 0, '对象存储配置保存失败,请稍后重试。', 500);
}
}
public function test($request, $response, $args)
{
try {
$input = CloudInput::object($request);
CloudInput::only($input, ['expected_revision']);
$state = CloudObjectStorageConfig::testAndEnable(
$input['expected_revision'] ?? null
);
return $this->json($response, 1, '连接验证成功,对象存储已启用。', 200, $state);
} catch (CloudError $exception) {
return $this->json($response, 0, $exception->getMessage(), $exception->httpStatus);
} catch (\Throwable $exception) {
error_log('Object storage connection test failed: ' . get_class($exception));
return $this->json($response, 0, '连接验证失败,请稍后重试。', 500);
}
}
public function disable($request, $response, $args)
{
try {
$input = CloudInput::object($request);
CloudInput::only($input, ['expected_revision']);
$state = CloudObjectStorageConfig::disable(
$input['expected_revision'] ?? null
);
return $this->json($response, 1, '对象存储已停用,已保存的凭据仍保持加密。', 200, $state);
} catch (CloudError $exception) {
return $this->json($response, 0, $exception->getMessage(), $exception->httpStatus);
} catch (\Throwable $exception) {
error_log('Object storage disable failed: ' . get_class($exception));
return $this->json($response, 0, '对象存储停用失败,请稍后重试。', 500);
}
}
/** @param array<string, mixed>|null $state */
private function json($response, int $ret, string $message, int $status, ?array $state = null)
{
$payload = ['ret' => $ret, 'msg' => $message];
if ($state !== null) {
$payload['object_storage'] = $state;
}
return $response->withJson($payload, $status)
->withHeader('Cache-Control', 'private, no-store');
}
}