Working on how hosted files are saved. Moving them into two letter subdirectories.

This commit is contained in:
Scott Idem
2021-08-26 20:52:13 -04:00
parent 4a839b91d7
commit 64b77ec238
2 changed files with 32 additions and 13 deletions

View File

@@ -32,6 +32,7 @@ class Hosted_File_Base(BaseModel):
version: Optional[int] version: Optional[int]
directory_path: Optional[str] directory_path: Optional[str]
subdirectory_path: Optional[str]
filename: Optional[str] filename: Optional[str]
extension: Optional[str] extension: Optional[str]
content_type: Optional[str] content_type: Optional[str]

View File

@@ -159,7 +159,7 @@ async def upload_files_fake(
account_id: str, account_id: str,
# filename: Optional[str] = Form(...), # filename: Optional[str] = Form(...),
link_to_type: str, link_to_type: str,
link_to_id: str, link_to_id: Union[int, str],
check_allowed_extension: bool = False, check_allowed_extension: bool = False,
# create_hosted_file_link: bool = True, # create_hosted_file_link: bool = True,
x_account_id: str = Header(..., ), x_account_id: str = Header(..., ),
@@ -173,16 +173,15 @@ async def upload_files_fake(
log.debug(file_info_li) log.debug(file_info_li)
account_id_random = account_id # This is for the account random str ID # account_id_random = account_id # This is for the account random str ID
if account_id := redis_lookup_id_random(record_id_random=account_id, table_name='account'): pass if account_id := redis_lookup_id_random(record_id_random=account_id, table_name='account'): pass
else: else:
return mk_resp(data=None, status_code=400) return mk_resp(data=None, status_code=400, response=response, status_message='The Account ID was not found.')
link_to_type = link_to_type # link_to_id_random = link_to_id # This is for the object random str ID
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 if link_to_id := redis_lookup_id_random(record_id_random=link_to_id, table_name=link_to_type): pass
else: else:
return mk_resp(data=None, status_code=400) return mk_resp(data=None, status_code=400, response=response, status_message=f'The ID for linking was not found. Link To Type: {link_to_type} Link To ID: {link_to_id}')
hosted_file_list = [] hosted_file_list = []
for file_info in file_info_li: for file_info in file_info_li:
@@ -192,15 +191,34 @@ async def upload_files_fake(
if file_info['already_exists']: if file_info['already_exists']:
# Look up in DB based on hash # Look up in DB based on hash
# Get existing host_file object_entry and existing host_file.id_random. # Get existing host_file object_entry and existing host_file.id_random.
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.info('Look up in DB based on hash...') log.info('Look up in DB based on hash...')
if hosted_file_sel_result := sql_select( if hosted_file_sel_result := sql_select(
table_name = 'hosted_file', table_name = 'hosted_file',
field_name = 'hash_sha256', field_name = 'hash_sha256',
field_value = file_info['hash_sha256'], field_value = file_info['hash_sha256'],
): ):
# NOTE: Since the file already exists and something was in the database, it may need to be updated with the new subdirectory_path.
hosted_file_id = hosted_file_sel_result.get('id_random', None) hosted_file_id = hosted_file_sel_result.get('id_random', None)
# hosted_file_obj = Hosted_File_Base(**file_info)
hosted_file_dict = load_hosted_file_obj(hosted_file_id=hosted_file_id, model_as_dict=True) hosted_file_dict = load_hosted_file_obj(hosted_file_id=hosted_file_id, model_as_dict=True)
# ******************************************************
# New as of 2021-08-26
# NOTE: Working on moving all hosted files to subdirectories because there are a lot of files. The database needs to be updated if the file already exists and it does not exist in the new subdirectory.
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
if file_info['already_exists'] and not file_info['already_exists_subdir']: # This means the database record probably needs to be updated with the new subdirectory_path field.
subdirectory_path_from_passed_data = file_info['subdirectory_path']
log.info(f'The database record probably needs to be updated with the new subdirectory_path field. Subdirectory Path (from passed data): {subdirectory_path_from_passed_data}')
if subdirectory_path := hosted_file_dict.get('subdirectory_path'):
log.info(f'The new subdirectory_path field was found? Subdirectory Path: {subdirectory_path}')
else:
log.info(f'The new subdirectory_path field was not found? This needs to be updated. Subdirectory Path: {subdirectory_path}')
# ******************************************************
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(hosted_file_dict) log.debug(hosted_file_dict)
else: else:
@@ -219,7 +237,7 @@ async def upload_files_fake(
log.debug(hosted_file_obj_result) log.debug(hosted_file_obj_result)
log.debug(hosted_file_sel_result) log.debug(hosted_file_sel_result)
else: else:
# Just in case look up in DB based on hash # NOTE: Just in case look up in DB based on hash
log.info('Look up in DB based on hash...') log.info('Look up in DB based on hash...')
if hosted_file_sel_result := sql_select( if hosted_file_sel_result := sql_select(
table_name = 'hosted_file', table_name = 'hosted_file',
@@ -260,12 +278,12 @@ async def upload_files_fake(
log.debug(hosted_file_dict) log.debug(hosted_file_dict)
hosted_file_list.append(hosted_file_dict) hosted_file_list.append(hosted_file_dict)
# NOTE: Currently sql_insert does not handel all successful inserts correctly. If there is not an autonum ID then it will return 0 as the ID. # 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.
if create_hosted_file_link( if create_hosted_file_link(
account_id=account_id, account_id = account_id,
hosted_file_id=hosted_file_id, hosted_file_id = hosted_file_id,
link_to_type=link_to_type, link_to_type = link_to_type,
link_to_id=link_to_id, link_to_id = link_to_id,
): pass # This if statement should be improved ): pass # This if statement should be improved
else: else:
# This if statement should be improved # This if statement should be improved