Sharing a toolbox meant exporting a JSON file and passing it around by hand. A toolbox can now be sent from the site while staying local first: the browser compresses and encrypts the v2 export with AES-GCM, and the key lives only in the link fragment, so the server keeps for at most 24 hours a copy it cannot read. - Add the /api/shares service: upload, metadata, download and revoke, with files in SHARES_DIR, a purge every 10 minutes, and single-use links (the default) deleted only once fully downloaded. - Cap the server load: 50 MiB per share, total storage, free disk space, daily volume, concurrent and stalled uploads, reads per link, plus a wide per-connection cap kept in memory without storing IPs. - Limit each browser to 2 shares and 100 MiB per day and 2 active links, and show the existing link of a toolbox already shared. - Add the send dialog (card and game drawer) and the /receive page, which previews or directly imports through the existing import path. - Move shared HTTP helpers to services/http.js for both APIs. - Document the format, delivery, server layout and privacy wording. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
63 lines
2.5 KiB
JavaScript
63 lines
2.5 KiB
JavaScript
// Rôle : enchaîne l'export, le chiffrement, les quotas et l'envoi d'une toolbox, puis la révocation de ses liens.
|
|
import { encryptSharePayload } from "./shareCrypto.js";
|
|
import { createShareQuota } from "./shareQuota.js";
|
|
import { loadShareMeta, revokeShare, uploadShare } from "./sharesApi.js";
|
|
|
|
export class ShareLimitError extends Error {
|
|
constructor(reason, details = {}) {
|
|
super(reason);
|
|
this.name = "ShareLimitError";
|
|
this.reason = reason;
|
|
Object.assign(this, details);
|
|
}
|
|
}
|
|
|
|
export function shareLink(id, key, origin = globalThis.location?.origin || "") {
|
|
return `${origin}/#/receive/${id}#${key}`;
|
|
}
|
|
|
|
export async function createToolboxShare({ toolboxId, buildPayload, maxBytes, singleUse = true, onStage, quota = createShareQuota() }) {
|
|
const existing = quota.activeShareFor(toolboxId);
|
|
if (existing) return existing;
|
|
const before = quota.check(0);
|
|
if (!before.ok) throw new ShareLimitError(before.reason, { retryAt: before.retryAt });
|
|
|
|
onStage?.({ stage: "preparing" });
|
|
const built = await buildPayload(toolboxId);
|
|
if (!built) throw new Error("La toolbox n'a pas pu être lue. Réessayez.");
|
|
const { bytes, key } = await encryptSharePayload(built.payload);
|
|
if (bytes.length > maxBytes) throw new ShareLimitError("too-large", { size: bytes.length, maxBytes });
|
|
const sized = quota.check(bytes.length);
|
|
if (!sized.ok) throw new ShareLimitError(sized.reason, { retryAt: sized.retryAt });
|
|
|
|
onStage?.({ stage: "uploading", progress: 0 });
|
|
const result = await uploadShare(bytes, { singleUse, onProgress: (progress) => onStage?.({ stage: "uploading", progress }) });
|
|
return quota.record({
|
|
id: result.id,
|
|
toolboxId,
|
|
toolboxName: built.toolbox.name,
|
|
expiresAt: result.expiresAt,
|
|
deleteToken: result.deleteToken,
|
|
singleUse: result.singleUse,
|
|
link: shareLink(result.id, key)
|
|
}, bytes.length);
|
|
}
|
|
|
|
export async function revokeToolboxShare(shareId, quota = createShareQuota()) {
|
|
const entry = quota.activeShares().find((item) => item.id === shareId);
|
|
if (!entry) return;
|
|
await revokeShare(entry.id, entry.deleteToken);
|
|
quota.forget(entry.id);
|
|
}
|
|
|
|
// Retire de la liste les liens que le serveur ne connaît plus : utilisés, révoqués ailleurs ou expirés.
|
|
export async function refreshActiveShares(quota = createShareQuota()) {
|
|
await Promise.all(quota.activeShares().map(async (entry) => {
|
|
try {
|
|
await loadShareMeta(entry.id);
|
|
} catch (error) {
|
|
if (error.status === 404) quota.forget(entry.id);
|
|
}
|
|
}));
|
|
return quota.activeShares();
|
|
}
|