Replace tuple with dataclass
This commit is contained in:
@ -14,13 +14,7 @@ from urllib.parse import urljoin
|
|||||||
from discord_embed import settings
|
from discord_embed import settings
|
||||||
|
|
||||||
|
|
||||||
def generate_html_for_videos(
|
def generate_html_for_videos(url: str, width: int, height: int, screenshot: str, filename: str) -> str:
|
||||||
url: str,
|
|
||||||
width: int,
|
|
||||||
height: int,
|
|
||||||
screenshot: str,
|
|
||||||
filename: str,
|
|
||||||
) -> str:
|
|
||||||
"""Generate HTML for video files.
|
"""Generate HTML for video files.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
@ -45,7 +45,6 @@ async def upload_file(file: UploadFile = File(...)) -> Dict[str, str]:
|
|||||||
Returns:
|
Returns:
|
||||||
Returns a dict with the filename, or a link to the .html if it was a video.
|
Returns a dict with the filename, or a link to the .html if it was a video.
|
||||||
"""
|
"""
|
||||||
domain_url = ""
|
|
||||||
if file.content_type.startswith("video/"):
|
if file.content_type.startswith("video/"):
|
||||||
return await do_things(file)
|
return await do_things(file)
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
"""Things that has to do with video file uploading."""
|
"""Things that has to do with video file uploading."""
|
||||||
import os
|
import os
|
||||||
|
from dataclasses import dataclass
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
|
|
||||||
@ -11,30 +12,41 @@ from discord_embed.video import make_thumbnail, video_resolution
|
|||||||
from discord_embed.webhook import send_webhook
|
from discord_embed.webhook import send_webhook
|
||||||
|
|
||||||
|
|
||||||
def save_to_disk(file: UploadFile) -> tuple[str, str]:
|
@dataclass
|
||||||
"""Save file to disk.
|
class VideoFile:
|
||||||
|
"""A video file.
|
||||||
|
|
||||||
If spaces in filename, replace with dots.
|
filename: The filename of the video file.
|
||||||
|
location: The location of the video file.
|
||||||
|
"""
|
||||||
|
filename: str
|
||||||
|
location: str
|
||||||
|
|
||||||
|
|
||||||
|
def save_to_disk(file: UploadFile) -> VideoFile:
|
||||||
|
"""Save the uploaded file to disk.
|
||||||
|
|
||||||
|
If spaces in the filename, replace with dots.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
file: Our uploaded file.
|
file: Our uploaded file.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Returns filename and file location.
|
VideoFile object with filename and location.
|
||||||
"""
|
"""
|
||||||
# Create folder if it doesn't exist.
|
# Create the folder where we should save the files
|
||||||
folder_video = os.path.join(settings.upload_folder, "video")
|
folder_video = os.path.join(settings.upload_folder, "video")
|
||||||
Path(folder_video).mkdir(parents=True, exist_ok=True)
|
Path(folder_video).mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
# Replace spaces with dots in filename.
|
# Replace spaces with dots in the filename.
|
||||||
filename = file.filename.replace(" ", ".")
|
filename = file.filename.replace(" ", ".")
|
||||||
|
|
||||||
# Save file to disk.
|
# Save the uploaded file to disk.
|
||||||
file_location = os.path.join(folder_video, filename)
|
file_location = os.path.join(folder_video, filename)
|
||||||
with open(file_location, "wb+") as f:
|
with open(file_location, "wb+") as f:
|
||||||
f.write(file.file.read())
|
f.write(file.file.read())
|
||||||
|
|
||||||
return filename, file_location
|
return VideoFile(filename, file_location)
|
||||||
|
|
||||||
|
|
||||||
async def do_things(file: UploadFile) -> Dict[str, str]:
|
async def do_things(file: UploadFile) -> Dict[str, str]:
|
||||||
@ -46,17 +58,18 @@ async def do_things(file: UploadFile) -> Dict[str, str]:
|
|||||||
Returns:
|
Returns:
|
||||||
Returns URL for video.
|
Returns URL for video.
|
||||||
"""
|
"""
|
||||||
filename, file_location = save_to_disk(file)
|
|
||||||
|
|
||||||
file_url = f"{settings.serve_domain}/video/{filename}"
|
video_file: VideoFile = save_to_disk(file)
|
||||||
height, width = video_resolution(file_location)
|
|
||||||
screenshot_url = make_thumbnail(file_location, filename)
|
file_url = f"{settings.serve_domain}/video/{video_file.filename}"
|
||||||
|
height, width = video_resolution(video_file.location)
|
||||||
|
screenshot_url = make_thumbnail(video_file.location, video_file.filename)
|
||||||
html_url = generate_html_for_videos(
|
html_url = generate_html_for_videos(
|
||||||
url=file_url,
|
url=file_url,
|
||||||
width=width,
|
width=width,
|
||||||
height=height,
|
height=height,
|
||||||
screenshot=screenshot_url,
|
screenshot=screenshot_url,
|
||||||
filename=filename,
|
filename=video_file.filename,
|
||||||
)
|
)
|
||||||
send_webhook(f"{settings.serve_domain}/{filename} was uploaded.")
|
send_webhook(f"{settings.serve_domain}/{video_file.filename} was uploaded.")
|
||||||
return {"html_url": f"{html_url}"}
|
return {"html_url": f"{html_url}"}
|
||||||
|
Reference in New Issue
Block a user