diff --git a/backend/converter.py b/backend/converter.py index 3ec0e78..2a5b1f1 100644 --- a/backend/converter.py +++ b/backend/converter.py @@ -75,13 +75,34 @@ def extract_if_needed(filename: str) -> Path: if stem.endswith(e): stem = stem[:-len(e)] break - out_dir = STAGING_IN / stem - out_dir.mkdir(parents=True, exist_ok=True) - result = subprocess.run(cmd, cwd=str(out_dir), capture_output=True, text=True, timeout=600) + # Snapshot STAGING_IN before extraction + before = set(STAGING_IN.iterdir()) if STAGING_IN.exists() else set() + + # Extract into STAGING_IN root (not a subdir) to avoid double nesting + # when the archive already has a top-level directory + result = subprocess.run(cmd, cwd=str(STAGING_IN), capture_output=True, text=True, timeout=600) if result.returncode != 0: raise RuntimeError(f"{tool} failed: {result.stderr.strip()[:500]}") + # Determine what was created + after = set(STAGING_IN.iterdir()) + new_items = after - before + + # If the archive had a single top-level directory matching the stem, use it + expected_dir = STAGING_IN / stem + if expected_dir in new_items and expected_dir.is_dir(): + return expected_dir + + # Otherwise, collect scattered files into a subdirectory + out_dir = STAGING_IN / stem + out_dir.mkdir(parents=True, exist_ok=True) + for item in new_items: + dest = out_dir / item.name + if item.is_dir(): + shutil.move(str(item), str(dest)) + else: + shutil.move(str(item), str(dest)) return out_dir