fix: 导出凭据时弹出另存为窗口
This commit is contained in:
@@ -104,6 +104,11 @@ class CredentialBody(BaseModel):
|
||||
language: str = ""
|
||||
|
||||
|
||||
@router.post("/login/credential/save")
|
||||
def save_credential():
|
||||
return call(db_login.save_credential_file)
|
||||
|
||||
|
||||
@router.post("/login/credential")
|
||||
def import_credential(body: CredentialBody):
|
||||
return call(lambda: db_login.import_credential(body.model_dump(), body.language))
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user