Move help-file rescan off Home into a dedicated Сканиране tab with separate file and folder pickers.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -136,46 +136,82 @@ def os_access_writable(path: Path) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
def _unique_dest(dest_dir: Path, name: str) -> Path:
|
||||
target = dest_dir / name
|
||||
if not target.exists():
|
||||
return target
|
||||
stem, suffix = Path(name).stem, Path(name).suffix
|
||||
i = 2
|
||||
while True:
|
||||
cand = dest_dir / f"{stem}_{i}{suffix}"
|
||||
if not cand.exists():
|
||||
return cand
|
||||
i += 1
|
||||
|
||||
|
||||
async def _stage_uploads(uploads: list[UploadFile], input_dir: Path) -> tuple[int, str]:
|
||||
"""Write one or more uploads into input_dir. Returns (file_count, label)."""
|
||||
saved = 0
|
||||
total = 0
|
||||
labels: list[str] = []
|
||||
max_bytes = 120 * 1024 * 1024
|
||||
|
||||
for up in uploads:
|
||||
name = (up.filename or "").strip()
|
||||
if not name:
|
||||
continue
|
||||
base = Path(name.replace("\\", "/")).name
|
||||
if not base or base.startswith("."):
|
||||
continue
|
||||
ext = Path(base).suffix.lower()
|
||||
if ext not in ALLOWED_EXT:
|
||||
continue
|
||||
|
||||
data = await up.read()
|
||||
if not data:
|
||||
continue
|
||||
total += len(data)
|
||||
if total > max_bytes:
|
||||
raise HTTPException(400, "Upload too large (max 120 MB)")
|
||||
|
||||
if ext == ".zip":
|
||||
n = _safe_extract_zip(data, input_dir)
|
||||
saved += n
|
||||
else:
|
||||
_unique_dest(input_dir, base).write_bytes(data)
|
||||
saved += 1
|
||||
labels.append(base)
|
||||
|
||||
if saved == 0:
|
||||
raise HTTPException(
|
||||
400,
|
||||
f"No usable files. Allowed: {sorted(ALLOWED_EXT)}",
|
||||
)
|
||||
label = labels[0] if len(labels) == 1 else f"{len(labels)} files"
|
||||
return saved, label
|
||||
|
||||
|
||||
@router.post("/rescan")
|
||||
async def rescan(
|
||||
background_tasks: BackgroundTasks,
|
||||
file: UploadFile = File(..., description="ZIP or single help file"),
|
||||
file: list[UploadFile] = File(..., description="ZIP, one help file, or several files from a folder"),
|
||||
prefix: str = Form("RIP"),
|
||||
force: str = Form("false"),
|
||||
x_rescan_token: Optional[str] = Header(None, alias="X-Rescan-Token"),
|
||||
token: Optional[str] = Form(None),
|
||||
):
|
||||
"""
|
||||
Upload a .zip (or one .docx/.html/.pdf/.txt/.doc), process on server,
|
||||
write sections to OUTPUT_DIR and Postgres.
|
||||
Upload a .zip, one .docx/.html/.pdf/.txt/.doc, or several such files
|
||||
(folder pick). Process on server, write sections to OUTPUT_DIR and Postgres.
|
||||
"""
|
||||
_check_token(x_rescan_token or token)
|
||||
|
||||
name = (file.filename or "upload.bin").strip()
|
||||
ext = Path(name).suffix.lower()
|
||||
if ext not in ALLOWED_EXT:
|
||||
raise HTTPException(
|
||||
400,
|
||||
f"Unsupported type {ext}. Allowed: {sorted(ALLOWED_EXT)}",
|
||||
)
|
||||
|
||||
data = await file.read()
|
||||
if not data:
|
||||
raise HTTPException(400, "Empty upload")
|
||||
if len(data) > 120 * 1024 * 1024:
|
||||
raise HTTPException(400, "Upload too large (max 120 MB)")
|
||||
|
||||
staging = Path(tempfile.mkdtemp(prefix="rip_rescan_"))
|
||||
input_dir = staging / "input"
|
||||
input_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
try:
|
||||
if ext == ".zip":
|
||||
n = _safe_extract_zip(data, input_dir)
|
||||
if n == 0:
|
||||
raise HTTPException(400, "ZIP has no usable files")
|
||||
else:
|
||||
(input_dir / Path(name).name).write_bytes(data)
|
||||
_count, label = await _stage_uploads(file, input_dir)
|
||||
except HTTPException:
|
||||
shutil.rmtree(staging, ignore_errors=True)
|
||||
raise
|
||||
@@ -189,7 +225,7 @@ async def rescan(
|
||||
job_id,
|
||||
status="queued",
|
||||
message="accepted",
|
||||
filename=name,
|
||||
filename=label,
|
||||
prefix=prefix,
|
||||
force=force_flag,
|
||||
created_at=datetime.now(timezone.utc).isoformat(),
|
||||
|
||||
Reference in New Issue
Block a user