| 1 | import os |
| 2 | from helpers.api import ApiHandler, Input, Output, Request, Response |
| 3 | from helpers import files, runtime |
| 4 | from typing import TypedDict |
| 5 | |
| 6 | class FileInfoApi(ApiHandler): |
| 7 | async def process(self, input: Input, request: Request) -> Output: |
| 8 | path = input.get("path", "") |
| 9 | info = await runtime.call_development_function(get_file_info, path) |
| 10 | return info |
| 11 | |
| 12 | class FileInfo(TypedDict): |
| 13 | input_path: str |
| 14 | abs_path: str |
| 15 | exists: bool |
| 16 | is_dir: bool |
| 17 | is_file: bool |
| 18 | is_link: bool |
| 19 | size: int |
| 20 | modified: float |
| 21 | created: float |
| 22 | permissions: int |
| 23 | dir_path: str |
| 24 | file_name: str |
| 25 | file_ext: str |
| 26 | message: str |
| 27 | |
| 28 | async def get_file_info(path: str) -> FileInfo: |
| 29 | abs_path = files.get_abs_path(path) |
| 30 | exists = os.path.exists(abs_path) |
| 31 | message = "" |
| 32 | |
| 33 | if not exists: |
| 34 | message = f"File {path} not found." |
| 35 | |
| 36 | return { |
| 37 | "input_path": path, |
| 38 | "abs_path": abs_path, |
| 39 | "exists": exists, |
| 40 | "is_dir": os.path.isdir(abs_path) if exists else False, |
| 41 | "is_file": os.path.isfile(abs_path) if exists else False, |
| 42 | "is_link": os.path.islink(abs_path) if exists else False, |
| 43 | "size": os.path.getsize(abs_path) if exists else 0, |
| 44 | "modified": os.path.getmtime(abs_path) if exists else 0, |
| 45 | "created": os.path.getctime(abs_path) if exists else 0, |
| 46 | "permissions": os.stat(abs_path).st_mode if exists else 0, |
| 47 | "dir_path": os.path.dirname(abs_path), |
| 48 | "file_name": os.path.basename(abs_path), |
| 49 | "file_ext": os.path.splitext(abs_path)[1], |
| 50 | "message": message |
| 51 | } |