Work on websockets end points and management
This commit is contained in:
@@ -20,7 +20,10 @@ router = APIRouter()
|
||||
|
||||
|
||||
# ### BEGIN ### API Hosted File ### directory_check() ###
|
||||
# Updated 2022-08-09
|
||||
# This can be used to clean up the hosted_files directory. Currently it only looks for hashed files in the root, but that is kind of useless now. 2023-03-28
|
||||
# This needs to be updated to delete orphan files (no records in the DB (dev, test, prod)). Careful...
|
||||
# I also need to clean up the DB side if there is no file in the hosted_files directory. Less concerning?
|
||||
# Updated 2023-03-28
|
||||
@router.get('/directory_check', response_model=Resp_Body_Base)
|
||||
async def directory_check(
|
||||
rm_orphan: bool = False,
|
||||
@@ -30,11 +33,6 @@ async def directory_check(
|
||||
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
# print('HERE HERE HERE')
|
||||
|
||||
# return mk_resp(data=True, response=commons.response, status_message='HERE HERE HERE The hosted file directory check.')
|
||||
|
||||
|
||||
# ### Orphan file: ### Delete file from server
|
||||
hosted_files_path = settings.FILES_PATH['hosted_files_root']
|
||||
# hosted_files_path = '/home/scott/tmp/hosted_files_dev/'
|
||||
@@ -49,12 +47,17 @@ async def directory_check(
|
||||
log.info('Path exists! Going to get a list of files...')
|
||||
directory_list = os.listdir(full_directory_path)
|
||||
|
||||
count = 0
|
||||
|
||||
result_list = []
|
||||
for directory_item in directory_list:
|
||||
if count >= 100: break
|
||||
|
||||
file_path_w_item = os.path.join(full_directory_path, directory_item)
|
||||
# log.info(f'Full file path with directory item: {file_path_w_item}')
|
||||
log.info(f'Checking directory item: {directory_item}')
|
||||
# log.info(f'Checking directory item: {directory_item}')
|
||||
if os.path.isfile(file_path_w_item):
|
||||
# ### Found file ###
|
||||
# log.debug(f'File: {directory_item}')
|
||||
# result_list.append(file_path_w_item)
|
||||
|
||||
@@ -63,66 +66,82 @@ async def directory_check(
|
||||
log.warning(f'Not a hashed file! File: {directory_item}')
|
||||
continue
|
||||
|
||||
if lookup_file_hash_result := lookup_file_hash(file_hash=directory_item.replace('.file', '')):
|
||||
# log.info('DB record found')
|
||||
# result_list.append(file_path_w_item)
|
||||
pass
|
||||
else:
|
||||
log.warning(f'Hosted File record not found!!! File: {directory_item}')
|
||||
result_list.append(file_path_w_item)
|
||||
if rm_orphan:
|
||||
log.info('Going remove the hosted file from server...')
|
||||
log.info(f'Hosted hashed file found: {directory_item}')
|
||||
result_list.append(file_path_w_item)
|
||||
|
||||
try:
|
||||
# log.warning('DELETE')
|
||||
pathlib.Path(file_path_w_item).unlink()
|
||||
# continue
|
||||
except OSError as e:
|
||||
log.error("Error: %s : %s" % (file_path, e.strerror))
|
||||
# return False
|
||||
continue
|
||||
# Create a subdirectory with the first 2 characters of the hash
|
||||
full_subdirectory_path = os.path.join(full_directory_path, directory_item[:2])
|
||||
log.info(f'Making directory: {full_subdirectory_path}')
|
||||
os.makedirs(full_subdirectory_path, exist_ok=True)
|
||||
|
||||
# Move the file to the subdirectory
|
||||
log.info(f'Moving to: {full_subdirectory_path}')
|
||||
shutil.move(os.path.join(full_directory_path, directory_item), os.path.join(full_subdirectory_path, directory_item))
|
||||
|
||||
# if lookup_file_hash_result := lookup_file_hash(file_hash=directory_item.replace('.file', '')):
|
||||
# log.info('DB record found')
|
||||
# # result_list.append(file_path_w_item)
|
||||
# pass
|
||||
# else:
|
||||
# log.warning(f'Hosted File record not found!!! File: {directory_item}')
|
||||
# result_list.append(file_path_w_item)
|
||||
# if rm_orphan:
|
||||
# log.info('Going remove the hosted file from server...')
|
||||
|
||||
# try:
|
||||
# # log.warning('DELETE')
|
||||
# pathlib.Path(file_path_w_item).unlink()
|
||||
# # continue
|
||||
# except OSError as e:
|
||||
# log.error("Error: %s : %s" % (file_path, e.strerror))
|
||||
# # return False
|
||||
# continue
|
||||
else:
|
||||
# ### Found directory ###
|
||||
# continue
|
||||
# log.debug(f'Directory: {directory_item}')
|
||||
# pass
|
||||
log.info('Subdirectory Path exists! Going to get a list of files...')
|
||||
full_subdirectory_path = os.path.join(full_directory_path, directory_item)
|
||||
subdirectory_list = os.listdir(full_subdirectory_path)
|
||||
log.info('Subdirectory Path exists! Going to get a list of files... [LATER]')
|
||||
# full_subdirectory_path = os.path.join(full_directory_path, directory_item)
|
||||
# subdirectory_list = os.listdir(full_subdirectory_path)
|
||||
|
||||
subdirectory_result_list = []
|
||||
for subdirectory_item in subdirectory_list:
|
||||
file_path_w_item = os.path.join(full_subdirectory_path, subdirectory_item)
|
||||
# log.info(f'Full file path with directory item: {file_path_w_item}')
|
||||
log.info(f'Checking subdirectory item: {subdirectory_item}')
|
||||
if os.path.isfile(file_path_w_item):
|
||||
# log.debug(f'File: {subdirectory_item}')
|
||||
# subdirectory_result_list.append(file_path_w_item)
|
||||
# subdirectory_result_list = []
|
||||
# for subdirectory_item in subdirectory_list:
|
||||
# file_path_w_item = os.path.join(full_subdirectory_path, subdirectory_item)
|
||||
# # log.info(f'Full file path with directory item: {file_path_w_item}')
|
||||
# log.info(f'Checking subdirectory item: {subdirectory_item}')
|
||||
# if os.path.isfile(file_path_w_item):
|
||||
# # log.debug(f'File: {subdirectory_item}')
|
||||
# # subdirectory_result_list.append(file_path_w_item)
|
||||
|
||||
if '.file' in subdirectory_item: pass
|
||||
else:
|
||||
log.warning(f'Not a hashed file! File: {subdirectory_item}')
|
||||
continue
|
||||
# if '.file' in subdirectory_item: pass
|
||||
# else:
|
||||
# log.warning(f'Not a hashed file! File: {subdirectory_item}')
|
||||
# continue
|
||||
|
||||
if lookup_file_hash_result := lookup_file_hash(file_hash=subdirectory_item.replace('.file', '')):
|
||||
# log.info('DB record found')
|
||||
# subdirectory_result_list.append(file_path_w_item)
|
||||
pass
|
||||
else:
|
||||
log.warning(f'Hosted File record not found!!! File: {subdirectory_item}')
|
||||
result_list.append(file_path_w_item)
|
||||
if rm_orphan:
|
||||
log.info('Going remove the hosted file from server...')
|
||||
# if lookup_file_hash_result := lookup_file_hash(file_hash=subdirectory_item.replace('.file', '')):
|
||||
# # log.info('DB record found')
|
||||
# # subdirectory_result_list.append(file_path_w_item)
|
||||
# pass
|
||||
# else:
|
||||
# log.warning(f'Hosted File record not found!!! File: {subdirectory_item}')
|
||||
# result_list.append(file_path_w_item)
|
||||
# if rm_orphan:
|
||||
# log.info('Going remove the hosted file from server...')
|
||||
|
||||
try:
|
||||
# log.warning('DELETE')
|
||||
pathlib.Path(file_path_w_item).unlink()
|
||||
# continue
|
||||
except OSError as e:
|
||||
log.error("Error: %s : %s" % (file_path, e.strerror))
|
||||
# return False
|
||||
continue
|
||||
else:
|
||||
log.warning(f'Subdirectory: {subdirectory_item}')
|
||||
pass
|
||||
# try:
|
||||
# # log.warning('DELETE')
|
||||
# pathlib.Path(file_path_w_item).unlink()
|
||||
# # continue
|
||||
# except OSError as e:
|
||||
# log.error("Error: %s : %s" % (file_path, e.strerror))
|
||||
# # return False
|
||||
# continue
|
||||
# else:
|
||||
# log.warning(f'Subdirectory: {subdirectory_item}')
|
||||
# pass
|
||||
|
||||
count = count + 1
|
||||
|
||||
return mk_resp(data=result_list, response=commons.response, status_message='The hosted file directory check.')
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user