diff --git a/discord_embed/main.py b/discord_embed/main.py index d296425..5f55842 100644 --- a/discord_embed/main.py +++ b/discord_embed/main.py @@ -1,8 +1,7 @@ -from typing import Dict from urllib.parse import urljoin from fastapi import FastAPI, File, Request, UploadFile -from fastapi.responses import HTMLResponse +from fastapi.responses import HTMLResponse, JSONResponse from fastapi.staticfiles import StaticFiles from fastapi.templating import Jinja2Templates @@ -12,16 +11,9 @@ from discord_embed.webhook import send_webhook app: FastAPI = FastAPI( title="discord-nice-embed", - description=settings.DESCRIPTION, - 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", + "name": "Github repo", + "url": "https://github.com/TheLovinator1/discord-embed", }, ) @@ -29,8 +21,8 @@ app.mount("/static", StaticFiles(directory="static"), name="static") templates: Jinja2Templates = Jinja2Templates(directory="templates") -@app.post("/uploadfiles/") -async def upload_file(file: UploadFile = File(...)) -> Dict[str, str]: +@app.post("/uploadfiles/", description="Where to send a POST request to upload files.") +async def upload_file(file: UploadFile = File()): """Page for uploading files. If it is a video, we need to make an HTML file, and a thumbnail @@ -44,16 +36,17 @@ async def upload_file(file: UploadFile = File(...)) -> Dict[str, str]: Returns a dict with the filename, or a link to the .html if it was a video. """ if file.content_type.startswith("video/"): - return await do_things(file) + html_url: str = await do_things(file) + else: + filename: str = await remove_illegal_chars(file.filename) - filename: str = await remove_illegal_chars(file.filename) + with open(f"{settings.upload_folder}/{filename}", "wb+") as f: + f.write(file.file.read()) - with open(f"{settings.upload_folder}/{filename}", "wb+") as f: - f.write(file.file.read()) + html_url: str = urljoin(settings.serve_domain, filename) # type: ignore - domain_url: str = urljoin(settings.serve_domain, filename) - send_webhook(f"{domain_url} was uploaded.") - return {"html_url": domain_url} + send_webhook(f"{html_url} was uploaded.") + return JSONResponse(content={"html_url": html_url}) async def remove_illegal_chars(file_name: str) -> str: @@ -96,7 +89,7 @@ async def remove_illegal_chars(file_name: str) -> str: return filename -@app.get("/", response_class=HTMLResponse) +@app.get("/", response_class=HTMLResponse, include_in_schema=False) async def main(request: Request): """Our index view. diff --git a/discord_embed/settings.py b/discord_embed/settings.py index 1e2ea0c..8f603e7 100644 --- a/discord_embed/settings.py +++ b/discord_embed/settings.py @@ -4,11 +4,6 @@ import sys from dotenv import load_dotenv -DESCRIPTION: str = ( - "Discord will only create embeds for videos and images if they are " - "smaller than 8 mb. We can 'abuse' this by creating a .html that " - "contains the 'twitter:player' HTML meta tag linking to the video." -) # Load environment variables load_dotenv() diff --git a/discord_embed/video_file_upload.py b/discord_embed/video_file_upload.py index 67b385e..c498822 100644 --- a/discord_embed/video_file_upload.py +++ b/discord_embed/video_file_upload.py @@ -1,14 +1,12 @@ import os from dataclasses import dataclass from pathlib import Path -from typing import Dict from fastapi import UploadFile from discord_embed import settings from discord_embed.generate_html import generate_html_for_videos from discord_embed.video import Resolution, make_thumbnail, video_resolution -from discord_embed.webhook import send_webhook @dataclass @@ -49,7 +47,7 @@ def save_to_disk(file: UploadFile) -> VideoFile: return VideoFile(filename, file_location) -async def do_things(file: UploadFile) -> Dict[str, str]: +async def do_things(file: UploadFile) -> str: """Save video to disk, generate HTML, thumbnail, and return a .html URL. Args: @@ -71,5 +69,4 @@ async def do_things(file: UploadFile) -> Dict[str, str]: screenshot=screenshot_url, filename=video_file.filename, ) - send_webhook(f"{settings.serve_domain}/{video_file.filename} was uploaded.") - return {"html_url": f"{html_url}"} + return html_url