Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots πŸ“–

fix(export): make downloads reliable and sanitise the filename

Luis Ortize83e54520c98
diff --git a/src/core/export/__tests__/download.test.ts b/src/core/export/__tests__/download.test.ts
new file mode 100644
index 0000000..822a303
--- /dev/null
+++ b/src/core/export/__tests__/download.test.ts
@@ -0,0 +1,28 @@
+import { describe, expect, it } from 'vitest';
+import { safeFilename } from '@/core/export/download';
+
+describe('safeFilename', () => {
+ it('appends the extension to a clean title', () => {
+ expect(safeFilename('Invite a teammate', 'pdf')).toBe('Invite a teammate.pdf');
+ });
+
+ it('strips characters that would break or redirect the download path', () => {
+ expect(safeFilename('reports/2026: "Q3" <draft>|v2\\final?', 'docx')).toBe(
+ 'reports-2026- -Q3- -draft--v2-final-.docx',
+ );
+ });
+
+ it('collapses whitespace and trims', () => {
+ expect(safeFilename(' Guide on es.wikipedia.org ', 'html')).toBe('Guide on es.wikipedia.org.html');
+ });
+
+ it('falls back when the title reduces to nothing', () => {
+ expect(safeFilename(' ', 'md')).toBe('guide.md');
+ });
+
+ it('caps the length so the filesystem accepts it', () => {
+ const name = safeFilename('x'.repeat(400), 'pdf');
+ expect(name.length).toBe(124);
+ expect(name.endsWith('.pdf')).toBe(true);
+ });
+});
diff --git a/src/core/export/download.ts b/src/core/export/download.ts
new file mode 100644
index 0000000..d03a9e1
--- /dev/null
+++ b/src/core/export/download.ts
@@ -0,0 +1,27 @@
+const REVOKE_DELAY_MS = 60_000;
+
+export function safeFilename(name: string, extension: string): string {
+ const cleaned = name
+ .replace(/[\\/:*?"<>|]/g, '-')
+ .replace(/\s+/g, ' ')
+ .trim()
+ .slice(0, 120);
+ return `${cleaned || 'guide'}.${extension}`;
+}
+
+export function downloadBlob(blob: Blob, filename: string): void {
+ const url = URL.createObjectURL(blob);
+ const anchor = document.createElement('a');
+ anchor.href = url;
+ anchor.download = filename;
+ anchor.rel = 'noopener';
+ anchor.style.display = 'none';
+ document.body.appendChild(anchor);
+ anchor.click();
+ anchor.remove();
+ setTimeout(() => URL.revokeObjectURL(url), REVOKE_DELAY_MS);
+}
+
+export function downloadText(content: string, filename: string, mimeType: string): void {
+ downloadBlob(new Blob([content], { type: mimeType }), filename);
+}
diff --git a/src/ui/sidepanel/ExportMenu.tsx b/src/ui/sidepanel/ExportMenu.tsx
index 20411d0..ae0935d 100644
--- a/src/ui/sidepanel/ExportMenu.tsx
+++ b/src/ui/sidepanel/ExportMenu.tsx
@@ -1,6 +1,7 @@
import { Download, FileCode, FileDown, FileText, Loader2 } from 'lucide-react';
import { useEffect, useRef, useState } from 'react';
import { i18n } from '#imports';
+import { downloadBlob, downloadText, safeFilename } from '@/core/export/download';
import { exportGuideAsHTML } from '@/core/export/html-export';
import { exportGuideAsMarkdown } from '@/core/export/markdown-export';
import { exportGuideAsPDF } from '@/core/export/pdf-export';
@@ -15,20 +16,6 @@ interface ExportMenuProps {
screenshots: Map<string, Screenshot>;
}
-function downloadFile(content: string, filename: string, mimeType: string) {
- const blob = new Blob([content], { type: mimeType });
- downloadBlob(blob, filename);
-}
-
-function downloadBlob(blob: Blob, filename: string) {
- const url = URL.createObjectURL(blob);
- const a = document.createElement('a');
- a.href = url;
- a.download = filename;
- a.click();
- URL.revokeObjectURL(url);
-}
-
type ExportType = 'docx' | 'html' | 'markdown' | 'pdf';
export default function ExportMenu({
@@ -60,17 +47,15 @@ export default function ExportMenu({
if (type === 'html') {
const html = await exportGuideAsHTML(guide, steps, screenshots);
- downloadFile(html, `${guide.title}.html`, 'text/html');
+ downloadText(html, safeFilename(guide.title, 'html'), 'text/html');
} else if (type === 'docx') {
const { exportGuideAsDOCX } = await import('@/core/export/docx-export');
- const blob = await exportGuideAsDOCX(guide, steps, screenshots);
- downloadBlob(blob, `${guide.title}.docx`);
+ downloadBlob(await exportGuideAsDOCX(guide, steps, screenshots), safeFilename(guide.title, 'docx'));
} else if (type === 'markdown') {
const md = await exportGuideAsMarkdown(guide, steps, screenshots);
- downloadFile(md, `${guide.title}.md`, 'text/markdown');
+ downloadText(md, safeFilename(guide.title, 'md'), 'text/markdown');
} else {
- const blob = await exportGuideAsPDF(guide, steps, screenshots);
- downloadBlob(blob, `${guide.title}.pdf`);
+ downloadBlob(await exportGuideAsPDF(guide, steps, screenshots), safeFilename(guide.title, 'pdf'));
}
} finally {
setExporting(false);