export function addFileWithDedup(state, file, cfg) { const originalName = file.name; const existing = state.fileMeta.get(originalName); let name = originalName; if (existing) { if (existing.size === file.size) return false; const dot = originalName.lastIndexOf('.'); const base = dot > 0 ? originalName.slice(0, dot) : originalName; const extension = dot > 0 ? originalName.slice(dot) : ''; let suffix = 2; while (state.fileMeta.has(`${base}_${suffix}${extension}`)) suffix += 1; name = `${base}_${suffix}${extension}`; } const storedFile = new File([file], name, { lastModified: file.lastModified }); state.fileMeta.set(name, { size: storedFile.size }); const includedBytes = state.files.reduce( (total, item) => total + (state.overNames.has(item.name) ? 0 : item.size), 0, ); if (storedFile.size > cfg.maxFileBytes || includedBytes + storedFile.size > cfg.maxSessionBytes) { state.overNames.add(name); } state.files.push(storedFile); return true; }