This commit is contained in:
Scott Idem
2023-04-09 19:39:04 -04:00
parent 890a5d8ad1
commit f2309a28a9
2 changed files with 60 additions and 60 deletions

View File

@@ -3,7 +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 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
@@ -739,75 +739,75 @@ async def download_tmp(
# ### 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),
# # ### 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',
# 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())
# 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
# # Need to look up file_hash for hosted_file_id
# file_hash = '0080f0b03144927c173694745483894a09208d9444fdaccab054493f699361be'
file_hash = '279312d1738fd3a8a2f136b48295e28664d38b18de66c55de56b8886b9454784'
file_hash_filename = f'{file_hash}.file'
# # 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))
# 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}')
# 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_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)
# 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')
# 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)
# # 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
# # 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) )
# # 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) )
# # 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
# # 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