diff --git a/app/routers/hosted_file.py b/app/routers/hosted_file.py index c669727..50a6e78 100644 --- a/app/routers/hosted_file.py +++ b/app/routers/hosted_file.py @@ -11,7 +11,7 @@ from app.db_sql import sql_insert, sql_update, sql_insert_or_update, sql_select, # from .api_crud import delete_obj_template, get_obj_template, get_obj_li_template, patch_obj_template, post_obj_template -from app.methods.hosted_file_methods import create_hosted_file_obj, handle_delete_hosted_file, load_hosted_file_obj, save_file, create_hosted_file_link, delete_hosted_file_link, get_hosted_file_link_rec_list, lookup_file_hash +from app.methods.hosted_file_methods import create_hosted_file_obj, handle_delete_hosted_file, load_hosted_file_obj, save_file, save_file_to_hosted_file, create_hosted_file_link, delete_hosted_file_link, get_hosted_file_link_rec_list, lookup_file_hash from app.models.hosted_file_models import Hosted_File_Base from app.models.response_models import Resp_Body_Base, mk_resp @@ -746,6 +746,12 @@ async def download_tmp( async def convert_file( hosted_file_id: str = Query(..., min_length=11, max_length=22), + link_to_type: str = Query(..., min_length=2, max_length=50), + link_to_id: str = Query(..., min_length=11, max_length=22), + + filename: str = Query('automatic_pdf_to_img_conversion.webp', min_length=11, max_length=150), + # extension: str = Query('webp', min_length=1, max_length=15), + from_type: str = 'pdf', to_type: str = 'webp', pdf_opt1: bool = False, @@ -756,10 +762,20 @@ async def convert_file( log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) + account_id = commons.x_account_id # OSIT _XY7DXtc9MY (1) + account_id_random = commons.x_account_id_random + + # example event_presenter: B3d8eILlQjI (3616) + + link_to_id_random = link_to_id # This is for the object random str ID + if link_to_id := redis_lookup_id_random(record_id_random=link_to_id, table_name=link_to_type): pass + else: + return mk_resp(data=None, status_code=400, response=commons.response) + # Need to look up file_hash for hosted_file_id # file_hash = '0080f0b03144927c173694745483894a09208d9444fdaccab054493f699361be' - file_hash = '279312d1738fd3a8a2f136b48295e28664d38b18de66c55de56b8886b9454784' + file_hash = '279312d1738fd3a8a2f136b48295e28664d38b18de66c55de56b8886b9454784' # G1rTLpGbzhs (5046) file_hash_filename = f'{file_hash}.file' hosted_files_path = settings.FILES_PATH['hosted_files_root'] @@ -783,6 +799,8 @@ async def convert_file( images = convert_from_path(full_file_path, size=(2160, None)) for image in images: + # *** Part 1: *** Convert the file and save the file to tmp and then save the hashed file to hosted_files directory. + save_path = os.path.join(hosted_tmp_convert_file_path, 'converted_2160px_lossless_90q.webp') # image.save('testing_2625px_9.png', compress_level=9) @@ -810,4 +828,96 @@ async def convert_file( # link_to_id_random = link_to_id_random, # check_allowed_extension = False, # ) - # if file_info['saved']: pass \ No newline at end of file + # if file_info['saved']: pass + + # *** Part 2: *** Save the converted hashed file to hosted_files directory. + + file_info = await save_file_to_hosted_file( + file_path = save_path, + filename = 'automatic_pdf_to_img_conversion.webp', + extension = 'webp', + account_id = account_id, + link_to_type = link_to_type, + link_to_id = link_to_id, + ) + + # *** Part 3: *** Save information to database in hosted_file table (hosted_file_link table will be updated by an event_file table trigger) + + if file_info.get('saved'): + # NOTE: Just in case look up in DB based on hash + log.info('Look up in DB based on hash...') + if hosted_file_sel_result := sql_select( + table_name = 'hosted_file', + field_name = 'hash_sha256', + field_value = file_info['hash_sha256'], + ): + log.warning('Found an existing host_file object_entry in the DB but the file was not found on the server!') + # Got existing host_file object_entry! + # Odd... the hash was found in the database, but the file had to be copied again. + # If this happens then the file on the host server was probably deleted at some point. + hosted_file_id = hosted_file_sel_result.get('id', None) + hosted_file_id_random = hosted_file_sel_result.get('id_random', None) + hosted_file_dict = load_hosted_file_obj(hosted_file_id=hosted_file_id, model_as_dict=True) + else: + # This is normal since the file was not found on the host server and not found in the DB. + # Create a new host_file object entry and new host_file.id_random. + file_info['account_id'] = account_id + # file_info['account_id_random'] = account_id_random + hosted_file_obj = Hosted_File_Base(**file_info) + if hosted_file_obj_result := create_hosted_file_obj(hosted_file_obj_new=hosted_file_obj): + hosted_file_id = hosted_file_obj_result + hosted_file_dict = load_hosted_file_obj(hosted_file_id=hosted_file_id, model_as_dict=True) + else: + log.warning('For some reason a host_file object entry could not be created.') + hosted_file_id = None + hosted_file_dict = hosted_file_obj.dict(by_alias=True, exclude_unset=True, exclude={'id', 'id_random'}) # pylint: disable=no-member + log.debug(hosted_file_obj_result) + log.debug(hosted_file_sel_result) + else: return False + + log.debug(hosted_file_dict) + return mk_resp(data=hosted_file_dict, response=commons.response) + + + # *** Part 4: *** Save information to database in event_file (will trigger an update to hosted_file_link) + + # event_file_data = {} + # event_file_data['hosted_file_id'] = hosted_file_id + # # event_file_data['hosted_file_id_random'] = hosted_file_id_random + + # event_file_data['for_type'] = link_to_type + # event_file_data['for_id'] = link_to_id + + # if event_id: + # event_file_data['event_id'] = event_id + # if event_location_id: + # event_file_data['event_location_id'] = event_location_id + # if event_presentation_id: + # event_file_data['event_presentation_id'] = event_presentation_id + # if event_presenter_id: + # event_file_data['event_presenter_id'] = event_presenter_id + # if event_session_id: + # event_file_data['event_session_id'] = event_session_id + # if event_track_id: + # event_file_data['event_track_id'] = event_track_id + + # event_file_data['filename'] = file_info.get('filename') + # event_file_data['extension'] = file_info.get('extension') + + # event_file_data['enable'] = True # hosted_file_obj.enable + + # # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL + # log.debug(event_file_data) + + # try: + # event_file_obj = Event_File_Base(**event_file_data) + # except ValidationError as e: + # log.error(e.json()) + # return False + # log.debug(event_file_obj) + + # create_event_file_obj_result = create_event_file_obj(event_file_obj_new=event_file_obj) + # log.debug(create_event_file_obj_result) + + + # return file_info \ No newline at end of file