Commit
·
3fc2a44
1
Parent(s):
6c68a13
add basic http auth. add base for periodic arhiving. sep html of submit form.
Browse files- .gitignore +1 -0
- api_config_example.yml +4 -0
- main.py +26 -4
- static/submit_video.html +14 -0
- utils/api_configs.py +6 -0
- utils/periodic_archiver.py +0 -0
.gitignore
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
data/*
|
| 2 |
temp/
|
|
|
|
| 3 |
# Byte-compiled / optimized / DLL files
|
| 4 |
__pycache__/
|
| 5 |
*.py[cod]
|
|
|
|
| 1 |
data/*
|
| 2 |
temp/
|
| 3 |
+
api_config.yml
|
| 4 |
# Byte-compiled / optimized / DLL files
|
| 5 |
__pycache__/
|
| 6 |
*.py[cod]
|
api_config_example.yml
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
api_config:
|
| 2 |
+
secrets:
|
| 3 |
+
username: user
|
| 4 |
+
password: password
|
main.py
CHANGED
|
@@ -1,9 +1,11 @@
|
|
| 1 |
-
from fastapi import FastAPI, UploadFile, File, HTTPException, Form
|
| 2 |
from fastapi.responses import FileResponse, HTMLResponse
|
|
|
|
| 3 |
from typing import Optional
|
| 4 |
from utils.process_video import process_video
|
| 5 |
from utils.zip_response import zip_response
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
logging.basicConfig(filename='main.log',
|
| 9 |
encoding='utf-8',
|
|
@@ -11,6 +13,19 @@ logging.basicConfig(filename='main.log',
|
|
| 11 |
format='%(asctime)s %(levelname)s %(message)s',
|
| 12 |
datefmt='%m/%d/%Y %I:%M:%S %p')
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
app = FastAPI()
|
| 16 |
|
|
@@ -45,11 +60,14 @@ async def process_video_api(video_file: UploadFile = File(...),
|
|
| 45 |
fontsize: Optional[int] = Form(36),
|
| 46 |
font: Optional[str] = Form("FuturaPTHeavy"),
|
| 47 |
bg_color: Optional[str] = Form("#070a13b3"),
|
| 48 |
-
text_color: Optional[str] = Form("white")
|
|
|
|
| 49 |
):
|
| 50 |
try:
|
| 51 |
if not str(video_file.filename).endswith('.mp4'):
|
| 52 |
-
raise HTTPException(status_code=400, detail="Invalid
|
|
|
|
|
|
|
| 53 |
logging.info("Creating temporary directories")
|
| 54 |
temp_dir = os.path.join(os.getcwd(),"temp")
|
| 55 |
os.makedirs(temp_dir, exist_ok=True)
|
|
@@ -83,3 +101,7 @@ async def process_video_api(video_file: UploadFile = File(...),
|
|
| 83 |
|
| 84 |
except Exception as e:
|
| 85 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException, Form, Depends
|
| 2 |
from fastapi.responses import FileResponse, HTMLResponse
|
| 3 |
+
from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
| 4 |
from typing import Optional
|
| 5 |
from utils.process_video import process_video
|
| 6 |
from utils.zip_response import zip_response
|
| 7 |
+
from utils.api_configs import api_configs
|
| 8 |
+
import shutil, os, logging, uvicorn, secrets
|
| 9 |
|
| 10 |
logging.basicConfig(filename='main.log',
|
| 11 |
encoding='utf-8',
|
|
|
|
| 13 |
format='%(asctime)s %(levelname)s %(message)s',
|
| 14 |
datefmt='%m/%d/%Y %I:%M:%S %p')
|
| 15 |
|
| 16 |
+
security = HTTPBasic()
|
| 17 |
+
api_configs_file = os.path.abspath("api_config_example.yml")
|
| 18 |
+
|
| 19 |
+
def get_current_user(credentials: HTTPBasicCredentials = Depends(security)):
|
| 20 |
+
correct_username = secrets.compare_digest(credentials.username, api_configs(api_configs_file)["secrets"]["username"])
|
| 21 |
+
correct_password = secrets.compare_digest(credentials.password, api_configs(api_configs_file)["secrets"]["password"])
|
| 22 |
+
if not (correct_username and correct_password):
|
| 23 |
+
raise HTTPException(
|
| 24 |
+
status_code=401,
|
| 25 |
+
detail="Incorrect email or password",
|
| 26 |
+
headers={"WWW-Authenticate": "Basic"},
|
| 27 |
+
)
|
| 28 |
+
return credentials.username
|
| 29 |
|
| 30 |
app = FastAPI()
|
| 31 |
|
|
|
|
| 60 |
fontsize: Optional[int] = Form(36),
|
| 61 |
font: Optional[str] = Form("FuturaPTHeavy"),
|
| 62 |
bg_color: Optional[str] = Form("#070a13b3"),
|
| 63 |
+
text_color: Optional[str] = Form("white"),
|
| 64 |
+
username: str = Depends(get_current_user)
|
| 65 |
):
|
| 66 |
try:
|
| 67 |
if not str(video_file.filename).endswith('.mp4'):
|
| 68 |
+
raise HTTPException(status_code=400, detail="Invalid Video File type. Please upload an MP4 file.")
|
| 69 |
+
if srt_file.size > 0 and not str(srt_file.filename).endswith('.srt'):
|
| 70 |
+
raise HTTPException(status_code=400, detail="Invalid Subtitles File type. Please upload an SRT file.")
|
| 71 |
logging.info("Creating temporary directories")
|
| 72 |
temp_dir = os.path.join(os.getcwd(),"temp")
|
| 73 |
os.makedirs(temp_dir, exist_ok=True)
|
|
|
|
| 101 |
|
| 102 |
except Exception as e:
|
| 103 |
raise HTTPException(status_code=500, detail=str(e))
|
| 104 |
+
|
| 105 |
+
if __name__ == "__main__":
|
| 106 |
+
# Use Uvicorn to run the application
|
| 107 |
+
uvicorn.run(app, host="127.0.0.1", port=8000)
|
static/submit_video.html
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<html>
|
| 2 |
+
<body>
|
| 3 |
+
<form action="/process_video/" enctype="multipart/form-data" method="post">
|
| 4 |
+
Video File: <input type="file" name="video_file"><br>
|
| 5 |
+
Subtitles File: <input type="file" name="srt_file"><br>
|
| 6 |
+
Max words per line: <input type="number" name="max_words_per_line" value="8"><br>
|
| 7 |
+
Font size: <input type="number" name="fontsize" value="36"><br>
|
| 8 |
+
Font: <input type="text" name="font" value="FuturaPTHeavy"><br>
|
| 9 |
+
Background color: <input type="text" name="bg_color" value="#070a13b3"><br>
|
| 10 |
+
Text color: <input type="text" name="text_color" value="white"><br>
|
| 11 |
+
<input type="submit">
|
| 12 |
+
</form>
|
| 13 |
+
</body>
|
| 14 |
+
</html>
|
utils/api_configs.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import yaml
|
| 2 |
+
|
| 3 |
+
def api_configs(config_file):
|
| 4 |
+
with open(config_file, 'r') as f:
|
| 5 |
+
db_config = yaml.safe_load(f)
|
| 6 |
+
return db_config["api_config"]
|
utils/periodic_archiver.py
ADDED
|
File without changes
|