Files
webmail/components/contacts/contact-export.tsx
T
Linus Rath 6bd55c9501 fix: export contact photos in batches on Stalwart 1.0
On 1.0 a vCard export sent every blob photo through the avatar download
queue, a burst of 40 and then 6 a second, two at a time: about 160 s for
1000 photos. Avatars and print (which gives up after 5 s) waited behind
it, the Export button showed nothing, and a second click started the
whole export again.

- Export fetches the blob media of all cards with Blob/get
  (`data:asBase64`), asking for the sizes first and packing the requests
  to about 4 MiB. `urn:ietf:params:jmap:blob` goes into `using` only
  when the session advertises it; without it, or when a request is
  refused, the rest is downloaded one by one behind avatars and print,
  which keep half of the download burst for themselves.
- The export buttons in the settings, the bulk bar, the context menu and
  the detail menu show that an export is running and ignore clicks until
  it is done.
- When the photo changes, the form rebuilds `media` on the card as the
  server has it now. A logo or sound blobId from the list's older copy
  made 1.0 refuse the whole update ("The following blobIds do not exist
  or are not accessible").
- A new photo takes the key, place, `pref`, `label` and `contexts` of the
  one it replaces, so it stays the photo shown on cards with several.
  Removing the photo removes every photo the card had instead of only the
  shown one, which brought up the next; logos and sounds stay.
- A card whose login has no client downloads nothing, instead of asking
  the active login's server and remembering the refusal for two minutes.
- Photo downloads run at half the server's `maxConcurrentRequests`
  instead of a fixed two.
- `invalidProperties` naming `media/...` also counts as a refused blobId
  in media, not only `media` itself.
2026-09-25 20:05:19 +02:00

73 lines
2.4 KiB
TypeScript

"use client";
import { useSyncExternalStore } from "react";
import { generateVCard } from "@/lib/vcard";
import type { ContactCard } from "@/lib/jmap/types";
import { getContactDisplayName } from "@/stores/contact-store";
import { inlineContactMediaForExport } from "@/lib/contact-media";
import { getContactPhotoClient } from "@/hooks/use-contact-photo";
// On Stalwart 1.0 an export first downloads every photo, which takes a while
// for a large address book; a second click meanwhile would start it all over.
let exporting = false;
const busyListeners = new Set<() => void>();
function setExporting(value: boolean): void {
exporting = value;
for (const listener of busyListeners) listener();
}
function subscribeExporting(listener: () => void): () => void {
busyListeners.add(listener);
return () => {
busyListeners.delete(listener);
};
}
/** Whether a vCard export is running; the export buttons show it and wait. */
export function useContactExportBusy(): boolean {
return useSyncExternalStore(subscribeExporting, () => exporting, () => false);
}
/**
* Builds the .vcf and hands it to the browser. Resolves false, doing nothing,
* while another export is still running.
*/
async function runExport(contacts: ContactCard[], filename: string): Promise<boolean> {
if (exporting) return false;
setExporting(true);
try {
// Blob media (Stalwart 1.0) become data: URIs, which a vCard carries inline.
const cards = await inlineContactMediaForExport(contacts, getContactPhotoClient);
downloadVcf(generateVCard(cards), filename);
return true;
} finally {
setExporting(false);
}
}
export function exportContact(contact: ContactCard): Promise<boolean> {
const name = getContactDisplayName(contact) || "contact";
return runExport([contact], `${sanitizeFilename(name)}.vcf`);
}
export function exportContacts(contacts: ContactCard[]): Promise<boolean> {
return runExport(contacts, `contacts-${new Date().toISOString().slice(0, 10)}.vcf`);
}
function downloadVcf(content: string, filename: string) {
const blob = new Blob([content], { type: "text/vcard;charset=utf-8" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
function sanitizeFilename(name: string): string {
return name.replace(/[^a-zA-Z0-9_-]/g, "_").substring(0, 50);
}