2021-10-28 17:02:07 +01:00
|
|
|
from http import HTTPStatus
|
2022-06-01 14:53:05 +02:00
|
|
|
|
2021-10-28 17:02:07 +01:00
|
|
|
from fastapi.param_functions import Depends
|
|
|
|
from fastapi.params import Query
|
|
|
|
from starlette.exceptions import HTTPException
|
|
|
|
from starlette.requests import Request
|
|
|
|
from starlette.responses import HTMLResponse, RedirectResponse
|
|
|
|
|
|
|
|
from lnbits.core.crud import get_wallet_payment
|
|
|
|
from lnbits.core.models import Payment, User
|
|
|
|
from lnbits.decorators import check_user_exists
|
|
|
|
|
|
|
|
from . import livestream_ext, livestream_renderer
|
|
|
|
from .crud import get_livestream_by_track, get_track
|
|
|
|
|
2022-07-16 14:23:03 +02:00
|
|
|
# from mmap import MAP_DENYWRITE
|
|
|
|
|
2021-10-28 17:02:07 +01:00
|
|
|
|
|
|
|
@livestream_ext.get("/", response_class=HTMLResponse)
|
|
|
|
async def index(request: Request, user: User = Depends(check_user_exists)):
|
2021-11-12 04:14:55 +00:00
|
|
|
return livestream_renderer().TemplateResponse(
|
|
|
|
"livestream/index.html", {"request": request, "user": user.dict()}
|
|
|
|
)
|
2021-10-28 17:02:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
@livestream_ext.get("/track/{track_id}", name="livestream.track_redirect_download")
|
2021-11-10 11:15:33 +00:00
|
|
|
async def track_redirect_download(track_id, p: str = Query(...)):
|
|
|
|
payment_hash = p
|
2021-10-28 17:02:07 +01:00
|
|
|
track = await get_track(track_id)
|
|
|
|
ls = await get_livestream_by_track(track_id)
|
|
|
|
payment: Payment = await get_wallet_payment(ls.wallet, payment_hash)
|
|
|
|
|
|
|
|
if not payment:
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=HTTPStatus.NOT_FOUND,
|
2021-11-12 04:14:55 +00:00
|
|
|
detail=f"Couldn't find the payment {payment_hash} or track {track.id}.",
|
2021-10-28 17:02:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
if payment.pending:
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=HTTPStatus.PAYMENT_REQUIRED,
|
2021-11-12 04:14:55 +00:00
|
|
|
detail=f"Payment {payment_hash} wasn't received yet. Please try again in a minute.",
|
2021-10-28 17:02:07 +01:00
|
|
|
)
|
|
|
|
return RedirectResponse(url=track.download_url)
|