36 lines
1.1 KiB
Python
36 lines
1.1 KiB
Python
from datetime import datetime, timezone
|
|
from pathlib import Path
|
|
|
|
from app.modules.editor.domain.entities import EditableFile
|
|
from app.shared.infrastructure.filesystem import read_text, write_text
|
|
|
|
|
|
def detect_format(file_path: Path) -> str:
|
|
suffix = file_path.suffix.lower()
|
|
if suffix == ".csv":
|
|
return "csv"
|
|
elif suffix == ".md":
|
|
return "md"
|
|
elif suffix in (".yaml", ".yml"):
|
|
if "openapi" in file_path.name or "api-contracts" in file_path.name:
|
|
return "openapi"
|
|
return "yaml"
|
|
return "unknown"
|
|
|
|
|
|
def read_file(base_dir: Path, relative_path: str) -> EditableFile:
|
|
full_path = base_dir / relative_path
|
|
content = read_text(full_path)
|
|
stat = full_path.stat()
|
|
return EditableFile(
|
|
path=relative_path,
|
|
format=detect_format(full_path),
|
|
content=content,
|
|
last_modified=datetime.fromtimestamp(stat.st_mtime, tz=timezone.utc),
|
|
)
|
|
|
|
|
|
def write_file(base_dir: Path, relative_path: str, content: str) -> None:
|
|
full_path = base_dir / relative_path
|
|
write_text(full_path, content)
|