Respond with error code

This commit is contained in:
2021-12-07 20:02:21 +01:00
parent fad95e339c
commit 0b387cc792
2 changed files with 34 additions and 27 deletions

21
main.py
View File

@ -1,9 +1,12 @@
"""Website for uploading files and creating .HTMLs and thumbnails so we can embed files in Discord.
"""Website for uploading files, creating .HTMLs, and thumbnails.
This was created for Discord. You can use this to embed videos in Discord.
"""
import sys
from datetime import datetime
from pathlib import Path
from typing import Dict
import ffmpeg
from fastapi import FastAPI, File, UploadFile
@ -28,17 +31,17 @@ app = FastAPI(
@app.post("/uploadfiles/")
async def upload_file(file: UploadFile = File(...)):
async def upload_file(file: UploadFile = File(...)) -> Dict[str, str]:
"""Page for uploading files.
Args:
file (UploadFile): Our uploaded file. Defaults to File(...).
Returns:
HTMLResponse: Returns HTML for site.
Dict[str, str]: Returns a dict with the filename or a link to the .html if it was a video.
"""
# TODO: Add syntax highlighting for text.
try:
# Make custom html for video files.
if file.content_type.startswith("video/"):
# Create folder if it doesn't exist.
@ -62,6 +65,10 @@ async def upload_file(file: UploadFile = File(...)):
file_object.write(file.file.read())
return {"html_url": f"{Settings.domain}/{file.filename}"}
except Exception as e:
# TODO: Change response code to 400.
print(e)
return {"error": f"Something went wrong: {e}"}
@app.get("/", response_class=HTMLResponse)
@ -104,9 +111,9 @@ def generate_html(url: str, width: int, height: int, screenshot: str, filename:
Args:
url (str): URL for video.
width (int): Video width.
height (int): Video height.
url (str): URL for the video. This is accessible from the browser.
width (int): This is the width of the video.
height (int): This is the height of the video.
screenshot (str): URL for screenshot. This is what you will see in Discord.
filename (str): Original video filename. We will append .html to the filename.

View File

@ -7,7 +7,7 @@ from dotenv import load_dotenv
class Settings:
description = (
"Discord will only create embeds for videos and images if they are smaller than 8 mb. We can 'abuse' this"
" by using the 'twitter:image' HTML meta tag."
" by creating a .html that contains the 'twitter:player' HTML meta tag linking to the video."
)
# Load environment variables
load_dotenv()