Clean up of hosted_file
This commit is contained in:
@@ -37,13 +37,13 @@ async def create_upload_files(
|
|||||||
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=404)
|
return mk_resp(data=None, status_code=400)
|
||||||
|
|
||||||
for_object_type = for_object_type
|
for_object_type = for_object_type
|
||||||
for_object_id_random = for_object_id # This is for the object random str ID
|
for_object_id_random = for_object_id # This is for the object random str ID
|
||||||
if for_object_id := redis_lookup_id_random(record_id_random=for_object_id, table_name=for_object_type): pass
|
if for_object_id := redis_lookup_id_random(record_id_random=for_object_id, table_name=for_object_type): pass
|
||||||
else:
|
else:
|
||||||
return mk_resp(data=None, status_code=404)
|
return mk_resp(data=None, status_code=400)
|
||||||
|
|
||||||
# file_info_li = await save_file_li(file_list, for_object_type, for_object_id, for_object_id_random)
|
# file_info_li = await save_file_li(file_list, for_object_type, for_object_id, for_object_id_random)
|
||||||
|
|
||||||
@@ -56,97 +56,6 @@ async def create_upload_files(
|
|||||||
return mk_resp(data=file_info_list)
|
return mk_resp(data=file_info_list)
|
||||||
|
|
||||||
|
|
||||||
# This is not needed. Just for reference of the mk_resp() function
|
|
||||||
# @router.post('/', response_model=Resp_Body_Base)
|
|
||||||
# async def post_upload_file(
|
|
||||||
# account_id: str = Query(..., min_length=1, max_length=22),
|
|
||||||
# ):
|
|
||||||
# if return_obj:
|
|
||||||
# if order_cart_obj := load_order_cart_obj(order_cart_id=order_cart_id, inc_order_cart_line_li=inc_order_cart_line_li, inc_order_cart_cfg=inc_order_cart_cfg):
|
|
||||||
# data = order_cart_obj.dict(by_alias=True, exclude_unset=False)
|
|
||||||
# return mk_resp(data=data)
|
|
||||||
# else:
|
|
||||||
# return mk_resp(data=False, status_code=404) # Not Found
|
|
||||||
# else:
|
|
||||||
# return mk_resp(data=True)
|
|
||||||
|
|
||||||
|
|
||||||
async def save_file_li(
|
|
||||||
file_li: List[UploadFile], # = File(...),
|
|
||||||
account_id: int,
|
|
||||||
account_id_random: str,
|
|
||||||
for_object_type: str,
|
|
||||||
for_object_id: int,
|
|
||||||
for_object_id_random: str,
|
|
||||||
):
|
|
||||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
||||||
log.debug(locals())
|
|
||||||
|
|
||||||
hosted_file_path = '/home/scott/tmp/hosted_file_dev/'
|
|
||||||
|
|
||||||
log.debug(shutil.disk_usage(hosted_file_path))
|
|
||||||
|
|
||||||
file_info_li = []
|
|
||||||
for file in file_li.g:
|
|
||||||
log.debug(file)
|
|
||||||
log.debug(f'{file.filename}')
|
|
||||||
|
|
||||||
file_info = {}
|
|
||||||
file_info['for_object_type'] = for_object_type
|
|
||||||
file_info['for_object_id'] = for_object_id
|
|
||||||
file_info['for_object_id_random'] = for_object_id_random
|
|
||||||
file_info['filename'] = file.filename
|
|
||||||
|
|
||||||
# There is a difference between Content-Type and MIME type.
|
|
||||||
# https://stackoverflow.com/questions/3452381/whats-the-difference-of-contenttype-and-mimetype
|
|
||||||
file_info['content_type'] = file.content_type # might also include charset or other parameters
|
|
||||||
file_info['mimetype'] = file.mimetype
|
|
||||||
|
|
||||||
file.file.seek(0, os.SEEK_END)
|
|
||||||
file_size = file.file.tell()
|
|
||||||
file.file.seek(0) # The file will not properly save if seek is not reset to 0.
|
|
||||||
#file_info['size'] = request_file_size
|
|
||||||
log.debug(file_size)
|
|
||||||
file_info['size'] = file_size
|
|
||||||
|
|
||||||
file_hash = await get_file_object_hash(file.file)
|
|
||||||
log.debug(file_hash)
|
|
||||||
file_info['hash_sha256'] = file_hash
|
|
||||||
|
|
||||||
# 16384 bytes is the default
|
|
||||||
# 4096 8192 16384 32768 65536 131072 262144 524288 1048576 bytes
|
|
||||||
buffer_size = 524288
|
|
||||||
|
|
||||||
#file_src = file.filename
|
|
||||||
#f_src = open(file_src, 'rb')
|
|
||||||
f_src = file.file # Don't need to do open(file_src, 'rb') since it is already "open"
|
|
||||||
|
|
||||||
#file_dest = f'{hosted_file_path}{file.filename}'
|
|
||||||
file_dest = f'{hosted_file_path}{file_hash}.file'
|
|
||||||
|
|
||||||
existing_file_check = pathlib.Path(file_dest)
|
|
||||||
|
|
||||||
if existing_file_check.exists():
|
|
||||||
file_info['already_exists'] = True
|
|
||||||
file_info['copy_timer'] = 0
|
|
||||||
else:
|
|
||||||
file_info['already_exists'] = False
|
|
||||||
f_dest = open(file_dest, 'wb')
|
|
||||||
|
|
||||||
timer_start = time.process_time()
|
|
||||||
shutil.copyfileobj(f_src, f_dest, buffer_size)
|
|
||||||
timer_end = time.process_time()
|
|
||||||
elapsed_time = timer_end - timer_start
|
|
||||||
log.debug(f'Elapsed time: {elapsed_time}')
|
|
||||||
file_info['copy_timer'] = elapsed_time
|
|
||||||
# result.append({ 'filename': file.filename })
|
|
||||||
file_info_li.append(file_info)
|
|
||||||
|
|
||||||
log.debug(shutil.disk_usage(hosted_file_path))
|
|
||||||
|
|
||||||
return file_info_li
|
|
||||||
|
|
||||||
|
|
||||||
async def save_file(
|
async def save_file(
|
||||||
file: UploadFile,
|
file: UploadFile,
|
||||||
account_id: int,
|
account_id: int,
|
||||||
@@ -155,7 +64,7 @@ async def save_file(
|
|||||||
for_object_id: int,
|
for_object_id: int,
|
||||||
for_object_id_random: str,
|
for_object_id_random: str,
|
||||||
):
|
):
|
||||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||||
log.debug(locals())
|
log.debug(locals())
|
||||||
|
|
||||||
hosted_file_path = '/home/scott/tmp/hosted_file_dev/'
|
hosted_file_path = '/home/scott/tmp/hosted_file_dev/'
|
||||||
@@ -180,7 +89,6 @@ async def save_file(
|
|||||||
file.file.seek(0, os.SEEK_END)
|
file.file.seek(0, os.SEEK_END)
|
||||||
file_size = file.file.tell()
|
file_size = file.file.tell()
|
||||||
file.file.seek(0) # The file will not properly save if seek is not reset to 0.
|
file.file.seek(0) # The file will not properly save if seek is not reset to 0.
|
||||||
#file_info['size'] = request_file_size
|
|
||||||
log.debug(file_size)
|
log.debug(file_size)
|
||||||
file_info['size'] = file_size
|
file_info['size'] = file_size
|
||||||
|
|
||||||
@@ -192,7 +100,6 @@ async def save_file(
|
|||||||
# 4096 8192 16384 32768 65536 131072 262144 524288 1048576 bytes
|
# 4096 8192 16384 32768 65536 131072 262144 524288 1048576 bytes
|
||||||
buffer_size = 524288
|
buffer_size = 524288
|
||||||
|
|
||||||
#file_src = file.filename
|
|
||||||
#f_src = open(file_src, 'rb')
|
#f_src = open(file_src, 'rb')
|
||||||
f_src = file.file # Don't need to do open(file_src, 'rb') since it is already "open"
|
f_src = file.file # Don't need to do open(file_src, 'rb') since it is already "open"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user