File size: 1,143 Bytes
0ebef7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""Safe temp handling for uploaded bank statements.

Bank data must never linger. Each upload is written into its OWN temp directory
inside WORK_DIR, processed, and the whole directory is removed in a finally block
— taking the PDF and any OCR scratch images (`.ocr_p*.png`, which the engine
writes next to the PDF) with it. A per-request directory also keeps concurrent
requests from clobbering each other's OCR scratch.
"""
from __future__ import annotations

import contextlib
import shutil
import tempfile
from collections.abc import Iterator
from pathlib import Path

from app.core.config import WORK_DIR


@contextlib.contextmanager
def temp_pdf(data: bytes) -> Iterator[Path]:
    """Write `data` to an isolated temp dir, yield the PDF path, then delete the
    whole dir (PDF + OCR scratch) no matter what."""
    WORK_DIR.mkdir(parents=True, exist_ok=True)
    workdir = Path(tempfile.mkdtemp(dir=str(WORK_DIR)))
    pdf_path = workdir / "statement.pdf"
    try:
        pdf_path.write_bytes(data)
        yield pdf_path
    finally:
        with contextlib.suppress(OSError):
            shutil.rmtree(workdir, ignore_errors=True)