Bug fixes for uploading the files. I though the changes being made where not supposed to break legacy endpoints. Not sure what happened. Either way things are almost back to normal.

This commit is contained in:
Scott Idem
2026-01-22 16:49:03 -05:00
parent 48d9e38c39
commit 1e6b9d1c18
8 changed files with 521 additions and 17 deletions

View File

@@ -458,6 +458,10 @@ async def upload_files(
link_to_id_random = link_to_id_random,
check_allowed_extension = check_allowed_extension,
)
hosted_file_id = None
hosted_file_dict = {}
if file_info['saved']:
# Create a new host_file object entry
log.info('Check and create a new host_file object entry...')
@@ -470,8 +474,7 @@ async def upload_files(
field_name = 'hash_sha256',
field_value = file_info['hash_sha256'],
):
hosted_file_id = hosted_file_sel_result.get('id_random', None)
# hosted_file_obj = Hosted_File_Base(**file_info)
hosted_file_id = hosted_file_sel_result.get('id', None)
hosted_file_dict = load_hosted_file_obj(hosted_file_id=hosted_file_id, model_as_dict=True)
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
@@ -520,8 +523,7 @@ async def upload_files(
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
return mk_resp(data=False, status_code=500, response=response, status_message='Database insertion failed.')
log.debug(hosted_file_obj_result)
log.debug(hosted_file_sel_result)
else:
@@ -536,7 +538,7 @@ async def upload_files(
# 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_random', None)
hosted_file_id = hosted_file_sel_result.get('id', 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.
@@ -568,6 +570,16 @@ async def upload_files(
hosted_file_dict['filename'] = file_info['filename']
hosted_file_dict['extension'] = file_info['extension']
# Ensure we return clean random IDs for the frontend
if hosted_file_dict.get('id') is None or isinstance(hosted_file_dict.get('id'), int):
# Try to get id_random for the dictionary if missing or integer
if hosted_file_id:
from app.db_sql import get_id_random
rid = get_id_random(hosted_file_id, table_name='hosted_file')
if rid:
hosted_file_dict['id'] = rid
hosted_file_dict['hosted_file_id'] = rid
hosted_file_list.append(hosted_file_dict)
# NOTE: Currently sql_insert does not handle all successful inserts correctly. If there is not an autonum ID then it will return 0 as the ID.