ruhs2 / Systumm /utils /render_template.py
ruhscps's picture
Upload 9 files
73c7f48 verified
Raw History Blame Contribute Delete
1.1 kB
import aiohttp
import jinja2
import urllib.parse
from Systumm.config import Telegram, Server
from Systumm.utils.database import Database
from Systumm.utils.human_readable import humanbytes
from Systumm.server.exceptions import FIleNotFound
db = Database(Telegram.DATABASE_URL, Telegram.SESSION_NAME)
async def render_page(db_id):
file_data = await db.get_file(db_id)
if not file_data:
# Raising this allows stream_routes.py to catch it and show a proper 404
raise FIleNotFound("File not found in database")
src = urllib.parse.urljoin(Server.URL, f'dl/{file_data["_id"]}')
file_size = humanbytes(file_data['file_size'])
file_name = file_data['file_name'].replace("_", " ")
if str((file_data['mime_type']).split('/')[0].strip()) == 'video':
template_file = "Systumm/template/play.html"
else:
template_file = "Systumm/template/dl.html"
with open(template_file) as f:
template = jinja2.Template(f.read())
return template.render(
file_name=file_name,
file_url=src,
file_size=file_size
)