fix: extraction uses file's parent dir as base, not hardcoded STAGING_IN
extract_if_needed now extracts relative to the source file's parent
directory instead of /mnt/converter/tmp/in/. This correctly handles
files in tmp/{guid}/in/ paths.
This commit is contained in:
parent
7a024ca166
commit
1b145adea7
1 changed files with 12 additions and 9 deletions
|
|
@ -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))
|
||||
|
|
|
|||
Loading…
Reference in a new issue