Fix warnings from Ruff and ffmpeg
This commit is contained in:
@ -21,7 +21,7 @@ app: FastAPI = FastAPI(
|
||||
|
||||
|
||||
@app.post("/uploadfiles/", description="Where to send a POST request to upload files.")
|
||||
async def upload_file(file: Annotated[UploadFile, File()]):
|
||||
async def upload_file(file: Annotated[UploadFile, File()]) -> JSONResponse:
|
||||
"""Page for uploading files.
|
||||
|
||||
If it is a video, we need to make an HTML file, and a thumbnail
|
||||
@ -42,9 +42,9 @@ async def upload_file(file: Annotated[UploadFile, File()]):
|
||||
return JSONResponse(content={"error": "Content type is None"}, status_code=500)
|
||||
|
||||
if file.content_type.startswith("video/"):
|
||||
html_url: str = await do_things(file)
|
||||
html_url: str = do_things(file)
|
||||
else:
|
||||
filename: str = await remove_illegal_chars(file.filename)
|
||||
filename: str = remove_illegal_chars(file.filename)
|
||||
|
||||
with Path.open(Path(settings.upload_folder, filename), "wb+") as f:
|
||||
f.write(file.file.read())
|
||||
@ -55,7 +55,7 @@ async def upload_file(file: Annotated[UploadFile, File()]):
|
||||
return JSONResponse(content={"html_url": html_url})
|
||||
|
||||
|
||||
async def remove_illegal_chars(file_name: str) -> str:
|
||||
def remove_illegal_chars(file_name: str) -> str:
|
||||
"""Remove illegal characters from the filename.
|
||||
|
||||
Args:
|
||||
@ -113,7 +113,7 @@ index_html: str = """
|
||||
|
||||
|
||||
@app.get("/", response_class=HTMLResponse, include_in_schema=False)
|
||||
async def main(request: Request): # noqa: ARG001
|
||||
async def main(request: Request) -> str: # noqa: ARG001
|
||||
"""Our index view.
|
||||
|
||||
You can upload files here.
|
||||
|
@ -54,14 +54,25 @@ def make_thumbnail(path_video: str, file_filename: str) -> str:
|
||||
path_video: Path where video file is stored.
|
||||
file_filename: File name for URL.
|
||||
|
||||
Raises:
|
||||
ffmpeg.Error: If there is an error creating the thumbnail.
|
||||
|
||||
Returns:
|
||||
Returns thumbnail filename.
|
||||
"""
|
||||
(
|
||||
ffmpeg.input(path_video, ss="1")
|
||||
.output(f"{settings.upload_folder}/{file_filename}.jpg", vframes=1)
|
||||
.overwrite_output()
|
||||
.run()
|
||||
)
|
||||
output_path: str = f"{settings.upload_folder}/{file_filename}.jpg"
|
||||
try:
|
||||
(
|
||||
ffmpeg.input(path_video, ss="1")
|
||||
.output(output_path, vframes=1, format="image2", update=1)
|
||||
.overwrite_output()
|
||||
.run()
|
||||
)
|
||||
except ffmpeg.Error:
|
||||
logger.exception("Error creating thumbnail")
|
||||
raise
|
||||
|
||||
logger.info("Thumbnail created: %s", output_path)
|
||||
|
||||
# Return URL for thumbnail.
|
||||
return f"{settings.serve_domain}/{file_filename}.jpg"
|
||||
|
@ -57,7 +57,7 @@ def save_to_disk(file: UploadFile) -> VideoFile:
|
||||
return VideoFile(filename, str(file_location))
|
||||
|
||||
|
||||
async def do_things(file: UploadFile) -> str:
|
||||
def do_things(file: UploadFile) -> str:
|
||||
"""Save video to disk, generate HTML, thumbnail, and return a .html URL.
|
||||
|
||||
Args:
|
||||
|
Reference in New Issue
Block a user