602 lines
23 KiB
Python
602 lines
23 KiB
Python
from __future__ import annotations
|
||||
|
|
|
|||
|
|
import base64
|
|||
|
|
import hashlib
|
|||
|
|
import json
|
|||
|
|
import logging
|
|||
|
|
import threading
|
|||
|
|
import time
|
|||
|
|
from collections.abc import Callable
|
|||
|
|
from dataclasses import asdict
|
|||
|
|
from dataclasses import dataclass, field
|
|||
|
|
from pathlib import Path
|
|||
|
|
from typing import Any
|
|||
|
|
|
|||
|
|
import requests
|
|||
|
|
|
|||
|
|
from pixiv_app.core.paths import runtime_path
|
|||
|
|
|
|||
|
|
logger = logging.getLogger(__name__)
|
|||
|
|
|
|||
|
|
|
|||
|
|
def file_sha256(path: str | Path) -> str:
|
|||
|
|
digest = hashlib.sha256()
|
|||
|
|
with Path(path).open("rb") as fp:
|
|||
|
|
for chunk in iter(lambda: fp.read(1024 * 1024), b""):
|
|||
|
|
digest.update(chunk)
|
|||
|
|
return digest.hexdigest()
|
|||
|
|
|
|||
|
|
|
|||
|
|
def persist_cloud_session_tokens(access_token: str, refresh_token: str = "") -> None:
|
|||
|
|
"""将刷新后的 Token 写回桌面会话文件,供 GUI 与 Gallery API 共用。"""
|
|||
|
|
from pixiv_app.core.auth import SessionStore
|
|||
|
|
from pixiv_app.core.paths import app_session_file
|
|||
|
|
|
|||
|
|
store = SessionStore(app_session_file())
|
|||
|
|
payload = store.load()
|
|||
|
|
cloud = payload.get("cloud_sync") if isinstance(payload.get("cloud_sync"), dict) else {}
|
|||
|
|
cloud["access_token"] = access_token
|
|||
|
|
if refresh_token:
|
|||
|
|
cloud["refresh_token"] = refresh_token
|
|||
|
|
payload["cloud_sync"] = cloud
|
|||
|
|
store.save(payload)
|
|||
|
|
|
|||
|
|
|
|||
|
|
def _decode_jwt_exp(token: str) -> float:
|
|||
|
|
"""解码 JWT 的 exp 字段,返回 Unix 时间戳;解码失败返回 0。"""
|
|||
|
|
if not token or "." not in token:
|
|||
|
|
return 0.0
|
|||
|
|
try:
|
|||
|
|
payload = token.split(".")[1]
|
|||
|
|
payload += "=" * (4 - len(payload) % 4)
|
|||
|
|
decoded = json.loads(base64.urlsafe_b64decode(payload))
|
|||
|
|
return float(decoded.get("exp", 0))
|
|||
|
|
except Exception:
|
|||
|
|
return 0.0
|
|||
|
|
|
|||
|
|
|
|||
|
|
class CloudSession:
|
|||
|
|
"""全局单例,统一管理云同步 Token 的读写、刷新与持久化。
|
|||
|
|
|
|||
|
|
所有需要访问 Sakura Cloud API 的模块(GUI、Gallery、FollowSync、XMedia)
|
|||
|
|
都通过此单例获取配置和创建客户端,确保 token 刷新互不冲突、变更全局可见。
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
_instance: CloudSession | None = None
|
|||
|
|
_instance_lock = threading.Lock()
|
|||
|
|
|
|||
|
|
def __new__(cls) -> CloudSession:
|
|||
|
|
if cls._instance is None:
|
|||
|
|
with cls._instance_lock:
|
|||
|
|
if cls._instance is None:
|
|||
|
|
instance = super().__new__(cls)
|
|||
|
|
instance._base_url = ""
|
|||
|
|
instance._access_token = ""
|
|||
|
|
instance._refresh_token = ""
|
|||
|
|
instance._enabled = False
|
|||
|
|
instance._refresh_lock = threading.Lock()
|
|||
|
|
instance._listeners: list[Callable[[str, str], None]] = []
|
|||
|
|
instance._last_refresh_time = 0.0
|
|||
|
|
cls._instance = instance
|
|||
|
|
return cls._instance
|
|||
|
|
|
|||
|
|
# ------------------------------------------------------------------
|
|||
|
|
# 配置入口
|
|||
|
|
# ------------------------------------------------------------------
|
|||
|
|
|
|||
|
|
def configure(
|
|||
|
|
self,
|
|||
|
|
*,
|
|||
|
|
base_url: str = "",
|
|||
|
|
access_token: str = "",
|
|||
|
|
refresh_token: str = "",
|
|||
|
|
enabled: bool = False,
|
|||
|
|
) -> None:
|
|||
|
|
"""GUI 启动 / 登录 / 保存会话时调用,同步最新 token 到全局单例。"""
|
|||
|
|
self._base_url = (base_url or "").strip()
|
|||
|
|
self._access_token = (access_token or "").strip()
|
|||
|
|
self._refresh_token = (refresh_token or "").strip()
|
|||
|
|
self._enabled = bool(enabled)
|
|||
|
|
|
|||
|
|
def load_from_disk(self) -> dict[str, Any]:
|
|||
|
|
"""从磁盘会话文件读取云端配置并应用到单例,返回原始 payload。"""
|
|||
|
|
from pixiv_app.core.auth import SessionStore
|
|||
|
|
from pixiv_app.core.paths import app_session_file
|
|||
|
|
|
|||
|
|
store = SessionStore(app_session_file())
|
|||
|
|
payload = store.load()
|
|||
|
|
cloud = payload.get("cloud_sync") if isinstance(payload.get("cloud_sync"), dict) else {}
|
|||
|
|
self.configure(
|
|||
|
|
base_url=str(cloud.get("base_url") or ""),
|
|||
|
|
access_token=str(cloud.get("access_token") or ""),
|
|||
|
|
refresh_token=str(cloud.get("refresh_token") or ""),
|
|||
|
|
enabled=bool(cloud.get("enabled", False)),
|
|||
|
|
)
|
|||
|
|
return payload
|
|||
|
|
|
|||
|
|
@property
|
|||
|
|
def enabled(self) -> bool:
|
|||
|
|
return self._enabled and bool(self._base_url) and bool(self._access_token)
|
|||
|
|
|
|||
|
|
# ------------------------------------------------------------------
|
|||
|
|
# Config 快照
|
|||
|
|
# ------------------------------------------------------------------
|
|||
|
|
|
|||
|
|
def get_config(self) -> CloudSyncConfig:
|
|||
|
|
"""返回当前 token 的 CloudSyncConfig 快照。
|
|||
|
|
|
|||
|
|
调用时机:每次创建 SakuraCloudClient 前。
|
|||
|
|
若 access_token 即将过期(< 5 分钟),主动触发一次刷新。
|
|||
|
|
"""
|
|||
|
|
if self._access_token and self._is_expired_or_near(threshold_seconds=300):
|
|||
|
|
self.try_refresh()
|
|||
|
|
return CloudSyncConfig(
|
|||
|
|
base_url=self._base_url,
|
|||
|
|
access_token=self._access_token,
|
|||
|
|
refresh_token=self._refresh_token,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# ------------------------------------------------------------------
|
|||
|
|
# 监听者管理
|
|||
|
|
# ------------------------------------------------------------------
|
|||
|
|
|
|||
|
|
def add_listener(self, callback: Callable[[str, str], None]) -> None:
|
|||
|
|
"""注册 token 刷新回调(如 GUI 的 _on_cloud_token_refreshed)。"""
|
|||
|
|
if callback not in self._listeners:
|
|||
|
|
self._listeners.append(callback)
|
|||
|
|
|
|||
|
|
def remove_listener(self, callback: Callable[[str, str], None]) -> None:
|
|||
|
|
try:
|
|||
|
|
self._listeners.remove(callback)
|
|||
|
|
except ValueError:
|
|||
|
|
pass
|
|||
|
|
|
|||
|
|
def notify_listeners(self, access_token: str, refresh_token: str) -> None:
|
|||
|
|
"""通知所有监听者 token 已更新(成功时为新的 token 对,失败时为空字符串)。"""
|
|||
|
|
for callback in self._listeners:
|
|||
|
|
try:
|
|||
|
|
callback(access_token, refresh_token)
|
|||
|
|
except Exception:
|
|||
|
|
logger.exception("CloudSession listener %s failed", getattr(callback, "__name__", callback))
|
|||
|
|
|
|||
|
|
# ------------------------------------------------------------------
|
|||
|
|
# Token 刷新(核心)
|
|||
|
|
# ------------------------------------------------------------------
|
|||
|
|
|
|||
|
|
def try_refresh(self) -> bool:
|
|||
|
|
"""统一刷新入口。使用全局锁防止并发重复刷新。
|
|||
|
|
|
|||
|
|
Returns:
|
|||
|
|
True: 刷新成功,access_token 已更新
|
|||
|
|
False: 刷新失败,token 已被清空,监听者已收到空 token 通知
|
|||
|
|
"""
|
|||
|
|
refresh = self._refresh_token
|
|||
|
|
if not refresh:
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
with self._refresh_lock:
|
|||
|
|
# 二次检查:可能其他线程已刷新
|
|||
|
|
refresh = self._refresh_token
|
|||
|
|
if not refresh:
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
# 距上次刷新不足 5 秒,跳过(防抖)
|
|||
|
|
elapsed = time.time() - self._last_refresh_time
|
|||
|
|
if elapsed < 5.0 and self._access_token:
|
|||
|
|
return True
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
resp = requests.post(
|
|||
|
|
f"{self._base_url}/api/v1/auth/refresh",
|
|||
|
|
json={"refresh_token": refresh},
|
|||
|
|
timeout=30.0,
|
|||
|
|
)
|
|||
|
|
if resp.status_code != 200:
|
|||
|
|
logger.warning(
|
|||
|
|
"CloudSession refresh failed HTTP %s: %s",
|
|||
|
|
resp.status_code,
|
|||
|
|
resp.text[:200],
|
|||
|
|
)
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
body = resp.json()
|
|||
|
|
data = body.get("data", body) if isinstance(body, dict) else {}
|
|||
|
|
if not isinstance(data, dict):
|
|||
|
|
self._on_refresh_failed()
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
new_access = (data.get("access_token") or "").strip()
|
|||
|
|
new_refresh = (data.get("refresh_token") or "").strip()
|
|||
|
|
if not new_access:
|
|||
|
|
self._on_refresh_failed()
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
# 更新内存
|
|||
|
|
self._access_token = new_access
|
|||
|
|
if new_refresh:
|
|||
|
|
self._refresh_token = new_refresh
|
|||
|
|
self._last_refresh_time = time.time()
|
|||
|
|
|
|||
|
|
# 写盘
|
|||
|
|
self._persist()
|
|||
|
|
|
|||
|
|
# 通知监听者
|
|||
|
|
self.notify_listeners(self._access_token, self._refresh_token)
|
|||
|
|
logger.info("CloudSession token refreshed successfully")
|
|||
|
|
return True
|
|||
|
|
|
|||
|
|
except Exception:
|
|||
|
|
logger.exception("CloudSession refresh error")
|
|||
|
|
self._on_refresh_failed()
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
def _on_refresh_failed(self) -> None:
|
|||
|
|
"""刷新失败:清空 token + 写盘 + 通知监听者。"""
|
|||
|
|
self._access_token = ""
|
|||
|
|
self._refresh_token = ""
|
|||
|
|
self._last_refresh_time = time.time()
|
|||
|
|
self._persist()
|
|||
|
|
self.notify_listeners("", "")
|
|||
|
|
|
|||
|
|
def _persist(self) -> None:
|
|||
|
|
"""将当前 token 写入磁盘会话文件。异常记录日志但不抛出。"""
|
|||
|
|
try:
|
|||
|
|
persist_cloud_session_tokens(self._access_token, self._refresh_token)
|
|||
|
|
except Exception:
|
|||
|
|
logger.exception("CloudSession persist to disk failed")
|
|||
|
|
|
|||
|
|
# ------------------------------------------------------------------
|
|||
|
|
# JWT 辅助
|
|||
|
|
# ------------------------------------------------------------------
|
|||
|
|
|
|||
|
|
def _is_expired_or_near(self, threshold_seconds: int = 300) -> bool:
|
|||
|
|
exp = _decode_jwt_exp(self._access_token)
|
|||
|
|
if exp <= 0:
|
|||
|
|
return False
|
|||
|
|
return (exp - threshold_seconds) <= time.time()
|
|||
|
|
|
|||
|
|
|
|||
|
|
@dataclass
|
|||
|
|
class CloudSyncConfig:
|
|||
|
|
base_url: str
|
|||
|
|
access_token: str
|
|||
|
|
refresh_token: str = ""
|
|||
|
|
chunk_size: int = 5 * 1024 * 1024
|
|||
|
|
timeout: float = 60.0
|
|||
|
|
|
|||
|
|
|
|||
|
|
@dataclass
|
|||
|
|
class SyncMetadata:
|
|||
|
|
resource_type: str = "artwork"
|
|||
|
|
source_platform: str = "pixiv"
|
|||
|
|
source_id: str | None = None
|
|||
|
|
content_type: str | None = None
|
|||
|
|
title: str = ""
|
|||
|
|
author: str | None = None
|
|||
|
|
author_id: str | None = None
|
|||
|
|
tags: list[str] = field(default_factory=list)
|
|||
|
|
metadata: dict[str, Any] = field(default_factory=dict)
|
|||
|
|
page_count: int = 1
|
|||
|
|
thumbnail_url: str | None = None
|
|||
|
|
description: str | None = None
|
|||
|
|
word_count: int = 0
|
|||
|
|
series_id: str | None = None
|
|||
|
|
series_title: str | None = None
|
|||
|
|
chapter_title: str | None = None
|
|||
|
|
chapter_order: int = 0
|
|||
|
|
|
|||
|
|
|
|||
|
|
class SyncFailureQueue:
|
|||
|
|
def __init__(self, path: str | Path | None = None) -> None:
|
|||
|
|
self.path = Path(path) if path else runtime_path("cloud_sync_failures.json")
|
|||
|
|
|
|||
|
|
def load(self) -> list[dict[str, Any]]:
|
|||
|
|
if not self.path.is_file():
|
|||
|
|
return []
|
|||
|
|
try:
|
|||
|
|
data = json.loads(self.path.read_text(encoding="utf-8"))
|
|||
|
|
return data if isinstance(data, list) else []
|
|||
|
|
except Exception:
|
|||
|
|
return []
|
|||
|
|
|
|||
|
|
def save(self, items: list[dict[str, Any]]) -> None:
|
|||
|
|
self.path.parent.mkdir(parents=True, exist_ok=True)
|
|||
|
|
self.path.write_text(json.dumps(items, ensure_ascii=False, indent=2), encoding="utf-8")
|
|||
|
|
|
|||
|
|
def add(self, file_path: str | Path, metadata: SyncMetadata, mime_type: str | None, error: str) -> None:
|
|||
|
|
now = time.time()
|
|||
|
|
file_path = str(Path(file_path))
|
|||
|
|
items = self.load()
|
|||
|
|
key = (file_path, metadata.source_platform, metadata.source_id)
|
|||
|
|
for item in items:
|
|||
|
|
item_key = (
|
|||
|
|
str(item.get("file_path") or ""),
|
|||
|
|
str(item.get("metadata", {}).get("source_platform") or ""),
|
|||
|
|
item.get("metadata", {}).get("source_id"),
|
|||
|
|
)
|
|||
|
|
if item_key == key:
|
|||
|
|
attempts = int(item.get("attempts") or 0)
|
|||
|
|
item.update(
|
|||
|
|
{
|
|||
|
|
"metadata": asdict(metadata),
|
|||
|
|
"mime_type": mime_type,
|
|||
|
|
"attempts": attempts,
|
|||
|
|
"last_error": error,
|
|||
|
|
"updated_at": now,
|
|||
|
|
"next_retry_at": now + min(3600, 60 * (2 ** min(attempts, 5))),
|
|||
|
|
}
|
|||
|
|
)
|
|||
|
|
self.save(items)
|
|||
|
|
return
|
|||
|
|
items.append(
|
|||
|
|
{
|
|||
|
|
"file_path": file_path,
|
|||
|
|
"metadata": asdict(metadata),
|
|||
|
|
"mime_type": mime_type,
|
|||
|
|
"attempts": 0,
|
|||
|
|
"last_error": error,
|
|||
|
|
"created_at": now,
|
|||
|
|
"updated_at": now,
|
|||
|
|
"next_retry_at": now + 60,
|
|||
|
|
}
|
|||
|
|
)
|
|||
|
|
self.save(items)
|
|||
|
|
|
|||
|
|
|
|||
|
|
class SakuraCloudClient:
|
|||
|
|
def __init__(
|
|||
|
|
self,
|
|||
|
|
config: CloudSyncConfig,
|
|||
|
|
*,
|
|||
|
|
on_token_refreshed: Callable[[str, str], None] | None = None,
|
|||
|
|
cloud_session: CloudSession | None = None,
|
|||
|
|
) -> None:
|
|||
|
|
self.config = config
|
|||
|
|
self.base_url = config.base_url.rstrip("/")
|
|||
|
|
self.session = requests.Session()
|
|||
|
|
self.session.headers.update({"Authorization": f"Bearer {config.access_token}"})
|
|||
|
|
self._cloud_session = cloud_session # 全局单例引用
|
|||
|
|
# 仅在没有 cloud_session 时使用独立锁和回调(兼容旧路径)
|
|||
|
|
if cloud_session is None:
|
|||
|
|
self._on_token_refreshed = on_token_refreshed or persist_cloud_session_tokens
|
|||
|
|
self._refresh_lock = threading.Lock()
|
|||
|
|
else:
|
|||
|
|
self._on_token_refreshed = None
|
|||
|
|
self._refresh_lock = None
|
|||
|
|
|
|||
|
|
def check_file(self, sha256: str, file_size: int) -> dict[str, Any]:
|
|||
|
|
return self._post("/api/v1/sync/check", {"sha256": sha256, "file_size": file_size})
|
|||
|
|
|
|||
|
|
def init_upload(
|
|||
|
|
self,
|
|||
|
|
*,
|
|||
|
|
sha256: str,
|
|||
|
|
file_name: str,
|
|||
|
|
file_size: int,
|
|||
|
|
mime_type: str | None,
|
|||
|
|
resource_type: str,
|
|||
|
|
) -> dict[str, Any]:
|
|||
|
|
return self._post(
|
|||
|
|
"/api/v1/upload/init",
|
|||
|
|
{
|
|||
|
|
"sha256": sha256,
|
|||
|
|
"file_name": file_name,
|
|||
|
|
"file_size": file_size,
|
|||
|
|
"mime_type": mime_type,
|
|||
|
|
"resource_type": resource_type,
|
|||
|
|
},
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
def upload_chunk(self, upload_id: str, chunk: bytes) -> dict[str, Any]:
|
|||
|
|
response = self._request(
|
|||
|
|
"POST",
|
|||
|
|
"/api/v1/upload/chunk",
|
|||
|
|
data={"upload_id": upload_id},
|
|||
|
|
files={"chunk": ("chunk.bin", chunk)},
|
|||
|
|
)
|
|||
|
|
return self._unwrap(response)
|
|||
|
|
|
|||
|
|
def complete_upload(self, upload_id: str) -> dict[str, Any]:
|
|||
|
|
return self._post("/api/v1/upload/complete", {"upload_id": upload_id})
|
|||
|
|
|
|||
|
|
def sync_metadata(self, file_id: str, metadata: SyncMetadata) -> dict[str, Any]:
|
|||
|
|
payload = {
|
|||
|
|
"file_id": file_id,
|
|||
|
|
"resource_type": metadata.resource_type,
|
|||
|
|
"source_platform": metadata.source_platform,
|
|||
|
|
"source_id": metadata.source_id,
|
|||
|
|
"content_type": metadata.content_type,
|
|||
|
|
"title": metadata.title,
|
|||
|
|
"author": metadata.author,
|
|||
|
|
"author_id": metadata.author_id,
|
|||
|
|
"tags": metadata.tags,
|
|||
|
|
"metadata": metadata.metadata,
|
|||
|
|
"page_count": metadata.page_count,
|
|||
|
|
"thumbnail_url": metadata.thumbnail_url,
|
|||
|
|
"description": metadata.description,
|
|||
|
|
"word_count": metadata.word_count,
|
|||
|
|
"series_id": metadata.series_id,
|
|||
|
|
"series_title": metadata.series_title,
|
|||
|
|
"chapter_title": metadata.chapter_title,
|
|||
|
|
"chapter_order": metadata.chapter_order,
|
|||
|
|
}
|
|||
|
|
return self._post("/api/v1/sync/metadata", payload)
|
|||
|
|
|
|||
|
|
def upsert_follow(self, payload: dict[str, Any]) -> dict[str, Any]:
|
|||
|
|
return self._post("/api/v1/follows", payload)
|
|||
|
|
|
|||
|
|
def bulk_upsert_follows(self, items: list[dict[str, Any]]) -> dict[str, Any]:
|
|||
|
|
return self._post("/api/v1/follows/bulk-upsert", {"items": items})
|
|||
|
|
|
|||
|
|
def create_alert(self, payload: dict[str, Any]) -> dict[str, Any]:
|
|||
|
|
return self._post("/api/v1/follows/alerts", payload)
|
|||
|
|
|
|||
|
|
def _request(
|
|||
|
|
self,
|
|||
|
|
method: str,
|
|||
|
|
path: str,
|
|||
|
|
*,
|
|||
|
|
json: dict[str, Any] | None = None,
|
|||
|
|
data: dict[str, Any] | None = None,
|
|||
|
|
files: dict[str, Any] | None = None,
|
|||
|
|
_retry_on_401: bool = True,
|
|||
|
|
) -> requests.Response:
|
|||
|
|
url = f"{self.base_url}{path}"
|
|||
|
|
response = self.session.request(
|
|||
|
|
method,
|
|||
|
|
url,
|
|||
|
|
json=json,
|
|||
|
|
data=data,
|
|||
|
|
files=files,
|
|||
|
|
timeout=self.config.timeout,
|
|||
|
|
)
|
|||
|
|
if response.status_code == 401 and _retry_on_401 and self._try_refresh_token():
|
|||
|
|
response = self.session.request(
|
|||
|
|
method,
|
|||
|
|
url,
|
|||
|
|
json=json,
|
|||
|
|
data=data,
|
|||
|
|
files=files,
|
|||
|
|
timeout=self.config.timeout,
|
|||
|
|
)
|
|||
|
|
return response
|
|||
|
|
|
|||
|
|
def _post(self, path: str, payload: dict[str, Any], *, _retry_on_401: bool = True) -> dict[str, Any]:
|
|||
|
|
response = self._request("POST", path, json=payload, _retry_on_401=_retry_on_401)
|
|||
|
|
return self._unwrap(response)
|
|||
|
|
|
|||
|
|
def _try_refresh_token(self) -> bool:
|
|||
|
|
# --- 新路径:委托给全局 CloudSession ---
|
|||
|
|
if self._cloud_session is not None:
|
|||
|
|
ok = self._cloud_session.try_refresh()
|
|||
|
|
if ok:
|
|||
|
|
# 刷新成功后同步更新本地 config 和 session headers
|
|||
|
|
new_config = self._cloud_session.get_config()
|
|||
|
|
self.config.access_token = new_config.access_token
|
|||
|
|
self.config.refresh_token = new_config.refresh_token
|
|||
|
|
self.session.headers.update({"Authorization": f"Bearer {new_config.access_token}"})
|
|||
|
|
return ok
|
|||
|
|
|
|||
|
|
# --- 旧路径(向后兼容,无 cloud_session 时使用) ---
|
|||
|
|
refresh = self.config.refresh_token.strip()
|
|||
|
|
if not refresh:
|
|||
|
|
return False
|
|||
|
|
with self._refresh_lock:
|
|||
|
|
refresh = self.config.refresh_token.strip()
|
|||
|
|
if not refresh:
|
|||
|
|
return False
|
|||
|
|
try:
|
|||
|
|
resp = requests.post(
|
|||
|
|
f"{self.base_url}/api/v1/auth/refresh",
|
|||
|
|
json={"refresh_token": refresh},
|
|||
|
|
timeout=self.config.timeout,
|
|||
|
|
)
|
|||
|
|
if resp.status_code != 200:
|
|||
|
|
return False
|
|||
|
|
body = resp.json()
|
|||
|
|
data = body.get("data", body) if isinstance(body, dict) else {}
|
|||
|
|
if not isinstance(data, dict):
|
|||
|
|
return False
|
|||
|
|
new_access = (data.get("access_token") or "").strip()
|
|||
|
|
new_refresh = (data.get("refresh_token") or "").strip()
|
|||
|
|
if not new_access:
|
|||
|
|
return False
|
|||
|
|
self.config.access_token = new_access
|
|||
|
|
self.session.headers.update({"Authorization": f"Bearer {new_access}"})
|
|||
|
|
if new_refresh:
|
|||
|
|
self.config.refresh_token = new_refresh
|
|||
|
|
try:
|
|||
|
|
self._on_token_refreshed(self.config.access_token, self.config.refresh_token)
|
|||
|
|
except Exception:
|
|||
|
|
logger.exception("SakuraCloudClient._on_token_refreshed failed")
|
|||
|
|
return True
|
|||
|
|
except Exception:
|
|||
|
|
logger.exception("SakuraCloudClient._try_refresh_token error")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
@staticmethod
|
|||
|
|
def _unwrap(response: requests.Response) -> dict[str, Any]:
|
|||
|
|
response.raise_for_status()
|
|||
|
|
body = response.json()
|
|||
|
|
if isinstance(body, dict) and "data" in body:
|
|||
|
|
return body["data"]
|
|||
|
|
return body
|
|||
|
|
|
|||
|
|
|
|||
|
|
class SyncManager:
|
|||
|
|
def __init__(self, client: SakuraCloudClient, failure_queue: SyncFailureQueue | None = None) -> None:
|
|||
|
|
self.client = client
|
|||
|
|
self.failure_queue = failure_queue or SyncFailureQueue()
|
|||
|
|
|
|||
|
|
def sync_file(self, file_path: str | Path, metadata: SyncMetadata, *, mime_type: str | None = None) -> dict[str, Any]:
|
|||
|
|
try:
|
|||
|
|
return self._sync_file_once(file_path, metadata, mime_type=mime_type)
|
|||
|
|
except Exception as exc:
|
|||
|
|
self.failure_queue.add(file_path, metadata, mime_type, str(exc))
|
|||
|
|
raise
|
|||
|
|
|
|||
|
|
def _sync_file_once(self, file_path: str | Path, metadata: SyncMetadata, *, mime_type: str | None = None) -> dict[str, Any]:
|
|||
|
|
path = Path(file_path)
|
|||
|
|
sha256 = file_sha256(path)
|
|||
|
|
file_size = path.stat().st_size
|
|||
|
|
checked = self.client.check_file(sha256, file_size)
|
|||
|
|
file_id = checked.get("file_id") if checked.get("owned_by_user") else None
|
|||
|
|
|
|||
|
|
if not file_id:
|
|||
|
|
init = self.client.init_upload(
|
|||
|
|
sha256=sha256,
|
|||
|
|
file_name=path.name,
|
|||
|
|
file_size=file_size,
|
|||
|
|
mime_type=mime_type,
|
|||
|
|
resource_type=self._upload_resource_type(metadata, mime_type),
|
|||
|
|
)
|
|||
|
|
if init.get("exists") and init.get("file_id"):
|
|||
|
|
file_id = str(init["file_id"])
|
|||
|
|
else:
|
|||
|
|
upload_id = str(init["upload_id"])
|
|||
|
|
with path.open("rb") as fp:
|
|||
|
|
for chunk in iter(lambda: fp.read(self.client.config.chunk_size), b""):
|
|||
|
|
self.client.upload_chunk(upload_id, chunk)
|
|||
|
|
completed = self.client.complete_upload(upload_id)
|
|||
|
|
file_id = str(completed["file_id"])
|
|||
|
|
|
|||
|
|
return self.client.sync_metadata(str(file_id), metadata)
|
|||
|
|
|
|||
|
|
@staticmethod
|
|||
|
|
def _upload_resource_type(metadata: SyncMetadata, mime_type: str | None) -> str:
|
|||
|
|
if metadata.resource_type == "novel":
|
|||
|
|
return "novel"
|
|||
|
|
if (mime_type or "").lower().startswith("video/") or (metadata.content_type or "").lower() == "video":
|
|||
|
|
return "video"
|
|||
|
|
return "image"
|
|||
|
|
|
|||
|
|
def retry_failed(self, *, limit: int = 5) -> dict[str, int]:
|
|||
|
|
now = time.time()
|
|||
|
|
items = self.failure_queue.load()
|
|||
|
|
kept: list[dict[str, Any]] = []
|
|||
|
|
retried = 0
|
|||
|
|
succeeded = 0
|
|||
|
|
failed = 0
|
|||
|
|
for item in items:
|
|||
|
|
if retried >= limit or float(item.get("next_retry_at") or 0) > now:
|
|||
|
|
kept.append(item)
|
|||
|
|
continue
|
|||
|
|
path = Path(str(item.get("file_path") or ""))
|
|||
|
|
if not path.is_file():
|
|||
|
|
failed += 1
|
|||
|
|
continue
|
|||
|
|
try:
|
|||
|
|
metadata = SyncMetadata(**dict(item.get("metadata") or {}))
|
|||
|
|
self._sync_file_once(path, metadata, mime_type=item.get("mime_type"))
|
|||
|
|
succeeded += 1
|
|||
|
|
except Exception as exc:
|
|||
|
|
attempts = int(item.get("attempts") or 0) + 1
|
|||
|
|
item["attempts"] = attempts
|
|||
|
|
item["last_error"] = str(exc)
|
|||
|
|
item["updated_at"] = now
|
|||
|
|
item["next_retry_at"] = now + min(3600, 60 * (2 ** min(attempts, 5)))
|
|||
|
|
kept.append(item)
|
|||
|
|
failed += 1
|
|||
|
|
retried += 1
|
|||
|
|
self.failure_queue.save(kept)
|
|||
|
|
return {"retried": retried, "succeeded": succeeded, "failed": failed, "remaining": len(kept)}
|