"""Website for uploading files and creating .HTMLs and thumbnails so we can embed files in Discord. """ import os import sys from datetime import datetime from pathlib import Path import ffmpeg from dhooks import Webhook from fastapi import FastAPI, File, UploadFile from fastapi.responses import HTMLResponse hook = Webhook(os.environ["WEBHOOK"]) app = FastAPI( title="discord-nice-embed", description=""" Discord will only create embeds for videos and images if they are smaller than 8mb. We can "abuse" this by using the "twitter:image" HTML meta tag. """, version="0.0.1", contact={ "name": "Joakim Hellsén", "url": "https://github.com/TheLovinator1", "email": "tlovinator@gmail.com", }, license_info={ "name": "GPL-3.0", "url": "https://www.gnu.org/licenses/gpl-3.0.txt", }, ) try: domain = os.environ["DOMAIN"] except KeyError: sys.exit("Environment variable 'DOMAIN' is missing!") if not domain.endswith("/"): domain += "/" @app.post("/uploadfiles/") async def upload_file(file: UploadFile = File(...)): """Page for uploading files. Args: file (UploadFile): Our uploaded file. Defaults to File(...). Returns: HTMLResponse: Returns HTML for site. """ content_type = file.content_type try: if content_type.startswith("video/"): output_folder = "Uploads/video" video_url = f"{domain}video/{file.filename}" Path(output_folder).mkdir(parents=True, exist_ok=True) elif content_type.startswith("image/"): output_folder = "Uploads/image" video_url = f"{domain}image/{file.filename}" Path(output_folder).mkdir(parents=True, exist_ok=True) elif content_type.startswith("text/"): output_folder = "Uploads/text" video_url = f"{domain}text/{file.filename}" Path(output_folder).mkdir(parents=True, exist_ok=True) else: output_folder = "Uploads/files" video_url = f"{domain}files/{file.filename}" Path(output_folder).mkdir(parents=True, exist_ok=True) except Exception as exception: print(f"Failed to get content type/create folder: {exception}") hook.send(f"Failed to get content type/create folder: {exception}") file_location = f"{output_folder}/{file.filename}" with open(file_location, "wb+") as file_object: file_object.write(file.file.read()) height, width = find_video_resolution(file_location) screenshot_url = make_thumbnail(file_location, file.filename) html_url = generate_html( video_url, width, height, screenshot_url, file.filename, ) return { "html_url": f"{html_url}", "video_url": f"{video_url}", "width": f"{width}", "height": f"{height}", "screenshot_url": f"{screenshot_url}", "filename": f"{file.filename}", "content_type": f"{content_type}", } @app.get("/", response_class=HTMLResponse) async def main(): """Our index view. You can upload files here. Returns: HTMLResponse: Returns HTML for site. """ return """