|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Controllers\Admin;
|
|
|
|
|
|
|
|
|
|
use App\Controllers\AdminController;
|
|
|
|
|
use App\Models\CloudInstanceBackup;
|
|
|
|
|
use App\Services\Cloud\CloudAccess;
|
|
|
|
|
use App\Services\Cloud\CloudBackupService;
|
|
|
|
|
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 = '';
|
|
|
|
|
$cleanupDebt = null;
|
|
|
|
|
$cleanupDebtError = '';
|
|
|
|
|
$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;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
$query = $request->getQueryParams();
|
|
|
|
|
$cleanupDebt = CloudBackupService::cleanupDebt(
|
|
|
|
|
CloudAccess::actor(false), [
|
|
|
|
|
'page' => $query['cleanup_page'] ?? 1,
|
|
|
|
|
'per_page' => 20,
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
} catch (CloudError $exception) {
|
|
|
|
|
$cleanupDebtError = $exception->getMessage();
|
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
|
error_log('Object cleanup debt read failed: ' . get_class($exception));
|
|
|
|
|
$cleanupDebtError = '对象清理债务暂时无法读取,请检查数据库结构。';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$smarty = $this->view()->assign([
|
|
|
|
|
'object_storage' => $state,
|
|
|
|
|
'object_storage_error' => $error,
|
|
|
|
|
'cleanup_debt' => $cleanupDebt,
|
|
|
|
|
'cleanup_debt_error' => $cleanupDebtError,
|
|
|
|
|
]);
|
|
|
|
|
$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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function cleanupDebt($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$payload = CloudBackupService::cleanupDebt(
|
|
|
|
|
CloudAccess::actor(false), $request->getQueryParams()
|
|
|
|
|
);
|
|
|
|
|
return $response->withJson([
|
|
|
|
|
'ret' => 1,
|
|
|
|
|
'msg' => '对象清理债务已刷新。',
|
|
|
|
|
'cleanup_debt' => $payload,
|
|
|
|
|
])->withHeader('Cache-Control', 'private, no-store');
|
|
|
|
|
} catch (CloudError $exception) {
|
|
|
|
|
return $this->json($response, 0, $exception->getMessage(),
|
|
|
|
|
$exception->httpStatus);
|
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
|
error_log('Object cleanup debt refresh failed: '
|
|
|
|
|
. get_class($exception));
|
|
|
|
|
return $this->json($response, 0,
|
|
|
|
|
'对象清理债务刷新失败,请稍后重试。', 500);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function retryCleanup($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$backupId = filter_var($args['backup_id'] ?? null,
|
|
|
|
|
FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
|
|
|
|
|
if ($backupId === false) {
|
|
|
|
|
throw new CloudError('对象清理记录无效', 400);
|
|
|
|
|
}
|
|
|
|
|
$backup = CloudInstanceBackup::find((int) $backupId);
|
|
|
|
|
if ($backup === null) {
|
|
|
|
|
throw new CloudError('对象清理记录不存在', 404);
|
|
|
|
|
}
|
|
|
|
|
$result = CloudBackupService::retryCleanup(
|
|
|
|
|
$backup, CloudAccess::actor(false)
|
|
|
|
|
);
|
|
|
|
|
return $response->withJson([
|
|
|
|
|
'ret' => 1,
|
|
|
|
|
'msg' => ($result['deleted'] ?? false)
|
|
|
|
|
? '对象已确认不存在,清理债务已关闭。'
|
|
|
|
|
: '已恢复稳定性核对;第二次证据一致后才会创建新删除代次。',
|
|
|
|
|
'cleanup' => $result,
|
|
|
|
|
])->withHeader('Cache-Control', 'private, no-store');
|
|
|
|
|
} catch (CloudError $exception) {
|
|
|
|
|
return $this->json($response, 0, $exception->getMessage(),
|
|
|
|
|
$exception->httpStatus);
|
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
|
error_log('Object cleanup retry 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');
|
|
|
|
|
}
|
|
|
|
|
}
|