Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

πŸ› fix: keep image/svg+xml for SVG files uploaded from the CLI (#20951)

YuTengjing9bede96c83a3
diff --git a/packages/utils/src/mimeType.test.ts b/packages/utils/src/mimeType.test.ts
index b270f0f4837..a8d359ac680 100644
--- a/packages/utils/src/mimeType.test.ts
+++ b/packages/utils/src/mimeType.test.ts
@@ -183,6 +183,39 @@ describe('resolveMimeType', () => {
);
});
+ it('keeps image/svg+xml for SVG instead of downgrading it to text/plain', async () => {
+ // SVG is text, but browsers only render it as an image from image/svg+xml.
+ const svgSource = Buffer.from(
+ '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10"><path d="M0 10L5 0L10 10z"/></svg>\n',
+ );
+ await expect(resolveMimeType('/repo/shape.svg', svgSource)).resolves.toBe('image/svg+xml');
+ });
+
+ it('keeps the text downgrade for other text extensions mime-db maps to non-text types', async () => {
+ const mtsSource = Buffer.from('export const bar = 1;\n');
+ await expect(resolveMimeType('/repo/module.mts', mtsSource)).resolves.toBe(
+ 'text/plain; charset=utf-8',
+ );
+
+ // Non-SVG `+xml` types stay on the downgrade so text previews keep working.
+ const atomFeed = Buffer.from('<feed xmlns="http://www.w3.org/2005/Atom"></feed>\n');
+ await expect(resolveMimeType('/repo/feed.atom', atomFeed)).resolves.toBe(
+ 'text/plain; charset=utf-8',
+ );
+ });
+
+ it('prefers the SVG extension over the generic XML match for an `<?xml` prolog', async () => {
+ // file-type fingerprints the prolog as application/xml, which browsers won't render as an image.
+ const exportedSvg = Buffer.from(
+ '<?xml version="1.0" encoding="UTF-8"?>\n<svg xmlns="http://www.w3.org/2000/svg"/>\n',
+ );
+ await expect(resolveMimeType('/repo/exported.svg', exportedSvg)).resolves.toBe('image/svg+xml');
+
+ // Plain .xml keeps the magic-byte result.
+ const plainXml = Buffer.from('<?xml version="1.0"?><root/>');
+ await expect(resolveMimeType('/repo/data.xml', plainXml)).resolves.toBe('application/xml');
+ });
+
it('falls back to text/plain for unknown text extensions via the sniff', async () => {
await expect(resolveMimeType('/repo/App.tsx', Buffer.from(''))).resolves.toBe(
'text/plain; charset=utf-8',
diff --git a/packages/utils/src/mimeType.ts b/packages/utils/src/mimeType.ts
index b327b9f0194..19add37b126 100644
--- a/packages/utils/src/mimeType.ts
+++ b/packages/utils/src/mimeType.ts
@@ -32,6 +32,12 @@ const BINARY_FALLBACK_MIME = 'application/octet-stream';
const TEXT_MIME_PATTERN =
/^(?:text\/|application\/(?:ecmascript|graphql|javascript|json|toml|xhtml\+xml|xml|yaml))/;
+// SVG is text on disk but must keep its registered type: browsers only render
+// it as an image from `image/svg+xml`, never from text/plain or application/xml.
+// Other `+xml` types (atom, rss, …) stay on the text downgrade so text previews
+// keep working for them.
+const SVG_MIME = 'image/svg+xml';
+
const withCharset = (mimeType: string): string =>
TEXT_MIME_PATTERN.test(mimeType) && !mimeType.includes('charset=')
? `${mimeType}; charset=utf-8`
@@ -89,10 +95,13 @@ export const tryGetMimeType = (filePath: string): string | undefined => {
* sniff overrule mime-db when the two disagree.
*
* 1. `fileTypeFromBuffer()` β€” magic-byte detection. Authoritative when it
- * matches: real MPEG-TS media wins here regardless of extension.
+ * matches: real MPEG-TS media wins here regardless of extension. The one
+ * exception is a generic `application/xml` match, which yields to the
+ * `.svg` extension (an SVG with an `<?xml` prolog).
* 2. Extension lookup (custom map + `mime.getType`) β€” the only signal for
* text formats file-type can't fingerprint (json/css/md/svg/…).
* - Text mime β†’ use as-is (with charset appended).
+ * - `image/svg+xml` β†’ use as-is; it is text but must render as an image.
* - Binary mime + non-empty buffer that sniffs as text β†’ downgrade to
* text/plain. Saves TypeScript source from mime-db's `video/mp2t`.
* - Otherwise β†’ use the extension mime.
@@ -101,12 +110,18 @@ export const tryGetMimeType = (filePath: string): string | undefined => {
export const resolveMimeType = async (filePath: string, buffer: Uint8Array): Promise<string> => {
const { fileTypeFromBuffer } = await import('file-type');
const detected = await fileTypeFromBuffer(buffer);
- if (detected?.mime) return detected.mime;
+ const fromExtension = lookupExtensionMime(filePath);
+ if (detected?.mime) {
+ // file-type fingerprints any `<?xml` prolog as generic application/xml,
+ // which would hide an SVG's image/svg+xml; the extension is more specific.
+ if (detected.mime === 'application/xml' && fromExtension === SVG_MIME) return SVG_MIME;
+ return detected.mime;
+ }
const isBinary = looksBinary(buffer);
- const fromExtension = lookupExtensionMime(filePath);
if (fromExtension) {
if (TEXT_MIME_PATTERN.test(fromExtension)) return withCharset(fromExtension);
+ if (fromExtension === SVG_MIME) return SVG_MIME;
if (buffer.byteLength > 0 && !isBinary) return TEXT_FALLBACK_MIME;
return fromExtension;
}