diff --git a/CHANGELOG.md b/CHANGELOG.md index f379eae..ce2d980 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,16 @@ The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and No unreleased changes at this time. Future work tracked in `docs/ROADMAP.md` under "Beyond Phase 3". +## [1.3.2] — 2026-05-13 + +### Fixed + +- **Tauri native drag-drop bypassed the batch extension filter**. v1.3.0/v1.3.1 desktop bundles accepted any file dropped from File Explorer / Finder / Nautilus into the workspace, because `ConverterWorkspace.handleMultiFileDrop` called `batchStore.addFiles` directly — skipping the `direction × format` extension whitelist that the web `react-dropzone` path enforced. Files like `.cs`/`.py` leaked into the queue with no warning. Extracted `src/lib/file-filter.ts` (`getAllowedExtensions` + `extOf` + `filterByExtension`) and routed the Tauri callback, `BatchPanel`, and `InputPanel` through it so the wrong-format toast surfaces in all three entry points. + +### Tests + +- 7 new unit tests for `getAllowedExtensions`, `extOf`, and `filterByExtension` in `src/lib/__tests__/file-filter.test.ts`. Total tests: **190**. + ## [1.3.1] — 2026-05-13 ### Fixed diff --git a/package-lock.json b/package-lock.json index 0e5fda3..fd5bbd3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "jsonprism", - "version": "1.3.1", + "version": "1.3.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "jsonprism", - "version": "1.3.1", + "version": "1.3.2", "license": "Apache-2.0", "dependencies": { "@codemirror/lang-json": "^6.0.0", diff --git a/package.json b/package.json index 12cf248..fcf16f7 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "jsonprism", "private": true, - "version": "1.3.1", + "version": "1.3.2", "type": "module", "description": "Disperse JSON into many formats — JSONL, CSV, YAML, XML, TOML, RESX, Markdown, SQL — fully client-side.", "author": "poli0981 ", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 56d7727..43f18f7 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "jsonprism" -version = "1.3.1" +version = "1.3.2" description = "Disperse JSON into many formats." authors = ["poli0981 "] license = "Apache-2.0" diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 72e3a6c..d7941aa 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -1,7 +1,7 @@ { "$schema": "https://schema.tauri.app/config/2", "productName": "JSONPrism", - "version": "1.3.1", + "version": "1.3.2", "identifier": "com.poli0981.jsonprism", "build": { "beforeDevCommand": "npm run dev", diff --git a/src/components/converter/BatchPanel.tsx b/src/components/converter/BatchPanel.tsx index 910000e..5dc2b09 100644 --- a/src/components/converter/BatchPanel.tsx +++ b/src/components/converter/BatchPanel.tsx @@ -13,6 +13,7 @@ import { import { toast } from '@/components/ui/sonner'; import { useBatchStore, BATCH_MAX_FILES, type BatchItem } from '@/stores/batchStore'; import { processBatch, zipOutputs, downloadBlob } from '@/lib/batch-processor'; +import { filterByExtension, getAllowedExtensions } from '@/lib/file-filter'; import { isTauri, nativeOpenFiles, nativeSaveBlob, fileFromNativePath } from '@/lib/tauri'; import { getConverter } from '@/converters/registry'; import type { FormatId } from '@/converters/types'; @@ -68,19 +69,14 @@ export function BatchPanel({ format, direction, options, open, onOpenChange }: B // Whitelist of file extensions that match the current direction × format. // The browser's native `` is only a hint, so we re-validate // in JS to reject files chosen via "Browse" with "All files" filter. - const allowedExtensions = useMemo(() => { - return isReverse ? [converter.meta.extension.toLowerCase(), 'txt'] : ['json', 'txt']; - }, [isReverse, converter.meta.extension]); + const allowedExtensions = useMemo( + () => getAllowedExtensions(isReverse ? 'reverse' : 'forward', converter.meta.extension), + [isReverse, converter.meta.extension], + ); const handleAddFiles = useCallback( (files: File[]) => { - const valid: File[] = []; - let wrongFormat = 0; - for (const f of files) { - const ext = (f.name.split('.').pop() ?? '').toLowerCase(); - if (allowedExtensions.includes(ext)) valid.push(f); - else wrongFormat += 1; - } + const { valid, wrongFormat } = filterByExtension(files, allowedExtensions); if (wrongFormat > 0) { toast.warning(t('batch.toast.wrong_format', { count: wrongFormat })); } diff --git a/src/components/converter/ConverterWorkspace.tsx b/src/components/converter/ConverterWorkspace.tsx index 39d0504..71705b9 100644 --- a/src/components/converter/ConverterWorkspace.tsx +++ b/src/components/converter/ConverterWorkspace.tsx @@ -4,6 +4,7 @@ import { toast } from '@/components/ui/sonner'; import { getConverter } from '@/converters/registry'; import type { FormatId } from '@/converters/types'; import { parseJsonInput } from '@/lib/detect'; +import { filterByExtension, getAllowedExtensions } from '@/lib/file-filter'; import { useKeyboardShortcuts } from '@/hooks/useKeyboardShortcuts'; import { useTauriDragDrop } from '@/hooks/useTauriDragDrop'; import { useBatchStore } from '@/stores/batchStore'; @@ -39,7 +40,16 @@ export function ConverterWorkspace() { const handleMultiFileDrop = useCallback( (files: File[]) => { - const result = addBatchFiles(files); + const allowed = getAllowedExtensions( + direction === 'reverse' ? 'reverse' : 'forward', + getConverter(format).meta.extension, + ); + const { valid, wrongFormat } = filterByExtension(files, allowed); + if (wrongFormat > 0) { + toast.warning(t('batch.toast.wrong_format', { count: wrongFormat })); + } + if (valid.length === 0) return; + const result = addBatchFiles(valid); if (result.added > 0) { toast.success(t('batch.toast.added', { count: result.added })); setBatchOpen(true); @@ -52,7 +62,7 @@ export function ConverterWorkspace() { } } }, - [addBatchFiles, t], + [addBatchFiles, direction, format, t], ); // Native OS drag-drop bridge (no-op outside Tauri). diff --git a/src/components/converter/InputPanel.tsx b/src/components/converter/InputPanel.tsx index 71956d4..7ca0aee 100644 --- a/src/components/converter/InputPanel.tsx +++ b/src/components/converter/InputPanel.tsx @@ -3,6 +3,7 @@ import { useTranslation } from 'react-i18next'; import { useDropzone } from 'react-dropzone'; import { Eraser, FileText, FolderOpen } from 'lucide-react'; import { toast } from '@/components/ui/sonner'; +import { extOf, getAllowedExtensions } from '@/lib/file-filter'; import { SAMPLE_JSON } from '@/lib/sample'; import { isTauri, nativeOpenFiles, fileFromNativePath } from '@/lib/tauri'; import { getConverter } from '@/converters/registry'; @@ -70,15 +71,12 @@ export function InputPanel({ // Allowed extensions for the single-file "Open" / drag-drop path. // `` is only a hint to the OS picker — re-validate in JS. const allowedExtensions = useMemo( - () => (isReverse ? [converter.meta.extension.toLowerCase(), 'txt'] : ['json', 'txt']), + () => getAllowedExtensions(isReverse ? 'reverse' : 'forward', converter.meta.extension), [isReverse, converter.meta.extension], ); const hasAllowedExt = useCallback( - (name: string) => { - const ext = (name.split('.').pop() ?? '').toLowerCase(); - return allowedExtensions.includes(ext); - }, + (name: string) => allowedExtensions.includes(extOf(name)), [allowedExtensions], ); diff --git a/src/lib/__tests__/file-filter.test.ts b/src/lib/__tests__/file-filter.test.ts new file mode 100644 index 0000000..4c1615b --- /dev/null +++ b/src/lib/__tests__/file-filter.test.ts @@ -0,0 +1,54 @@ +import { describe, expect, it } from 'vitest'; +import { extOf, filterByExtension, getAllowedExtensions } from '../file-filter'; + +function f(name: string): File { + return new File(['x'], name); +} + +describe('getAllowedExtensions', () => { + it('returns json + txt for forward direction regardless of format', () => { + expect(getAllowedExtensions('forward', 'yaml')).toEqual(['json', 'txt']); + expect(getAllowedExtensions('forward', 'resx')).toEqual(['json', 'txt']); + }); + + it('returns format extension + txt for reverse direction, lowercased', () => { + expect(getAllowedExtensions('reverse', 'yaml')).toEqual(['yaml', 'txt']); + expect(getAllowedExtensions('reverse', 'RESX')).toEqual(['resx', 'txt']); + expect(getAllowedExtensions('reverse', 'toml')).toEqual(['toml', 'txt']); + }); +}); + +describe('extOf', () => { + it('lowercases the trailing extension', () => { + expect(extOf('foo.JSON')).toBe('json'); + expect(extOf('a.b.cs')).toBe('cs'); + }); + + it('returns empty string for names with no extension', () => { + expect(extOf('noext')).toBe(''); + expect(extOf('trailing.')).toBe(''); + expect(extOf('.hidden')).toBe('hidden'); + }); +}); + +describe('filterByExtension', () => { + it('partitions by extension whitelist (case-insensitive)', () => { + const cs = f('a.cs'); + const json = f('b.json'); + const yaml = f('c.yaml'); + const result = filterByExtension([cs, json, yaml], ['json', 'txt']); + expect(result.valid).toEqual([json]); + expect(result.wrongFormat).toBe(2); + }); + + it('accepts uppercase extensions because whitelist comparison is lowercased', () => { + const upper = f('FOO.JSON'); + const result = filterByExtension([upper], ['json', 'txt']); + expect(result.valid).toEqual([upper]); + expect(result.wrongFormat).toBe(0); + }); + + it('returns empty result for empty input', () => { + expect(filterByExtension([], ['json'])).toEqual({ valid: [], wrongFormat: 0 }); + }); +}); diff --git a/src/lib/file-filter.ts b/src/lib/file-filter.ts new file mode 100644 index 0000000..5273ef8 --- /dev/null +++ b/src/lib/file-filter.ts @@ -0,0 +1,41 @@ +export type FileFilterDirection = 'forward' | 'reverse'; + +/** + * Allowed file extensions for a given direction × format combo. + * Forward: JSON-only input. Reverse: the format's native extension. + * `.txt` is always accepted as a freeform escape hatch. + * + * Returned extensions are lowercase, without the leading dot. + */ +export function getAllowedExtensions( + direction: FileFilterDirection, + formatExtension: string, +): string[] { + if (direction === 'reverse') { + return [formatExtension.toLowerCase(), 'txt']; + } + return ['json', 'txt']; +} + +/** Lowercase extension of a filename, empty string if there's no dot. */ +export function extOf(name: string): string { + const idx = name.lastIndexOf('.'); + if (idx < 0 || idx === name.length - 1) return ''; + return name.slice(idx + 1).toLowerCase(); +} + +export interface FilterByExtensionResult { + valid: File[]; + wrongFormat: number; +} + +/** Split files into (accepted, wrong-format count) by extension whitelist. */ +export function filterByExtension(files: File[], allowed: string[]): FilterByExtensionResult { + const valid: File[] = []; + let wrongFormat = 0; + for (const f of files) { + if (allowed.includes(extOf(f.name))) valid.push(f); + else wrongFormat += 1; + } + return { valid, wrongFormat }; +}