From 8a27c5c9695abe56d9e2782b5cf547cba8cb4485 Mon Sep 17 00:00:00 2001 From: poli0981 <127664709+poli0981@users.noreply.github.com> Date: Wed, 13 May 2026 20:33:12 +0700 Subject: [PATCH] fix(tauri): set File.lastModified from stat() so batch dedup works (v1.3.3) (#27) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `fileFromNativePath` was calling `new File([text], name, { type })` without `lastModified`. The File API defaults `lastModified` to `Date.now()` at construction time, so reading the same path twice produced two File objects with different timestamps — and `batchStore` dedups on `(name, size, lastModified)`, so dropping the same file twice through Tauri native drag-drop / native picker added both copies to the queue with no `duplicate` toast. Web wasn't affected because `` and `react-dropzone` build File objects from the OS mtime. Fix: call `stat()` from `@tauri-apps/plugin-fs` and pass `mtime.getTime()` into the File constructor. Fall back to an FNV-1a hash of the path if stat throws (capability missing, file vanished between read and stat) so repeat drops still dedup. Also infer `type` from the extension instead of hard-coding `application/json`. Add `fs:allow-stat` to the default capability. 3 new unit tests for `pathHashAsMillis`. Total: 193. Co-authored-by: Claude Opus 4.7 (1M context) --- CHANGELOG.md | 10 ++++++ package-lock.json | 4 +-- package.json | 2 +- src-tauri/Cargo.toml | 2 +- src-tauri/capabilities/default.json | 1 + src-tauri/tauri.conf.json | 2 +- src/lib/__tests__/tauri.test.ts | 22 +++++++++++++ src/lib/tauri.ts | 50 ++++++++++++++++++++++++++++- 8 files changed, 87 insertions(+), 6 deletions(-) create mode 100644 src/lib/__tests__/tauri.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index ce2d980..3dc3d3b 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.3] — 2026-05-13 + +### Fixed + +- **Tauri native drag-drop / file picker bỏ qua batch dedup**. v1.3.0–v1.3.2 desktop bundles cho phép drop cùng một file vào queue nhiều lần mà không hiển thị toast `duplicate`. Root cause: `fileFromNativePath` tạo `new File([text], name, { type })` không truyền `lastModified`, nên File API default về `Date.now()` mỗi lần gọi; key dedup của `batchStore` (`name|size|lastModified`) khác nhau giữa các lần drop cùng một file. Sửa: stat file qua `@tauri-apps/plugin-fs` để lấy mtime thật, fall back về hash FNV-1a của path nếu stat fail. Bonus: infer `type` từ extension thay vì hard-code `application/json`. Thêm permission `fs:allow-stat` vào default capability. + +### Tests + +- 3 unit test mới cho `pathHashAsMillis` trong `src/lib/__tests__/tauri.test.ts`. Total tests: **193**. + ## [1.3.2] — 2026-05-13 ### Fixed diff --git a/package-lock.json b/package-lock.json index fd5bbd3..8ac80a7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "jsonprism", - "version": "1.3.2", + "version": "1.3.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "jsonprism", - "version": "1.3.2", + "version": "1.3.3", "license": "Apache-2.0", "dependencies": { "@codemirror/lang-json": "^6.0.0", diff --git a/package.json b/package.json index fcf16f7..c0ccfc2 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "jsonprism", "private": true, - "version": "1.3.2", + "version": "1.3.3", "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 43f18f7..1b6b1df 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "jsonprism" -version = "1.3.2" +version = "1.3.3" description = "Disperse JSON into many formats." authors = ["poli0981 "] license = "Apache-2.0" diff --git a/src-tauri/capabilities/default.json b/src-tauri/capabilities/default.json index 74223ac..0bc4290 100644 --- a/src-tauri/capabilities/default.json +++ b/src-tauri/capabilities/default.json @@ -20,6 +20,7 @@ "fs:allow-read-text-file", "fs:allow-write-text-file", "fs:allow-write-file", + "fs:allow-stat", { "identifier": "fs:scope", "allow": ["$HOME/**", "$DOCUMENT/**", "$DOWNLOAD/**", "$DESKTOP/**"] diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index d7941aa..ed26b66 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.2", + "version": "1.3.3", "identifier": "com.poli0981.jsonprism", "build": { "beforeDevCommand": "npm run dev", diff --git a/src/lib/__tests__/tauri.test.ts b/src/lib/__tests__/tauri.test.ts new file mode 100644 index 0000000..f73b24c --- /dev/null +++ b/src/lib/__tests__/tauri.test.ts @@ -0,0 +1,22 @@ +import { describe, expect, it } from 'vitest'; +import { pathHashAsMillis } from '../tauri'; + +describe('pathHashAsMillis', () => { + it('is deterministic for the same input', () => { + const a = pathHashAsMillis('C:/Users/me/data.json'); + const b = pathHashAsMillis('C:/Users/me/data.json'); + expect(a).toBe(b); + }); + + it('produces different hashes for different paths', () => { + expect(pathHashAsMillis('/a/foo.json')).not.toBe(pathHashAsMillis('/a/bar.json')); + expect(pathHashAsMillis('/a/foo.json')).not.toBe(pathHashAsMillis('/b/foo.json')); + }); + + it('returns a non-negative integer (unsigned 32-bit) even for empty input', () => { + const h = pathHashAsMillis(''); + expect(Number.isInteger(h)).toBe(true); + expect(h).toBeGreaterThanOrEqual(0); + expect(h).toBeLessThanOrEqual(0xffffffff); + }); +}); diff --git a/src/lib/tauri.ts b/src/lib/tauri.ts index 0d4b083..5035a54 100644 --- a/src/lib/tauri.ts +++ b/src/lib/tauri.ts @@ -70,14 +70,62 @@ export async function nativeReadText(path: string): Promise { return readTextFile(path); } +// MIME type per extension. Used so File objects created from native paths +// carry a sensible `type` instead of being hard-coded to `application/json`. +const TYPE_BY_EXT: Record = { + json: 'application/json', + yaml: 'application/x-yaml', + yml: 'application/x-yaml', + toml: 'application/toml', + xml: 'application/xml', + csv: 'text/csv', + tsv: 'text/tab-separated-values', + resx: 'application/xml', + txt: 'text/plain', +}; + +/** + * FNV-1a 32-bit hash of a path, returned as an unsigned 32-bit integer. + * Used as a stable `lastModified` for File objects when `stat()` is + * unavailable — `batchStore` dedups on `(name, size, lastModified)`, + * so the value must be deterministic across repeated drops of the same path. + */ +export function pathHashAsMillis(path: string): number { + let h = 0x811c9dc5; + for (let i = 0; i < path.length; i += 1) { + h ^= path.charCodeAt(i); + h = Math.imul(h, 0x01000193); + } + return h >>> 0; +} + /** * Convert a native file path to a browser-style File so the * existing batch store and editor flows can consume it unchanged. + * + * `lastModified` is read from the file's real mtime via `stat()` so that + * re-dropping the same file dedups correctly (batchStore keys on + * name|size|lastModified). Falling back to `Date.now()` would break dedup + * because every drop produces a fresh timestamp. */ export async function fileFromNativePath(path: string): Promise { const text = await nativeReadText(path); const name = path.replace(/^.*[\\/]/, ''); // basename - return new File([text], name, { type: 'application/json' }); + const ext = (name.split('.').pop() ?? '').toLowerCase(); + const type = TYPE_BY_EXT[ext] ?? 'application/octet-stream'; + + let lastModified: number; + try { + const { stat } = await import('@tauri-apps/plugin-fs'); + const info = await stat(path); + lastModified = info.mtime ? info.mtime.getTime() : pathHashAsMillis(path); + } catch { + // Capability missing or file vanished between read + stat — fall back + // to a deterministic hash so repeat drops still dedup. + lastModified = pathHashAsMillis(path); + } + + return new File([text], name, { type, lastModified }); } /**