Place video/image/text/other in separate folders

This commit is contained in:
2021-10-28 16:24:00 +02:00
parent 84537b7f01
commit 3c666bc0de

33
main.py
View File

@ -4,7 +4,7 @@ import shlex
import subprocess import subprocess
import sys import sys
from datetime import datetime from datetime import datetime
from typing import List from pathlib import Path
from fastapi import FastAPI, File, UploadFile from fastapi import FastAPI, File, UploadFile
from fastapi.responses import HTMLResponse from fastapi.responses import HTMLResponse
@ -19,25 +19,39 @@ except KeyError:
@app.post("/uploadfiles/") @app.post("/uploadfiles/")
async def upload_file(file: UploadFile = File(...)): async def upload_file(file: UploadFile = File(...)):
content_type = file.content_type
try: try:
if upload_file.content_type == "video/mp4": if content_type.startswith("video/"):
os.mkdir("videos") output_folder = "Uploads/video"
elif upload_file.content_type == "": video_url = f"{domain}video/{file.filename}"
os.mkdir("videos") Path(output_folder).mkdir(parents=True, exist_ok=True)
elif upload_file.content_type == "":
os.mkdir("files") 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 e: except Exception as e:
print(e) # TODO: Send to Discord print(e) # TODO: Send to Discord
print(file.filename) print(file.filename)
file_location = f"Uploads/v/{file.filename}" file_location = f"{output_folder}/{file.filename}"
with open(file_location, "wb+") as file_object: with open(file_location, "wb+") as file_object:
file_object.write(file.file.read()) file_object.write(file.file.read())
height, width = find_video_resolution(file_location) height, width = find_video_resolution(file_location)
screenshot_url = get_first_frame(file_location, file.filename) screenshot_url = get_first_frame(file_location, file.filename)
video_url = f"{domain}v/{file.filename}"
html_url = generate_html( html_url = generate_html(
video_url, video_url,
@ -54,6 +68,7 @@ async def upload_file(file: UploadFile = File(...)):
"height": f"{height}", "height": f"{height}",
"screenshot_url": f"{screenshot_url}", "screenshot_url": f"{screenshot_url}",
"filename": f"{file.filename}", "filename": f"{file.filename}",
"content_type": f"{content_type}",
} }