Rename the domain environment variable and use urljoin when joining URLs

This commit is contained in:
2022-04-20 16:47:47 +02:00
parent 506670e4de
commit 1676186e2f
6 changed files with 19 additions and 8 deletions

8
.env.example Normal file
View File

@ -0,0 +1,8 @@
# Domain where we server files from, not where we upload files to
SERVE_DOMAIN=https://i.example.com/
# Path to the directory where we store files
UPLOAD_FOLDER=/Uploads
# Discord Webhook URL
WEBHOOK_URL=https://discord.com/api/webhooks/9251111537353924718/X0WIe7gAFeHtplVrYZ_zU2Jb22RMOgRlwbaIkXVvZz9Z2H8b4ogfTkE7T-0mQkvPG8_Q

View File

@ -9,6 +9,7 @@ location / {
"""
import os
from datetime import datetime
from urllib.parse import urljoin
from discord_embed import settings
@ -47,7 +48,8 @@ def generate_html_for_videos(
</head>
</html>
"""
html_url = os.path.join(settings.domain, filename)
domain = settings.serve_domain
html_url: str = urljoin(domain, filename)
# Take the filename and append .html to it.
filename += ".html"

View File

@ -1,6 +1,7 @@
"""Our site has one POST endpoint for uploading videos and one GET
endpoint for getting the HTML. Images are served from a webserver."""
from typing import Dict
from urllib.parse import urljoin
from fastapi import FastAPI, File, Request, UploadFile
from fastapi.responses import HTMLResponse
@ -55,7 +56,7 @@ async def upload_file(file: UploadFile = File(...)) -> Dict[str, str]:
with open(f"{settings.upload_folder}/{filename}", "wb+") as f:
f.write(file.file.read())
domain_url = f"{settings.domain}/{filename}"
domain_url = urljoin(settings.serve_domain, filename)
send_webhook(f"{domain_url} was uploaded.")
return {"html_url": domain_url}

View File

@ -15,13 +15,13 @@ load_dotenv()
# Check if user has added a domain to the environment.
try:
domain = os.environ["DOMAIN"]
serve_domain = os.environ["SERVE_DOMAIN"]
except KeyError:
sys.exit("discord-embed: Environment variable 'DOMAIN' is missing!")
# Remove trailing slash from domain
if domain.endswith("/"):
domain = domain[:-1]
if serve_domain.endswith("/"):
serve_domain = serve_domain[:-1]
# Check if we have a folder for uploads.
try:

View File

@ -44,4 +44,4 @@ def make_thumbnail(path_video: str, file_filename: str) -> str:
.run()
)
# Return URL for thumbnail.
return f"{settings.domain}/{file_filename}.jpg"
return f"{settings.serve_domain}/{file_filename}.jpg"

View File

@ -48,7 +48,7 @@ async def do_things(file: UploadFile) -> Dict[str, str]:
"""
filename, file_location = save_to_disk(file)
file_url = f"{settings.domain}/video/{filename}"
file_url = f"{settings.serve_domain}/video/{filename}"
height, width = video_resolution(file_location)
screenshot_url = make_thumbnail(file_location, filename)
html_url = generate_html_for_videos(
@ -58,5 +58,5 @@ async def do_things(file: UploadFile) -> Dict[str, str]:
screenshot=screenshot_url,
filename=filename,
)
send_webhook(f"{settings.domain}/{filename} was uploaded.")
send_webhook(f"{settings.serve_domain}/{filename} was uploaded.")
return {"html_url": f"{html_url}"}