diff --git a/backend/converter.py b/backend/converter.py index c4e3eb4..070ba1d 100644 --- a/backend/converter.py +++ b/backend/converter.py @@ -77,6 +77,11 @@ def extract_if_needed(filename: str) -> Path: if not shutil.which(tool): raise RuntimeError(f"Extraction tool '{tool}' not found on host") + # Determine extraction base: use the file's parent directory + # (handles both tmp/in/ and tmp/{guid}/in/ paths) + extract_base = filepath.parent + extract_base.mkdir(parents=True, exist_ok=True) + # Directory stem: strip extension(s) stem = name for e in sorted(tool_map, key=len, reverse=True): @@ -85,7 +90,7 @@ def extract_if_needed(filename: str) -> Path: break # If already extracted, skip re-extraction - expected_dir = STAGING_IN / stem + expected_dir = extract_base / stem if expected_dir.is_dir() and any( f.suffix.lower() in DISK_EXTENSIONS for f in expected_dir.rglob("*") @@ -94,12 +99,11 @@ def extract_if_needed(filename: str) -> Path: logger.info("Already extracted: %s", expected_dir) return expected_dir - # Snapshot STAGING_IN before extraction - before = set(STAGING_IN.iterdir()) if STAGING_IN.exists() else set() + # Snapshot before extraction + before = set(extract_base.iterdir()) if extract_base.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) + # Extract into the file's parent directory + result = subprocess.run(cmd, cwd=str(extract_base), capture_output=True, text=True, timeout=600) if result.returncode != 0: raise RuntimeError(f"{tool} failed: {result.stderr.strip()[:500]}") @@ -108,12 +112,11 @@ def extract_if_needed(filename: str) -> Path: new_items = after - before # If the archive had a single top-level directory matching the stem, use it - expected_dir = STAGING_IN / stem + expected_dir = extract_base / stem if expected_dir in new_items and expected_dir.is_dir(): result = expected_dir else: - # Collect scattered files into a subdirectory - out_dir = STAGING_IN / stem + out_dir = extract_base / stem out_dir.mkdir(parents=True, exist_ok=True) for item in new_items: shutil.move(str(item), str(out_dir / item.name))