fix: 导出凭据时弹出另存为窗口

This commit is contained in:
Nixevol
2026-10-29 21:02:46 +08:00
parent 4dfd947488
commit f6274c6ff6
7 changed files with 67 additions and 12 deletions
+51
View File
@@ -1,4 +1,7 @@
import json
import secrets
import subprocess
from pathlib import Path
from server.services.dramabox.client import client
from server.services.dramabox.locales import DEFAULT_LANGUAGE, public_list, resolve
@@ -107,6 +110,54 @@ def export_credential() -> dict:
return {"kind": _CREDENTIAL_KIND, "version": _CREDENTIAL_VERSION, "app": app}
def save_credential_file() -> dict:
document = export_credential()
chosen = _pick_credential_path()
if not chosen:
return {"path": ""}
path = Path(chosen)
if path.suffix.lower() != ".json":
path = path.with_suffix(".json")
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(document, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
return {"path": str(path)}
def _pick_credential_path() -> str:
script = """
Add-Type -AssemblyName System.Windows.Forms
$owner = New-Object System.Windows.Forms.Form
$owner.TopMost = $true
$owner.ShowInTaskbar = $false
$owner.Opacity = 0
$owner.Show()
$owner.Activate()
$dialog = New-Object System.Windows.Forms.SaveFileDialog
$dialog.Title = '导出登录凭据'
$dialog.Filter = 'JSON 文件 (*.json)|*.json'
$dialog.FileName = 'dramabox-login.json'
$dialog.DefaultExt = 'json'
$dialog.AddExtension = $true
$dialog.OverwritePrompt = $true
$downloads = Join-Path $env:USERPROFILE 'Downloads'
if (Test-Path $downloads) { $dialog.InitialDirectory = $downloads }
$result = $dialog.ShowDialog($owner)
$owner.Close()
if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
[Console]::OutputEncoding = New-Object System.Text.UTF8Encoding $false
[Console]::Out.WriteLine($dialog.FileName)
}
"""
completed = subprocess.run(
["powershell", "-NoProfile", "-STA", "-Command", script],
capture_output=True,
text=True,
encoding="utf-8",
check=False,
)
return (completed.stdout or "").strip()
def import_credential(payload: dict, language: str = "") -> dict:
record = _credential_record(payload)
try: