Only create HTML for video, otherwise return file immediately

This commit is contained in:
2021-12-05 00:11:56 +01:00
parent f9478267bb
commit 96cfc914ad

74
main.py
View File

@ -36,7 +36,7 @@ try:
except KeyError: except KeyError:
sys.exit("discord-embed: Environment variable 'DOMAIN' is missing!") sys.exit("discord-embed: Environment variable 'DOMAIN' is missing!")
# Remove trailing slash from domain. # We check if the domain ends with a forward slash. If it does, we remove it.
if domain.endswith("/"): if domain.endswith("/"):
domain = domain[:-1] domain = domain[:-1]
@ -46,7 +46,7 @@ try:
except KeyError: except KeyError:
sys.exit("discord-embed: Environment variable 'UPLOAD_FOLDER' is missing!") sys.exit("discord-embed: Environment variable 'UPLOAD_FOLDER' is missing!")
# Remove trailing slash from path. # We check if the upload folder ends with a forward slash. If it does, we remove it.
if upload_folder.endswith("/"): if upload_folder.endswith("/"):
upload_folder = upload_folder[:-1] upload_folder = upload_folder[:-1]
@ -61,54 +61,31 @@ async def upload_file(file: UploadFile = File(...)):
Returns: Returns:
HTMLResponse: Returns HTML for site. HTMLResponse: Returns HTML for site.
""" """
# TODO: Add syntax highlighting for text.
# Make custom html for video files.
if file.content_type.startswith("video/"): if file.content_type.startswith("video/"):
file_type = "video"
elif file.content_type.startswith("image/"):
file_type = "image"
elif file.content_type.startswith("text/"):
file_type = "text"
else:
file_type = "files"
output_folder = f"{upload_folder}/{file_type}"
# Create folder if it doesn't exist. # Create folder if it doesn't exist.
Path(output_folder).mkdir(parents=True, exist_ok=True) Path(f"{upload_folder}/video").mkdir(parents=True, exist_ok=True)
file_url = f"{domain}/{file_type}/{file.filename}" # Save file to disk.
file_location = f"{output_folder}/{file.filename}" with open(f"{upload_folder}/video/{file.filename}", "wb+") as file_object:
with open(file_location, "wb+") as file_object:
file_object.write(file.file.read()) file_object.write(file.file.read())
file_url = f"{domain}/video/{file.filename}"
file_location = f"{upload_folder}/video/{file.filename}"
height, width = find_video_resolution(file_location) height, width = find_video_resolution(file_location)
# Only create thumbnail if file is a video.
if file_type == "video":
screenshot_url = make_thumbnail_from_video(file_location, file.filename) screenshot_url = make_thumbnail_from_video(file_location, file.filename)
else: html_url = generate_html(
return file_url filename=file.filename, url=file_url, width=width, height=height, screenshot=screenshot_url
)
return {"html_url": f"{html_url}"}
html_url = generate_html(file_url, width, height, screenshot_url, file.filename) # Save file to disk.
json_output = { with open(f"{upload_folder}/{file.filename}", "wb+") as file_object:
"html_url": f"{html_url}", file_object.write(file.file.read())
"video_url": f"{file_url}",
"width": f"{width}",
"height": f"{height}",
"filename": f"{file.filename}",
"content_type": f"{file.content_type}",
"file_type": f"{file_type}",
"current_time": f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}",
}
# Only add screenshot_url if it exists. return {"html_url": f"{domain}/{file.filename}"}
if screenshot_url:
json_output.update({"screenshot_url": f"{screenshot_url}"})
return json_output
@app.get("/", response_class=HTMLResponse) @app.get("/", response_class=HTMLResponse)
@ -137,17 +114,16 @@ async def main():
""" """
def generate_html( def generate_html(url: str, width: int, height: int, screenshot: str, filename: str) -> str:
url: str, """Generate HTML for video files.
width: int,
height: int,
screenshot: str,
filename: str,
) -> str:
"""Generate HTML for media files.
This is what we will send to other people on Discord. This is what we will send to other people on Discord.
You can remove the .html with your web server so the link will look normal. You can remove the .html with your web server so the link will look normal.
For example, with nginx, you can do this(note the $uri.html):
location / {
try_files $uri $uri/ $uri.html;
}
Args: Args:
url (str): URL for video. url (str): URL for video.