Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mail): match inline image CIDs exactly

Zhao Dengfenge0ce9aa3e8fa
diff --git a/packages/maker-shared/src/__tests__/ghostCardForm.test.ts b/packages/maker-shared/src/__tests__/ghostCardForm.test.ts
index 2d7ff4c768..4c6001a36c 100644
--- a/packages/maker-shared/src/__tests__/ghostCardForm.test.ts
+++ b/packages/maker-shared/src/__tests__/ghostCardForm.test.ts
@@ -107,6 +107,17 @@ describe("declarative card forms", () => {
expect(visibleMailAttachments({ ...values, bodyHtml: '' }).map(f => f.key)).toEqual(['body', 'unused', 'pdf']);
expect(visibleMailAttachments({ ...values, bodyHtml: '', quoteHtml: '' })).toEqual(files);
});
+ // Unit regression through insertMailImage: attachment-list coverage already checked CID
+ // prefixes, but insertion still used substring matching. `cid:photo-large` plus a `photo`
+ // attachment was the minimal input that incorrectly left the user's draft unchanged.
+ it('inserts an image when another inline CID only shares its prefix', () => {
+ const values = { bodyHtml: '<img src="cid:photo-large">', quoteHtml: '', attachmentItems: JSON.stringify([
+ { key: 'photo', filename: 'photo.png', mimeType: 'image/png', contentId: 'photo' },
+ ]) };
+ const next = insertMailImage(values, 'attachmentItems', 'photo');
+ expect(next.bodyHtml).toContain('<img src="cid:photo-large">');
+ expect(next.bodyHtml).toContain('<img src="cid:photo" alt="photo.png"');
+ });
it('inserts existing image bytes with an unused CID and keeps preview addresses out of the authored HTML', () => {
const url = `cindy-media://blobs/${'a'.repeat(64)}.png`;
const values = { bodyHtml: '<html><head><style>img{width:200px}</style></head><body><p>Hi</p></body></html>', attachmentItems: JSON.stringify([
diff --git a/packages/maker-shared/src/ghostCardForm.ts b/packages/maker-shared/src/ghostCardForm.ts
index 95a7a01325..86ab36ed39 100644
--- a/packages/maker-shared/src/ghostCardForm.ts
+++ b/packages/maker-shared/src/ghostCardForm.ts
@@ -27,11 +27,7 @@ export function parseMailAttachmentItems(value: string): MailAttachmentItem[] |
...(typeof i.previewUrl === 'string' && /^cindy-media:\/\/blobs\/[a-f0-9]{64}\.(png|jpg|jpeg|gif|webp)$/.test(i.previewUrl) ? { previewUrl: i.previewUrl } : {}) }));
} catch { return null; }
}
-/** Presentation only: body images keep their MIME parts and preview URLs, but
- * do not also appear as attachments. Removing the reference reveals them again. */
-export function visibleMailAttachments(values: Record<string, string>, field = 'attachmentItems'): MailAttachmentItem[] {
- const files = parseMailAttachmentItems(values[field]) || [];
- if (!files.some(file => file.contentId)) return files;
+function usedMailImageCids(values: Record<string, string>): Set<string> {
const used = new Set<string>();
const walk = (node: DefaultTreeAdapterTypes.Node) => {
if ('tagName' in node) {
@@ -44,6 +40,14 @@ export function visibleMailAttachments(values: Record<string, string>, field = '
if ('childNodes' in node) for (const child of node.childNodes) walk(child);
};
for (const name of ['bodyHtml', 'quoteHtml']) if (values[name]) walk(parse(values[name]));
+ return used;
+}
+/** Presentation only: body images keep their MIME parts and preview URLs, but
+ * do not also appear as attachments. Removing the reference reveals them again. */
+export function visibleMailAttachments(values: Record<string, string>, field = 'attachmentItems'): MailAttachmentItem[] {
+ const files = parseMailAttachmentItems(values[field]) || [];
+ if (!files.some(file => file.contentId)) return files;
+ const used = usedMailImageCids(values);
return files.filter(file => !file.contentId || !used.has(file.contentId.replace(/^<|>$/g, '')));
}
/** Remove the MIME part and its visible CID image together, preserving the rest
@@ -64,7 +68,7 @@ export function removeMailAttachment(values: Record<string, string>, field: stri
export function canInsertMailImage(values: Record<string, string>, file: MailAttachmentItem): boolean {
if (typeof values.bodyHtml !== 'string' || !/^image\/(png|jpeg|gif|webp)$/i.test(file.mimeType || '')) return false;
const cid = file.contentId?.replace(/^<|>$/g, '');
- return !cid || !(values.bodyHtml + (values.quoteHtml || '')).includes('cid:' + cid);
+ return !cid || !usedMailImageCids(values).has(cid);
}
/** Reuse an existing MIME part. Only its CID/disposition changes; its bytes
* remain at the provider. Preview URLs are never written into the HTML. */