Rendering code refactor
This commit is contained in:
@@ -3,8 +3,10 @@ import os
|
||||
import re
|
||||
import uuid
|
||||
from collections.abc import Iterator
|
||||
from contextlib import asynccontextmanager
|
||||
from pathlib import Path
|
||||
|
||||
import aiofiles
|
||||
import magic
|
||||
from fastapi import HTTPException, UploadFile
|
||||
from folkugat_web.config import db
|
||||
@@ -57,8 +59,8 @@ async def store_file(tema_id: int, upload_file: UploadFile) -> str:
|
||||
|
||||
def create_tema_filename(tema_id: int, extension: str = "") -> Path:
|
||||
filename = str(uuid.uuid4().hex) + extension
|
||||
filedir = db.DB_FILES_DIR / str(tema_id)
|
||||
filedir.mkdir(exist_ok=True)
|
||||
filedir = db.DB_FILES_DIR / "tema" / str(tema_id)
|
||||
filedir.mkdir(parents=True, exist_ok=True)
|
||||
filepath = filedir / filename
|
||||
return filepath
|
||||
|
||||
@@ -71,6 +73,18 @@ def create_tmp_filename(extension: str = "") -> Path:
|
||||
return filepath
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def tmp_file(content: str):
|
||||
input_filename = create_tmp_filename(extension=".ly")
|
||||
async with aiofiles.open(input_filename, "w") as f:
|
||||
_ = await f.write(content)
|
||||
try:
|
||||
yield input_filename
|
||||
finally:
|
||||
if input_filename.exists():
|
||||
os.remove(input_filename)
|
||||
|
||||
|
||||
def list_files(tema_id: str) -> list[str]:
|
||||
filedir = db.DB_FILES_DIR / str(tema_id)
|
||||
return [get_db_file_path(f) for f in filedir.iterdir()]
|
||||
|
||||
Reference in New Issue
Block a user