Too many changes. Work on file uploads, hosted files, deletion of links, records, and stored files.

This commit is contained in:
Scott Idem
2022-08-09 16:08:50 -04:00
parent fac9ccad75
commit 09e3f29501
4 changed files with 274 additions and 129 deletions

View File

@@ -10,7 +10,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, load_hosted_file_obj, save_file, create_hosted_file_link, delete_hosted_file_link, get_hosted_file_link_rec_list
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
from app.models.hosted_file_models import Hosted_File_Base
from app.models.response_models import Resp_Body_Base, mk_resp
@@ -166,10 +166,13 @@ async def upload_files(
log.warning(f'The new subdirectory_path field was not found in the database record or the passed file info.')
# ******************************************************
else:
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
# NOTE: 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
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
@@ -197,6 +200,8 @@ async def upload_files(
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
@@ -511,104 +516,124 @@ async def delete_hosted_file(
if hosted_file_id := redis_lookup_id_random(record_id_random=hosted_file_id, table_name='hosted_file'): pass
else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The hosted_file ID was invalid or not found.')
# ### SECTION ### Handle links NOTE NOTE NOTE NOTE NOTE NOTE
# NOTE: If link_to_type and link_to_id passed then try and remove that link record first.
if hosted_file_delete_result := handle_delete_hosted_file(account_id=commons.x_account_id, hosted_file_id=hosted_file_id, link_to_type=link_to_type, link_to_id=link_to_id, rm_orphan=rm_orphan):
return mk_resp(data=True, response=commons.response, status_message='The hosted file link was deleted. Not an orphan file.')
elif hosted_file_delete_result is None:
log.warning(f'The file and or hosted file record may have already been deleted. Hosted File ID: {hosted_file_id}')
return mk_resp(data=None, status_code=404, response=commons.response, status_message='The file and or hosted file record may have already been deleted.') # Not Found (maybe sort of...)
else:
log.error(f'Something may have gone wrong while trying to delete the hosted file from the server or the hosted_file record.')
return mk_resp(data=False, status_code=400, response=commons.response, status_message='Something may have gone wrong while trying to delete the hosted file from the server or the hosted_file record.') # Bad Request
# if hosted_file_result := delete_hosted_file_link(
# account_id = commons.x_account_id,
# # ### SECTION ### Handle links NOTE NOTE NOTE NOTE NOTE NOTE
# # NOTE: If link_to_type and link_to_id passed then try and remove that link record first.
# # if hosted_file_result := delete_hosted_file_link(
# # account_id = commons.x_account_id,
# # hosted_file_id = hosted_file_id,
# # link_to_type = link_to_type,
# # link_to_id = link_to_id,
# # rm_orphan = rm_orphan,
# # ):
# # pass
# # elif hosted_file_result is None:
# # log.error('The hosted file link record was not found and may have already been deleted.')
# # return mk_resp(data=False, status_code=404, response=commons.response) # Not Found
# # else:
# # log.error('Something went wrong while trying to delete the hosted file link record.')
# # return mk_resp(data=False, status_code=400, response=commons.response) # Bad Request
# # ### SECTION ### Handle orphan check and deletion of hosted_file record and file on server NOTE NOTE NOTE NOTE NOTE NOTE
# # NOTE: If not rm_orphan then do nothing else.
# # NOTE: If rm_orphan then get list of links for file.
# # NOTE: If 0 links result then delete the hosted_file record and file on the server.
# # NOTE: If >0 links result then do nothing else.
# # NOTE: Don't check or remove orphan
# if not rm_orphan:
# log.info('Remove hosted file link. No orphan check.')
# return mk_resp(data=True, response=commons.response, status_message='The hosted file file link was deleted. No orphan check.')
# if hosted_file_delete_result := handle_delete_hosted_file(account_id=account_id, hosted_file_id=hosted_file_id, link_to_type=link_to_type, link_to_id=link_to_id, rm_orphan=rm_orphan):
# return mk_resp(data=True, response=commons.response, status_message='The hosted file link was deleted. Not an orphan file.')
# else:
# pass
# # # NOTE: Check and remove orphan
# # if hosted_file_link_rec_list_result := get_hosted_file_link_rec_list(hosted_file_id=hosted_file_id):
# # hosted_file_link_result_list = []
# # for hosted_file_link_rec in hosted_file_link_rec_list_result:
# # hosted_file_link_result_list.append(hosted_file_link_rec)
# # log.debug(hosted_file_link_result_list)
# # hosted_file_list = hosted_file_link_result_list
# # # NOT safe to delete the hosted_file record and file from server!!!
# # # STOP!
# # log.info('Remove hosted file link. Not an orphan file.')
# # return mk_resp(data=True, response=commons.response, status_message='The hosted file link was deleted. Not an orphan file.')
# # elif isinstance(hosted_file_link_rec_list_result, list) or hosted_file_link_rec_list_result is None:
# # hosted_file_list = []
# # # Safe to delete the hosted_file record and file from server???
# # # CONTINUE
# # delete_hosted_file(hosted_file_id=hosted_file_id)
# # else:
# # hosted_file_list = False
# # # Safe to delete the hosted_file record and file from server???
# # # CONTINUE
# # hosted_files_path = settings.FILES_PATH['hosted_files_root']
# hosted_files_path = '/home/scott/tmp/hosted_files_dev/'
# log.info(f'Hosted Files Path: {hosted_files_path}')
# if hosted_file_obj := load_hosted_file_obj(
# hosted_file_id = hosted_file_id,
# link_to_type = link_to_type,
# link_to_id = link_to_id,
# rm_orphan = rm_orphan,
# # inc_hosted_file = True,
# inc_hosted_file_link_list = rm_orphan, # if rm_orphan then need to include hosted_file_link_list
# ):
# pass
# elif hosted_file_result is None:
# log.error('The hosted file link record was not found and may have already been deleted.')
# return mk_resp(data=False, status_code=404, response=commons.response) # Not Found
# else:
# log.error('Something went wrong while trying to delete the hosted file link record.')
# return mk_resp(data=False, status_code=400, response=commons.response) # Bad Request
# ### SECTION ### Handle orphan check and deletion of hosted_file record and file on server NOTE NOTE NOTE NOTE NOTE NOTE
# NOTE: If not rm_orphan then do nothing else.
# NOTE: If rm_orphan then get list of links for file.
# NOTE: If 0 links result then delete the hosted_file record and file on the server.
# NOTE: If >0 links result then do nothing else.
# # if not filename:
# # filename = hosted_file_obj.filename
# # log.info(f'Filename: {filename}')
# dir_path = hosted_file_obj.directory_path
# subdir_path = hosted_file_obj.subdirectory_path
# hash_sha256 = hosted_file_obj.hash_sha256
# hash_filename = hash_sha256+'.file'
# NOTE: Don't check or remove orphan
if not rm_orphan:
log.info('Remove hosted file link. No orphan check.')
return mk_resp(data=True, response=commons.response, status_message='The hosted file file link was deleted. No orphan check.')
# if subdir_path:
# full_subdirectory_path = os.path.join(hosted_files_path, subdir_path)
# else:
# full_subdirectory_path = hosted_files_path
# log.debug(full_subdirectory_path)
# # pathlib.Path(full_subdirectory_path).mkdir(parents=True, exist_ok=True)
# file_path_w_subdir = os.path.join(full_subdirectory_path, hash_filename)
# log.info(f'Full file path with subdirectory: {file_path_w_subdir}')
# NOTE: Check and remove orphan
if hosted_file_rec_list_result := get_hosted_file_link_rec_list(hosted_file_id=hosted_file_id):
hosted_file_result_list = []
for hosted_file_rec in hosted_file_rec_list_result:
hosted_file_result_list.append(hosted_file_rec)
# if os.path.exists(file_path_w_subdir):
# log.info('File exists!')
# if rm_orphan:
# log.info('Going remove the file if it is an orphan...')
# # try:
# # pathlib.Path(file_path_w_subdir).unlink()
# # except OSError as e:
# # log.error("Error: %s : %s" % (file_path, e.strerror))
# # return mk_resp(data=False, status_code=400, response=commons.response, status_message='Something went wrong while trying to delete the hosted file from the server.') # Bad Request
# return mk_resp(data=True, response=commons.response, status_message='The orphan hosted file was deleted from the server.')
# else:
# log.info('Remove hosted file link. No orphan check.')
# return mk_resp(data=True, response=commons.response, status_message='The hosted file file link was deleted. No orphan check.')
log.debug(hosted_file_result_list)
hosted_file_list = hosted_file_result_list
# NOT safe to delete the hosted_file record and file from server!!!
# STOP!
elif isinstance(hosted_file_rec_list_result, list):
hosted_file_list = []
# Safe to delete the hosted_file record and file from server???
# CONTINUE
else:
hosted_file_list = None
# Safe to delete the hosted_file record and file from server???
# CONTINUE
# hosted_files_path = settings.FILES_PATH['hosted_files_root']
hosted_files_path = '/home/scott/tmp/hosted_files_dev/'
log.info(f'Hosted Files Path: {hosted_files_path}')
if hosted_file_obj := load_hosted_file_obj(
hosted_file_id = hosted_file_id,
# inc_hosted_file = True,
inc_hosted_file_link_list = rm_orphan, # if rm_orphan then need to include hosted_file_link_list
):
pass
else:
return mk_resp(data=False, status_code=400, response=commons.response) # Bad Request
# if not filename:
# filename = hosted_file_obj.filename
# log.info(f'Filename: {filename}')
dir_path = hosted_file_obj.directory_path
subdir_path = hosted_file_obj.subdirectory_path
hash_sha256 = hosted_file_obj.hash_sha256
hash_filename = hash_sha256+'.file'
if subdir_path:
full_subdirectory_path = os.path.join(hosted_files_path, subdir_path)
else:
full_subdirectory_path = hosted_files_path
log.debug(full_subdirectory_path)
# pathlib.Path(full_subdirectory_path).mkdir(parents=True, exist_ok=True)
file_path_w_subdir = os.path.join(full_subdirectory_path, hash_filename)
log.info(f'Full file path with subdirectory: {file_path_w_subdir}')
if os.path.exists(file_path_w_subdir):
log.info('File exists!')
if rm_orphan:
log.info('Going remove the file if it is an orphan...')
# try:
# pathlib.Path(file_path_w_subdir).unlink()
# except OSError as e:
# log.error("Error: %s : %s" % (file_path, e.strerror))
# return mk_resp(data=False, status_code=400, response=commons.response, status_message='Something went wrong while trying to delete the hosted file from the server.') # Bad Request
return mk_resp(data=True, response=commons.response, status_message='The orphan hosted file was deleted from the server.')
else:
log.info('Remove hosted file link. No orphan check.')
return mk_resp(data=True, response=commons.response, status_message='The hosted file file link was deleted. No orphan check.')
else:
log.error(f'The hosted file was not found on the server. Hash: {hash_sha256}')
return mk_resp(data=False, status_code=400, response=commons.response, status_message='The hosted file was not found on the server.') # Bad Request
# else:
# log.error(f'The hosted file was not found on the server. Hash: {hash_sha256}')
# return mk_resp(data=False, status_code=400, response=commons.response, status_message='The hosted file was not found on the server.') # Bad Request
# ### END ### API Hosted File ### download_hosted_file() ###