-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
73 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,42 @@ | ||
from fastapi import APIRouter, Request | ||
import typing | ||
|
||
from fastapi import APIRouter, Depends, Request | ||
from fastapi.responses import HTMLResponse | ||
from fastapi.templating import Jinja2Templates | ||
from sqlalchemy import desc | ||
from sqlalchemy.orm import selectinload | ||
from sqlmodel import select | ||
from sqlmodel.ext.asyncio.session import AsyncSession | ||
|
||
from app.api.dependencies import yield_db_session | ||
from app.models.klepp import Video | ||
|
||
if typing.TYPE_CHECKING: | ||
from starlette.templating import _TemplateResponse | ||
|
||
templates = Jinja2Templates(directory='templates') | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.get('/', response_class=HTMLResponse, include_in_schema=False) | ||
async def render_video_page(request: Request, path: str): | ||
return templates.TemplateResponse('video.html', {'request': request, 'path': path}) | ||
async def render_video_page( | ||
request: Request, path: str, session: AsyncSession = Depends(yield_db_session) | ||
) -> '_TemplateResponse': | ||
""" | ||
Static site for share.klepp.me?path=<path> | ||
""" | ||
video_statement = ( | ||
select(Video) | ||
.options(selectinload(Video.user)) | ||
.options(selectinload(Video.tags)) | ||
.options(selectinload(Video.likes)) | ||
.order_by(desc(Video.uploaded_at)) | ||
) | ||
if path: | ||
# Short route, specific path requested. This cannot be a `files/{path}` API due to `/` in video paths. | ||
video_statement = video_statement.where(Video.path == path) | ||
video_response = await session.exec(video_statement) # type: ignore | ||
if found := video_response.one_or_none(): | ||
return templates.TemplateResponse('video.html', {'request': request, 'video_dict': found.dict()}) | ||
return templates.TemplateResponse('404.html', {'request': request}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta property="og:title" content="Klepp.me"/> | ||
<meta property="og:type" content="website"/> | ||
<meta property="og:image" content="https://klepp.me/favicon.ico"/> | ||
<title>Klepp 404</title> | ||
<style> | ||
body {background: linear-gradient(90deg,#0f2027,#36474f 50%,#2c5364); color: white} | ||
</style> | ||
</head> | ||
<body> | ||
<div class="404"> | ||
404 video not found | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters