Skip to content

Commit

Permalink
Add previous and next buttons to video player
Browse files Browse the repository at this point in the history
  • Loading branch information
dormant-user committed Dec 16, 2023
1 parent 3a5a18a commit eed7845
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
43 changes: 42 additions & 1 deletion pystream/models/squire.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import pathlib
import re
from typing import Dict, List, Union
from typing import Dict, List, Union, Tuple

from fastapi import Request

Expand Down Expand Up @@ -83,6 +83,47 @@ def get_all_stream_content() -> Dict[str, List[Dict[str, str]]]:
return structure


def get_iter(filename: pathlib.PurePath) -> Union[Tuple[str, str], Tuple[None, None]]:
"""Sort video files at the currently served file's directory, and return the previous and next filenames.
Args:
filename: Path to the video file currently rendered.
Returns:
Tuple[str, str]:
Tuple of previous file and next file.
"""
dir_content = sorted(os.listdir(filename.parent), key=lambda x: natural_sort_key(x))
for idx, file in enumerate(dir_content):
if file == filename.name:
try:
previous_ = dir_content[idx - 1]
except IndexError:
previous_ = None
try:
next_ = dir_content[idx + 1]
except IndexError:
next_ = None
if pathlib.PosixPath(previous_).suffix not in config.env.file_formats:
previous_ = None
if pathlib.PosixPath(next_).suffix not in config.env.file_formats:
next_ = None
return previous_, next_
return None, None


def scanner() -> None:
"""Update the shared static object member to response from stream all content."""
config.static.landing_page = get_all_stream_content()


def remove_thumbnail(img_path: pathlib.PosixPath) -> None:
"""Triggered by timer to remove the thumbnail file.
Args:
img_path: PosixPath to the thumbnail image.
"""
if img_path.exists():
os.remove(img_path)
else:
logger.warning("%s was removed before hand." % img_path)
8 changes: 5 additions & 3 deletions pystream/routers/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ async def preview_loader(request: Request, img_path: str,
"""
await authenticator.verify(credentials)
squire.log_connection(request)
img_path = html.unescape(img_path)
if pathlib.PosixPath(img_path).exists():
img_path = pathlib.PosixPath(html.unescape(img_path))
if img_path.exists():
# Image is required only for caching in the browser, so it is not required after it has been rendered
threading.Timer(interval=5, function=os.remove, args=(img_path,)).start()
threading.Timer(interval=5, function=squire.remove_thumbnail, args=(img_path,)).start()
return FileResponse(img_path)
logger.critical("'%s' not found", img_path)
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"{img_path!r} NOT FOUND")
Expand Down Expand Up @@ -74,9 +74,11 @@ async def stream_video(request: Request,
}
)
if pure_path.exists():
prev_, next_ = squire.get_iter(pure_path)
attrs = {
"request": request, "title": video_path,
"path": f"{config.static.streaming_endpoint}?{config.static.query_param}={urlparse.quote(str(pure_path))}",
"previous": prev_ or "#", "next": next_ or "#"
}
# set default to avoid broken image sign in thumbnail
preview_src = os.path.join(pathlib.PurePath(__file__).parent, "blank.jpg")
Expand Down
21 changes: 21 additions & 0 deletions pystream/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,25 @@
display: block
}
</style>
<style>
a {
text-decoration: none;
display: inline-block;
padding: 8px 16px;
}
a:hover {
background-color: #ddd;
color: black;
}
.previous {
background-color: #f1f1f1;
color: black;
}
.next {
background-color: #f1f1f1;
color: black;
}
</style>
</head>
<body>
<button class="home" onclick="goHome()"><i class="fa fa-home"></i> Home</button>
Expand Down Expand Up @@ -123,6 +142,8 @@ <h1>{{title}}</h1>
<a href="https://videojs.com/html5-video-support/" target="_blank">supports HTML5 video</a>
</p>
</video>
<a style="float: left" href="{{ previous }}" title="{{ previous }}" class="previous">&laquo; Previous</a>
<a style="float: right" href="{{ next }}" title="{{ next }}" class="next">Next &raquo;</a>
<br><br>
</div>
<script>
Expand Down

0 comments on commit eed7845

Please sign in to comment.