|
33 | 33 | get_request_app_shutdown, |
34 | 34 | get_release_storage_startup_barrier, |
35 | 35 | ) |
36 | | -from utils.cloudsave_runtime import ROOT_MODE_MAINTENANCE_READONLY, ROOT_MODE_NORMAL, set_root_mode |
| 36 | +from utils.cloudsave_runtime import ( |
| 37 | + ROOT_MODE_MAINTENANCE_READONLY, |
| 38 | + ROOT_MODE_NORMAL, |
| 39 | + cloudsave_disabled_reason, |
| 40 | + is_cloudsave_disabled_due_to_local_state_unavailable, |
| 41 | + set_root_mode, |
| 42 | +) |
37 | 43 | from utils.storage_location_bootstrap import ( |
38 | 44 | STORAGE_STARTUP_BLOCKING_REASONS, |
39 | 45 | STORAGE_STATUS_POLL_INTERVAL_MS, |
@@ -113,6 +119,19 @@ def _set_no_cache_headers(response: Response) -> None: |
113 | 119 | response.headers["Expires"] = "0" |
114 | 120 |
|
115 | 121 |
|
| 122 | +def _reject_storage_mutation_when_cloudsave_disabled(response: Response) -> dict[str, Any] | None: |
| 123 | + if not is_cloudsave_disabled_due_to_local_state_unavailable(): |
| 124 | + return None |
| 125 | + response.status_code = 409 |
| 126 | + return { |
| 127 | + "ok": False, |
| 128 | + "error_code": "cloudsave_local_state_unavailable", |
| 129 | + "error": "本机状态目录不可用,当前会话已禁用云存档。请先修复本机 state 路径后重启应用,再进行存储位置变更。", |
| 130 | + "cloudsave_disabled": True, |
| 131 | + "cloudsave_disabled_reason": cloudsave_disabled_reason(), |
| 132 | + } |
| 133 | + |
| 134 | + |
116 | 135 | def _normalize_optional_path(value: Any) -> str: |
117 | 136 | raw_value = str(value or "").strip() |
118 | 137 | if not raw_value: |
@@ -1081,6 +1100,10 @@ async def post_storage_location_exit(request: Request, response: Response): |
1081 | 1100 | "error": "缺少存储退出确认标记。", |
1082 | 1101 | } |
1083 | 1102 |
|
| 1103 | + disabled_response = _reject_storage_mutation_when_cloudsave_disabled(response) |
| 1104 | + if disabled_response is not None: |
| 1105 | + return disabled_response |
| 1106 | + |
1084 | 1107 | config_manager = _get_storage_config_manager() |
1085 | 1108 | bootstrap_payload = build_storage_location_bootstrap_payload(config_manager) |
1086 | 1109 | blocking_reason = str(bootstrap_payload.get("blocking_reason") or "").strip() |
@@ -1216,6 +1239,10 @@ async def _post_storage_location_retained_source_cleanup_locked( |
1216 | 1239 | ): |
1217 | 1240 | _set_no_cache_headers(response) |
1218 | 1241 |
|
| 1242 | + disabled_response = _reject_storage_mutation_when_cloudsave_disabled(response) |
| 1243 | + if disabled_response is not None: |
| 1244 | + return disabled_response |
| 1245 | + |
1219 | 1246 | config_manager = _get_storage_config_manager() |
1220 | 1247 | notice = _build_completed_migration_notice( |
1221 | 1248 | config_manager, |
@@ -1300,6 +1327,10 @@ async def _post_storage_location_select_locked( |
1300 | 1327 | ): |
1301 | 1328 | _set_no_cache_headers(response) |
1302 | 1329 |
|
| 1330 | + disabled_response = _reject_storage_mutation_when_cloudsave_disabled(response) |
| 1331 | + if disabled_response is not None: |
| 1332 | + return disabled_response |
| 1333 | + |
1303 | 1334 | config_manager = _get_storage_config_manager() |
1304 | 1335 | current_root = normalize_runtime_root(config_manager.app_docs_dir) |
1305 | 1336 | anchor_root = compute_anchor_root(config_manager, current_root=current_root) |
@@ -1508,6 +1539,10 @@ async def post_storage_location_preflight( |
1508 | 1539 | ): |
1509 | 1540 | _set_no_cache_headers(response) |
1510 | 1541 |
|
| 1542 | + disabled_response = _reject_storage_mutation_when_cloudsave_disabled(response) |
| 1543 | + if disabled_response is not None: |
| 1544 | + return disabled_response |
| 1545 | + |
1511 | 1546 | config_manager = _get_storage_config_manager() |
1512 | 1547 | current_root = normalize_runtime_root(config_manager.app_docs_dir) |
1513 | 1548 | anchor_root = compute_anchor_root(config_manager, current_root=current_root) |
@@ -1587,6 +1622,10 @@ async def _post_storage_location_restart_locked( |
1587 | 1622 | ): |
1588 | 1623 | _set_no_cache_headers(response) |
1589 | 1624 |
|
| 1625 | + disabled_response = _reject_storage_mutation_when_cloudsave_disabled(response) |
| 1626 | + if disabled_response is not None: |
| 1627 | + return disabled_response |
| 1628 | + |
1590 | 1629 | config_manager = _get_storage_config_manager() |
1591 | 1630 | current_root = normalize_runtime_root(config_manager.app_docs_dir) |
1592 | 1631 | anchor_root = compute_anchor_root(config_manager, current_root=current_root) |
|
0 commit comments