@app.post("/extract", tags=["extract"]) asyncdefextract(file: UploadFile): """Extracts the given ZIP and returns a JSON object containing the contents of every file extracted""" with TemporaryDirectory(dir=UPLOAD_DIR) as tmpdir: file_to_extract = Path(tmpdir) / file.filename withopen(file_to_extract, "wb") as f: whileTrue: data = await file.read(2048) ifnot data: break f.write(data) # make sure the file is a valid zip because Python's zipfile doesn't support symlinks (no hacking!) ifnot is_zipfile(file_to_extract): raise HTTPException(status_code=415, detail=f"The input file must be an ZIP archive.") with TemporaryDirectory(dir=tmpdir) as extract_to_dir: try: extract_archive(str(file_to_extract), outdir=extract_to_dir) except PatoolError as e: raise HTTPException(status_code=400, detail=f"Error extracting ZIP {file_to_extract.name}: {e!s}") return read_files(extract_to_dir)