From 09e3f29501815e1c8de753259c128f595881f8c1 Mon Sep 17 00:00:00 2001 From: Scott Idem Date: Tue, 9 Aug 2022 16:08:50 -0400 Subject: [PATCH] Too many changes. Work on file uploads, hosted files, deletion of links, records, and stored files. --- app/methods/hosted_file_methods.py | 196 ++++++++++++++++++++++------ app/models/hosted_file_models.py | 1 + app/routers/hosted_file.py | 203 ++++++++++++++++------------- app/routers/site_domain.py | 3 +- 4 files changed, 274 insertions(+), 129 deletions(-) diff --git a/app/methods/hosted_file_methods.py b/app/methods/hosted_file_methods.py index 3d5a988..6a7f126 100644 --- a/app/methods/hosted_file_methods.py +++ b/app/methods/hosted_file_methods.py @@ -4,15 +4,17 @@ from fastapi import File, UploadFile from typing import Dict, List, Optional, Set, Union from pydantic import BaseModel, EmailStr, Field, PrivateAttr, ValidationError, validator -from app.db_sql import redis_lookup_id_random, sql_enable_part, sql_insert, sql_limit_offset_part, sql_select, sql_update +from app.config import settings +from app.db_sql import redis_lookup_id_random, sql_delete, sql_enable_part, sql_insert, sql_limit_offset_part, sql_select, sql_update from app.lib_general import log, logging, logger_reset from app.models.hosted_file_models import Hosted_File_Base # ### BEGIN ### API Hosted File Methods ### create_hosted_file_obj() ### +@logger_reset def create_hosted_file_obj(hosted_file_obj_new:Hosted_File_Base): - log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL + log.setLevel(logging.INFO) # 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={'created_on', 'updated_on'}) @@ -32,6 +34,7 @@ def create_hosted_file_obj(hosted_file_obj_new:Hosted_File_Base): # ### BEGIN ### API Hosted File Methods ### load_hosted_file_obj() ### +@logger_reset def load_hosted_file_obj( hosted_file_id: int|str, limit: int = 1000, @@ -47,19 +50,19 @@ def load_hosted_file_obj( if hosted_file_id := redis_lookup_id_random(record_id_random=hosted_file_id, table_name='hosted_file'): pass else: return False - if hosted_file_rec := sql_select(table_name='v_hosted_file', record_id=hosted_file_id): - log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL - log.debug(hosted_file_rec) - else: - return False + if hosted_file_rec := sql_select(table_name='v_hosted_file', record_id=hosted_file_id): pass + elif hosted_file_rec is None: return None + else: return False + + log.debug(hosted_file_rec) try: hosted_file_obj = Hosted_File_Base(**hosted_file_rec) - log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL - log.debug(hosted_file_obj) except ValidationError as e: log.error(e.json()) return False + # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL + log.debug(hosted_file_obj) # if inc_x: # x_id = hosted_file_rec.get('x_id', None) @@ -76,6 +79,7 @@ def load_hosted_file_obj( # ### BEGIN ### API Hosted File Route ### get_file_object_hash() ### +@logger_reset async def get_file_object_hash(file_object:File): #log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) @@ -113,9 +117,9 @@ def allowed_file_extension(extension: str, extension_list: list): - - # ### BEGIN ### API Hosted File Route ### save_file() ### +# Updated 2022-08-09 +@logger_reset async def save_file( file: UploadFile, account_id: int, @@ -125,11 +129,11 @@ async def save_file( link_to_id_random: str, check_allowed_extension: bool = False, ): - # log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL + log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) - # hosted_files_path = settings.FILES_PATH['hosted_files_root'] - hosted_files_path = '/home/scott/tmp/hosted_files_dev/' + 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}') log.debug(shutil.disk_usage(hosted_files_path)) @@ -290,6 +294,8 @@ async def save_file( # ### BEGIN ### API Hosted File Methods ### create_hosted_file_link() ### +# Updated 2022-08-09 +@logger_reset def create_hosted_file_link( account_id: int|str, hosted_file_id: int|str, @@ -334,9 +340,9 @@ def create_hosted_file_link( # ### BEGIN ### API Hosted File Methods ### delete_hosted_file() ### -# Updated 2022-08-08 +# Updated 2022-08-09 @logger_reset -def delete_hosted_file( +def handle_delete_hosted_file( account_id: int|str, hosted_file_id: int|str, @@ -346,39 +352,147 @@ def delete_hosted_file( rm_all_links: bool = False, rm_orphan: bool = False, ): - log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL + log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) - # if account_id := redis_lookup_id_random(record_id_random=account_id, table_name='account'): pass - # else: return False + if account_id := redis_lookup_id_random(record_id_random=account_id, table_name='account'): pass + else: return False if hosted_file_id := redis_lookup_id_random(record_id_random=hosted_file_id, table_name='hosted_file'): pass else: return False + # ### 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 link_to_type and link_to_id: + if hosted_file_link_result := delete_hosted_file_link( + 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, + ): + log.info('The hosted file link record was deleted.') + elif hosted_file_link_result is None: + log.warning('The hosted file link record was not found and may have already been deleted. Odd, but this can happen.') + # return None + else: + log.error('Something went wrong while trying to delete the hosted file link record.') + return False + + # ### 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('Removed hosted file link. No orphan check.') + return True + + if hosted_file_obj := load_hosted_file_obj( + hosted_file_id = hosted_file_id, + # inc_hosted_file = True, + inc_hosted_file_link_list = True, # if rm_orphan (True) then need to include hosted_file_link_list (True) + ): + log.info('Hosted File object loaded.') + pass + elif hosted_file_obj is None: + log.warning('Hosted File object not found. Can not attempt to delete file from the server if there is one.') + # pass + return None + else: + log.error('Something went wrong while trying to load the Hosted File object.') + return False + log.debug(hosted_file_obj) + + # NOTE: Check and remove orphan + if hosted_file_link_rec_list_result := get_hosted_file_link_rec_list(hosted_file_id=hosted_file_id): + log.info('This hosted file has linked records to it.') + 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_list = hosted_file_link_result_list + # NOT safe to delete the hosted_file record and file from server!!! + # STOP! + log.info('Removed hosted file link (above). Still not an orphan file.') + return True + elif isinstance(hosted_file_link_rec_list_result, list) or hosted_file_link_rec_list_result is None: + log.info('This hosted file has no link records to it.') + hosted_file_list = [] + # Safe to delete the hosted_file record and file from server??? + # CONTINUE + else: + hosted_file_list = False + # Safe to delete the hosted_file record and file from server??? + # CONTINUE??? + log.error('Something went wrong while trying to get a list of the hosted file link records.') + return False + + # ### Orphan file: ### Delete file from server + 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}') + + 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) + 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!') + 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 False + pass + # return True + else: + log.warning(f'The hosted file was not found on the server. Hash: {hash_sha256}') + pass + # return None + + # ### Orphan file: ### Delete hosted_file record sql = f""" DELETE FROM hosted_file - WHERE hosted_file_id = :hosted_file_id + WHERE hosted_file.id = :hosted_file_id """ + log.debug(sql) + hosted_file_data = {} hosted_file_data['hosted_file_id'] = hosted_file_id - # hosted_file_data['link_to_type'] = link_to_type - # hosted_file_data['link_to_id'] = link_to_id log.debug(hosted_file_data) if hosted_file_delete_result := sql_delete(sql=sql, data=hosted_file_data): - log.info(f'Deleted Hosted File Link. Hosted File ID: {hosted_file_id}') + log.info(f'Deleted Hosted File record. Hosted File ID: {hosted_file_id}') + return True elif hosted_file_delete_result is None: + log.warning(f'Hosted File record was not found and may have already been removed. Hosted File ID: {hosted_file_id}') return None + # pass else: + log.error('Something went wrong while trying to delete the hosted file record.') return False - - return True -# ### END ### API Hosted File Methods ### delete_hosted_file() ### +# ### END ### API Hosted File Methods ### handle_delete_hosted_file() ### # ### BEGIN ### API Hosted File Methods ### delete_hosted_file_link() ### -# Updated 2022-08-08 +# Updated 2022-08-09 @logger_reset def delete_hosted_file_link( account_id: int|str, @@ -387,9 +501,9 @@ def delete_hosted_file_link( link_to_type: str, link_to_id: int|str, - rm_orphan: bool = False, + # rm_orphan: bool = False, ): - log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL + log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) # if account_id := redis_lookup_id_random(record_id_random=account_id, table_name='account'): pass @@ -407,6 +521,8 @@ def delete_hosted_file_link( AND link_to_type = :link_to_type AND link_to_id = :link_to_id """ + log.debug(sql) + hosted_file_link_data = {} hosted_file_link_data['hosted_file_id'] = hosted_file_id hosted_file_link_data['link_to_type'] = link_to_type @@ -427,6 +543,8 @@ def delete_hosted_file_link( # ### BEGIN ### API Hosted File Methods ### get_hosted_file_rec_list() ### # This needs to be improved. Currently it does not really do anything. # Need to allow for list by account? Probably have the same actual hosted file have two hosted_file entries if it was uploaded for two separate accounts. +# Updated 2022 +@logger_reset def get_hosted_file_rec_list( for_obj_type: str, for_obj_id: str, @@ -481,6 +599,8 @@ def get_hosted_file_rec_list( # ### BEGIN ### API Hosted File Methods ### get_hosted_file_link_rec_list() ### +# Updated 2022-08-09 +@logger_reset def get_hosted_file_link_rec_list( hosted_file_id: int|str, @@ -501,19 +621,19 @@ def get_hosted_file_link_rec_list( sql = f""" SELECT * - FROM `v_hosted_file` AS `hosted_file` + FROM `hosted_file_link` AS `hosted_file_link` WHERE - `hosted_file`.hosted_file_id = :hosted_file_id - ORDER BY `hosted_file`.created_on DESC, `hosted_file`.updated_on DESC + `hosted_file_link`.hosted_file_id = :hosted_file_id + ORDER BY `hosted_file_link`.created_on DESC, `hosted_file_link`.updated_on DESC {sql_limit}; """ - if hosted_file_rec_li_result := sql_select(data=data, sql=sql, as_list=True): - hosted_file_rec_li = hosted_file_rec_li_result + if hosted_file_link_rec_li_result := sql_select(data=data, sql=sql, as_list=True): + hosted_file_link_rec_li = hosted_file_link_rec_li_result else: - hosted_file_rec_li = [] + hosted_file_link_rec_li = [] log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL - log.debug(hosted_file_rec_li_result) + log.debug(hosted_file_link_rec_li_result) - return hosted_file_rec_li -# ### END ### API Hosted File Methods ### get_hosted_file_rec_list() ### + return hosted_file_link_rec_li +# ### END ### API Hosted File Methods ### get_hosted_file_link_rec_list() ### diff --git a/app/models/hosted_file_models.py b/app/models/hosted_file_models.py index dcb4f49..d9fd3d8 100644 --- a/app/models/hosted_file_models.py +++ b/app/models/hosted_file_models.py @@ -88,5 +88,6 @@ class Hosted_File_Base(BaseModel): class Config: underscore_attrs_are_private = True + allow_population_by_field_name = True fields = base_fields # ### END ### API Hosted File Models ### Hosted_File_Base() ### diff --git a/app/routers/hosted_file.py b/app/routers/hosted_file.py index 609559d..dca0e0c 100644 --- a/app/routers/hosted_file.py +++ b/app/routers/hosted_file.py @@ -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() ### diff --git a/app/routers/site_domain.py b/app/routers/site_domain.py index 8586160..719bcf9 100644 --- a/app/routers/site_domain.py +++ b/app/routers/site_domain.py @@ -4,10 +4,9 @@ from fastapi import APIRouter, Body, Depends, Header, HTTPException, Query, Resp from pydantic import BaseModel, EmailStr, Field from typing import Dict, List, Optional, Set, Union -from app.lib_general import log, logging, common_route_params, Common_Route_Params, common_route_params_min, Common_Route_Params_Min -#from ..log import * from app.config import settings from app.db_sql import sql_enable_part, sql_insert, sql_update, sql_insert_or_update, sql_limit_offset_part, sql_select, sql_delete, redis_lookup_id_random +from app.lib_general import log, logging, common_route_params, Common_Route_Params, common_route_params_min, Common_Route_Params_Min from .api_crud import delete_obj_template, get_obj_template, get_obj_li_template, patch_obj_template, post_obj_template