Get file information
(file_path: Path)
| 31 | |
| 32 | |
| 33 | def get_file_info(file_path: Path) -> dict: |
| 34 | """Get file information""" |
| 35 | stat = file_path.stat() |
| 36 | record_count = None |
| 37 | |
| 38 | # Try to get record count |
| 39 | try: |
| 40 | if file_path.suffix == ".json": |
| 41 | with open(file_path, "r", encoding="utf-8") as f: |
| 42 | data = json.load(f) |
| 43 | if isinstance(data, list): |
| 44 | record_count = len(data) |
| 45 | elif file_path.suffix == ".csv": |
| 46 | with open(file_path, "r", encoding="utf-8") as f: |
| 47 | record_count = sum(1 for _ in f) - 1 # Subtract header row |
| 48 | except Exception: |
| 49 | pass |
| 50 | |
| 51 | return { |
| 52 | "name": file_path.name, |
| 53 | "path": str(file_path.relative_to(DATA_DIR)), |
| 54 | "size": stat.st_size, |
| 55 | "modified_at": stat.st_mtime, |
| 56 | "record_count": record_count, |
| 57 | "type": file_path.suffix[1:] if file_path.suffix else "unknown" |
| 58 | } |
| 59 | |
| 60 | |
| 61 | @router.get("/files") |
no test coverage detected