| 1 | // attachDedup centralizes the small deduplication helpers the composer |
| 2 | // uses when adding attachments. The composer's image paste/drop already |
| 3 | // works, but a user can drop the same file twice (or paste the same |
| 4 | // clipboard twice) and end up with two @path references pointing to |
| 5 | // the same on-disk blob — which the kernel would re-process. Dedup |
| 6 | // keys on the SHA-256 of the file bytes, with a path fallback for the |
| 7 | // case where a file:// URL or data: URL is the only available signal. |
| 8 | |
| 9 | const HEX = "0123456789abcdef"; |
| 10 | |
| 11 | const ordinaryFileSources = new WeakMap<File, string>(); |
| 12 | |
| 13 | // File metadata cannot distinguish two different pathless files with the same |
| 14 | // name, size and timestamp. Keep object identity without reading their bytes. |
| 15 | export function ordinaryFileSource(file: File, nativePath: string): string { |
| 16 | if (nativePath) return `path:${nativePath}`; |
| 17 | let source = ordinaryFileSources.get(file); |
| 18 | if (!source) { |
| 19 | source = `file:${crypto.randomUUID()}`; |
| 20 | ordinaryFileSources.set(file, source); |
| 21 | } |
| 22 | return source; |
| 23 | } |
| 24 | |
| 25 | function bytesToHex(bytes: Uint8Array): string { |
| 26 | let out = ""; |
| 27 | for (let i = 0; i < bytes.length; i++) { |
| 28 | const b = bytes[i]; |
| 29 | out += HEX[(b >> 4) & 0xf] + HEX[b & 0xf]; |
| 30 | } |
| 31 | return out; |
| 32 | } |
| 33 | |
| 34 | // sha256 returns the hex SHA-256 of `blob`. The Web Crypto Subtle API |
| 35 | // is available in the Chromium shell; we |
| 36 | // don't fall back to a JS implementation because a no-op (returning |
| 37 | // "") would silently disable dedup, which is worse than no dedup |
| 38 | // at all. The caller checks the empty-string return and skips the |
| 39 | // dedup step in that case. |
| 40 | export async function sha256(blob: Blob): Promise<string> { |
| 41 | if (typeof crypto === "undefined" || !crypto.subtle) return ""; |
| 42 | try { |
| 43 | const buf = await blob.arrayBuffer(); |
| 44 | const digest = await crypto.subtle.digest("SHA-256", buf); |
| 45 | return bytesToHex(new Uint8Array(digest)); |
| 46 | } catch { |
| 47 | return ""; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // DedupIndex tracks the SHA-256 hashes the user has already attached |
| 52 | // in the current composer session (lives for the life of the App |
| 53 | // mount; cleared on new session because the user expects a fresh |
| 54 | // palette). A path-keyed fallback lets a non-Crypto-capable browser |
| 55 | // still dedup by URL when the same path is dropped twice — the |
| 56 | // fallback is weaker (the same content from two paths won't match) |
| 57 | // but covers the common "dropped the same file twice" case. |
| 58 | export class DedupIndex { |
| 59 | private hashes = new Set<string>(); |
| 60 | private paths = new Set<string>(); |
| 61 | |
| 62 | seen(hash: string, path: string): boolean { |
| 63 | if (hash) { |
| 64 | if (this.hashes.has(hash)) return true; |
| 65 | return false; |
| 66 | } |
| 67 | return this.paths.has(path); |
| 68 | } |
| 69 | |
| 70 | add(hash: string, path: string): void { |
| 71 | if (hash) this.hashes.add(hash); |
| 72 | this.paths.add(path); |
| 73 | } |
| 74 | |
| 75 | forget(hash: string, path: string): void { |
| 76 | if (hash) this.hashes.delete(hash); |
| 77 | this.paths.delete(path); |
| 78 | } |
| 79 | |
| 80 | clear(): void { |
| 81 | this.hashes.clear(); |
| 82 | this.paths.clear(); |
| 83 | } |
| 84 | } |
| 85 |