diff --git a/app/routers/hosted_file.py b/app/routers/hosted_file.py index fd8f168..19ea409 100644 --- a/app/routers/hosted_file.py +++ b/app/routers/hosted_file.py @@ -51,8 +51,69 @@ async def upload_files( file_info_list = [] for file_obj in file_list: - file_info = await save_file(file=file_obj, account_id=account_id, account_id_random=account_id_random, for_object_type=for_object_type, for_object_id=for_object_id, for_object_id_random=for_object_id_random, check_allowed_extension=check_allowed_extension) - file_info_list.append(file_info) + file_info = await save_file( + file = file_obj, + account_id = account_id, + account_id_random = account_id_random, + for_object_type = for_object_type, + for_object_id = for_object_id, + for_object_id_random = for_object_id_random, + check_allowed_extension = check_allowed_extension, + ) + if file_info['saved']: + # Create a new host_file object entry + if file_info['already_exists']: + # Look up in DB based on hash + # Get existing host_file object_entry and existing host_file.id_random. + if hosted_file_sel_result := sql_select( + table_name = 'hosted_file', + field_name = 'hash_sha256', + filed_value = file_info['hash_sha256'], + ): + file_info['id_random'] = hosted_file_result.get('id_random', None) + hosted_file_obj = Hosted_File_Base(**file_info) + else: + # SOMETHING WENT WRONG + # Going to try and create a new host_file entry... + log.warning('For some reason a host_file object entry with the has was not found.') + # file_info['id_random'] = None + hosted_file_dict = Hosted_File_Base(**file_info).dict(by_alias=False, exclude_defaults=False, exclude_unset=True) + if hosted_file_in_result := sql_insert(data=hosted_file_dict, table_name='hosted_file'): + hosted_file.id_random = hosted_file_result.get('id_random', None) + else: + log.warning('For some reason a host_file object entry could not be created.') + hosted_file.id_random = None + log.debug(hosted_file_in_result) + log.debug(hosted_file_sel_result) + else: + # Just in case look up in DB based on hash + if hosted_file_sel_result := sql_select( + table_name = 'hosted_file', + field_name = 'hash_sha256', + filed_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. + file_info['id_random'] = hosted_file_result.get('id_random', None) + hosted_file_obj = Hosted_File_Base(**file_info) + 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. + hosted_file_dict = Hosted_File_Base(**file_info).dict(by_alias=False, exclude_defaults=False, exclude_unset=True) + if hosted_file_in_result := sql_insert(data=hosted_file_dict, table_name='hosted_file'): + file_info['id_random'] = hosted_file_result.get('id_random', None) + else: + log.warning('For some reason a host_file object entry could not be created.') + file_info['id_random'] = None + log.debug(hosted_file_in_result) + log.debug(hosted_file_sel_result) + else: + file_info['id_random'] = None + + # file_info_obj = Hosted_File_Base(**file_info) + file_info_list.append(file_info_obj) log.debug(file_info_list) return mk_resp(data=file_info_list) @@ -68,8 +129,9 @@ async def save_file( for_object_id: int, for_object_id_random: str, check_allowed_extension: bool = False, + ): - log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL + # log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) hosted_file_path = '/home/scott/tmp/hosted_file_dev/' @@ -179,3 +241,63 @@ def guess_file_extension(filename:str): def allowed_file_extension(extension:str): return extension.lower() in app.config['ALLOWED_EXTENSIONS'] # ### END ### API Hosted File Route ### allowed_file_extension() ### + + +# ### BEGIN ### API Hosted File Route ### save_file() ### +async def hosted_file_link( + account_id: int, + hosted_file_id: str, + for_object_type: str, + for_object_id: int, + for_object_id_random: str, + ): + log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL + log.debug(locals()) + + hosted_file_link_data = {} + hosted_file_link_data['account_id'] = account_id + hosted_file_link_data['hosted_file_id'] = hosted_file_id + hosted_file_link_data['object_type'] = for_object_type + hosted_file_link_data['object_id'] = for_object_id + + if response['data']['id'] == True: + #print('Tried to insert a new hosted_file record, but there is a duplicate.') + # There was likely a record with the same hash value. + table_name = 'hosted_file' + field_name = 'hash_sha256' + field_value = data['hash_sha256'] + select_hosted_file_response = select_record(table_name=table_name, field_name=field_name, field_value=field_value) + if select_hosted_file_response: + hosted_file_link_data['hosted_file_id'] = select_hosted_file_response['id'] + response['data']['id'] = select_hosted_file_response['id'] + response['data']['id_random'] = select_hosted_file_response['id_random'] + else: + return False + else: + print('Inserted new host_file record.') + pass + + table_name = 'hosted_file_link' + + hosted_file_link_response = sql_insert_for_rest(data=hosted_file_link_data, table_name=table_name, sql=None, model=None, resource_ref=True) + + + +# ### BEGIN ### API Hosted File Methods ### create_hosted_file_obj() ### +def create_hosted_file_obj(hosted_file_obj_new:Hosted_File_Base): + log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL + log.debug(locals()) + + hosted_file_obj_data = hosted_file_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'user', 'created_on', 'updated_on'}) + + if hosted_file_obj_in_result := sql_insert(data=hosted_file_obj_data, table_name='hosted_file', rm_id_random=True, id_random_length=8): pass + else: + return False + + log.debug(hosted_file_obj_in_result) + + hosted_file_id = hosted_file_obj_in_result + + log.debug(f'Returning the new hosted_file_id: {hosted_file_id}') + return hosted_file_id +# ### END ### API Hosted File Methods ### create_hosted_file_obj() ### \ No newline at end of file