Work on websockets. A lot...

This commit is contained in:
Scott Idem
2023-04-07 00:37:57 -04:00
parent b369d00b3e
commit 0eb775826f
4 changed files with 1273 additions and 72 deletions

View File

@@ -3,6 +3,7 @@ from fastapi import APIRouter, Body, Depends, File, Form, Header, HTTPException,
from fastapi.responses import FileResponse
from pydantic import BaseModel, EmailStr, Field
from typing import Dict, List, Optional, Set, Union
from pdf2image import convert_from_path
from app.lib_general import log, logging, common_route_params, Common_Route_Params, common_route_params_min, Common_Route_Params_Min
from app.config import settings
@@ -699,20 +700,114 @@ async def get_hosted_file_obj(
# ### BEGIN ### API Hosted File ### download_tmp() ###
# Updated 2021-11-23
@router.get('/download/tmp/{filename}', response_model=Resp_Body_Base)
# Updated 2023-04-05
@router.get('/tmp/{subdirectory}/{filename}/download', response_model=Resp_Body_Base)
async def download_tmp(
filename: str = Query(..., min_length=4, max_length=100),
# x_account_id: str = Header(...),
by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True,
response: Response = Response,
subdirectory: str = Query(..., min_length=1, max_length=100),
filename: str = Query(..., min_length=4, max_length=120),
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
full_dest_path = 'admin/temp/order_line/order_line_list_2021-11-23_1310.xlsx'
filename = 'text.xlsx'
# NOTE: WARNING NOTE: WARNING NOTE: WARNING NOTE: WARNING NOTE: WARNING NOTE: WARNING NOTE: WARNING
time.sleep(3.5) # NOTE: WARNING NOTE: WARNING NOTE: WARNING NOTE: WARNING NOTE: WARNING NOTE: WARNING
# NOTE: WARNING NOTE: WARNING NOTE: WARNING NOTE: WARNING NOTE: WARNING NOTE: WARNING NOTE: WARNING
return FileResponse(full_dest_path, filename=filename)
hosted_tmp_path = settings.FILES_PATH['hosted_tmp_root']
log.info(f'Hosted Tmp Path: {hosted_tmp_path}')
log.debug(shutil.disk_usage(hosted_tmp_path))
hosted_tmp_w_subdir = os.path.join(hosted_tmp_path, subdirectory)
# if pathlib.Path(hosted_tmp_w_subdir):
if os.path.exists(hosted_tmp_w_subdir):
log.info('Hosted tmp with subdirectory found')
else:
log.info('Hosted tmp with subdirectory not found')
return mk_resp(data=False, status_code=404, response=commons.response, status_message='The hosted tmp file subdirectory was not found.') # Not Found
hosted_tmp_w_subdir_filename = os.path.join(hosted_tmp_path, subdirectory, filename)
# if pathlib.Path(hosted_tmp_w_subdir_filename):
if os.path.exists(hosted_tmp_w_subdir_filename):
log.info('Hosted tmp with subdirectory and filename found')
else:
log.info('Hosted tmp with subdirectory and filename not found')
return mk_resp(data=False, status_code=404, response=commons.response, status_message='The hosted tmp file was not found.') # Not Found
return FileResponse(hosted_tmp_w_subdir_filename, filename=filename)
# ### END ### API Hosted File ### download_tmp() ###
# ### BEGIN ### API Hosted File Route ### convert_file() ###
# This just needs to return the correct model for a new hosted_file
# Updated 2023-04-04
@router.get('/{hosted_file_id}/convert_file')
async def convert_file(
hosted_file_id: str = Query(..., min_length=11, max_length=22),
from_type: str = 'pdf',
to_type: str = 'webp',
pdf_opt1: bool = False,
pdf_opt2: str = 'test',
commons: Common_Route_Params = Depends(common_route_params),
):
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
# Need to look up file_hash for hosted_file_id
# file_hash = '0080f0b03144927c173694745483894a09208d9444fdaccab054493f699361be'
file_hash = '279312d1738fd3a8a2f136b48295e28664d38b18de66c55de56b8886b9454784'
file_hash_filename = f'{file_hash}.file'
hosted_files_path = settings.FILES_PATH['hosted_files_root']
log.info(f'Hosted Files Path: {hosted_files_path}')
log.debug(shutil.disk_usage(hosted_files_path))
file_subdirectory = file_hash[0:2]
full_file_path = os.path.join(hosted_files_path, file_subdirectory, file_hash_filename)
log.info(f'File Hash with Subdirectory: {full_file_path}')
hosted_tmp_path = settings.FILES_PATH['hosted_tmp_root']
log.info(f'Hosted Tmp Path: {hosted_tmp_path}')
log.debug(shutil.disk_usage(hosted_tmp_path))
hosted_tmp_convert_file_path = os.path.join(hosted_tmp_path, 'convert_file')
if pathlib.Path(hosted_tmp_convert_file_path):
log.info('Hosted tmp convert file path found')
else:
log.info('Creating hosted tmp convert file path')
pathlib.Path(hosted_tmp_convert_file_path).mkdir(parents=True, exist_ok=True)
images = convert_from_path(full_file_path, size=(2160, None))
for image in images:
save_path = os.path.join(hosted_tmp_convert_file_path, 'converted_2160px_lossless_90q.webp')
# image.save('testing_2625px_9.png', compress_level=9)
# Lossy WebP takes about 25% of the time as WebP lossless compression with 100 level effort
# .46 seconds vs 2.1 seconds with example PDF
# image.save('testing_2625px_80q.webp', quality=80) # default
# timer_2a_start = timer()
image.save(save_path, lossless=False, quality=90) # default quality is 80
# timer_2a_end = timer()
# print( round((timer_2a_end - timer_2a_start), 8) )
# timer_2b_start = timer()
# image.save('testing_2160px_lossless_100q.webp', lossless=True, quality=100) # quality is level of effort
# timer_2b_end = timer()
# print( round((timer_2b_end - timer_2b_start), 8) )
# file_info = await save_file(
# file = file_obj,
# account_id = account_id,
# account_id_random = account_id_random,
# link_to_type = link_to_type,
# link_to_id = link_to_id,
# link_to_id_random = link_to_id_random,
# check_allowed_extension = False,
# )
# if file_info['saved']: pass