fix(webui): copy and export files on Safari LAN HTTP

Clipboard APIs and instant blob downloads fail on insecure origins
and WebKit. Route every export through the shared helpers.
This commit is contained in:
DrSmoothl committed 2026-09-19 16:30:11 +08:00
1 parent 1ab14ea306
commit feea35da4c
9 files changed
+70 -126

No files matched your search

@@ -19,7 +19,7 @@ import { ConfirmDialog } from '@/components/confirm-dialog';
import { useActionFeedback } from '@/contexts/ActionFeedbackContext';
import { useApi } from '@/lib/api';
import { useFlashMessage } from '@/hooks/use-flash-message';
import { cn } from '@/lib/utils';
import { cn, downloadBlob } from '@/lib/utils';
import type { SystemSettingsResponse } from '@/types';
export function SystemPanel() {
@@ -157,13 +157,10 @@ export function SystemPanel() {
},
() => api.systemSettings.exportBackup(exportCreds),
);
const blob = new Blob([JSON.stringify(bundle, null, 2)], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `snowluma-backup-${new Date().toISOString().replace(/[:.]/g, '-')}.json`;
a.click();
URL.revokeObjectURL(url);
downloadBlob(
new Blob([JSON.stringify(bundle, null, 2)], { type: 'application/json' }),
`snowluma-backup-${new Date().toISOString().replace(/[:.]/g, '-')}.json`,
);
flash('ok', '已导出备份');
} catch (e) {
flash('err', e instanceof Error ? e.message : '导出失败');
@@ -9,6 +9,7 @@ import type { TotpEnrollment, TotpStatus } from '@/lib/api/types';
import { parseSecondFactor } from '@/lib/totp-second-factor';
import { TotpQr } from '@/components/settings/totp-qr';
import { TotpRecoveryCodes } from '@/components/settings/totp-recovery-codes';
import { copyText } from '@/lib/utils';
function SettingRow({
label,
@@ -198,6 +199,22 @@ function TotpSetupDialog({
<div className="flex flex-col items-center gap-3">
<TotpQr value={enrollment.otpauthUrl} label="2FA 绑定二维码" />
<p className="break-all text-center font-mono text-xs text-muted-foreground">{enrollment.secret}</p>
<div className="flex flex-wrap justify-center gap-2">
<Button
type="button"
variant="outline"
size="sm"
onClick={() => { void copyText(enrollment.secret); }}
>
复制密钥
</Button>
<a
href={enrollment.otpauthUrl}
className="inline-flex h-8 items-center rounded-md border border-border px-3 text-xs font-medium text-foreground"
>
在验证器中打开
</a>
</div>
</div>
)}
<div className="grid grid-cols-2 gap-3">
@@ -220,6 +237,8 @@ function TotpSetupDialog({
<Input
inputMode="numeric"
autoComplete="one-time-code"
maxLength={6}
pattern="[0-9]*"
placeholder="6 位验证码"
value={code}
onChange={(e) => setCode(e.target.value)}
@@ -1,27 +1,7 @@
import { useState } from 'react';
import { Check, Copy } from 'lucide-react';
import { Button } from '@/components/ui/button';
async function copyText(value: string): Promise<boolean> {
try {
await navigator.clipboard.writeText(value);
return true;
} catch {
try {
const ta = document.createElement('textarea');
ta.value = value;
ta.style.position = 'fixed';
ta.style.opacity = '0';
document.body.appendChild(ta);
ta.select();
const ok = document.execCommand('copy');
document.body.removeChild(ta);
return ok;
} catch {
return false;
}
}
}
import { copyText } from '@/lib/utils';
export function TotpRecoveryCodes({
codes,
@@ -31,6 +11,7 @@ export function TotpRecoveryCodes({
onConfirm: () => void;
}) {
const [copied, setCopied] = useState(false);
const [copyError, setCopyError] = useState(false);
const [saved, setSaved] = useState(false);
return (
@@ -40,8 +21,13 @@ export function TotpRecoveryCodes({
</p>
<ol className="grid grid-cols-2 gap-2 font-mono text-sm">
{codes.map((code) => (
<li key={code} className="rounded-md border bg-muted/40 px-3 py-2 text-center tracking-wide">
{code}
<li key={code}>
<input
readOnly
value={code}
className="w-full rounded-md border bg-muted/40 px-3 py-2 text-center tracking-wide outline-none"
onFocus={(e) => e.currentTarget.select()}
/>
</li>
))}
</ol>
@@ -51,12 +37,18 @@ export function TotpRecoveryCodes({
size="sm"
className="self-start"
onClick={async () => {
if (await copyText(codes.join('\n'))) setCopied(true);
const ok = await copyText(codes.join('\n'));
setCopied(ok);
setCopyError(!ok);
if (ok) window.setTimeout(() => setCopied(false), 1600);
}}
>
{copied ? <Check className="size-4" /> : <Copy className="size-4" />}
{copied ? '已复制' : '复制全部'}
</Button>
{copyError && (
<p className="text-xs text-destructive">复制失败,请长按上面的码手动拷贝。</p>
)}
<label className="flex items-start gap-2 text-sm">
<input
type="checkbox"