File uploads seem to be working well so far. Need to use mk_resp() to return the data in the standard way.

This commit is contained in:
Scott Idem
2021-05-22 04:38:50 -04:00
parent ada4a75ecb
commit 0f886374cf

View File

@@ -18,12 +18,27 @@ from ..models.response_model import *
router = APIRouter()
# This just needs to return the currect model for a hosted_file
# Everything else seems to be workign well
# Should this also do something with meta data and updating the DB?
@router.post('/upload_files/')
async def create_upload_files(files: List[UploadFile] = File(...), field_1: str = Form(...), field_2: str = Form(...)):
async def create_upload_files(
files: List[UploadFile] = File(...),
account_id: str = Form(..., min_length=1, max_length=22),
filename: Optional[str] = Form(...),
object_type: Optional[str] = Form(...),
object_id: Optional[str] = Form(..., min_length=1, max_length=22),
x_account_id: Optional[str] = Header(..., ),
return_obj: Optional[bool] = True,
by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True,
):
response_data = {}
response_data['field_1'] = field_1
response_data['field_2'] = field_2
response_data['account_id'] = account_id
response_data['filename'] = filename
response_data['object_type'] = object_type
response_data['object_id'] = object_id
log.debug(await save_file_li(files))
@@ -40,42 +55,11 @@ async def create_upload_files(files: List[UploadFile] = File(...), field_1: str
return {'filenames': [file.filename for file in files]}
@router.post("/upload_files_form/")
async def create_upload_files_form(
file: bytes = File(...), fileb: UploadFile = File(...), token: str = Form(...)
):
return {
"file_size": len(file),
"token": token,
"fileb_content_type": fileb.content_type,
}
# 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),
hosted_file_obj: Hosted_File_Base = None,
x_account_id: Optional[str] = Header(..., ),
return_obj: Optional[bool] = True,
by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True,
):
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
log.debug(order_cart_obj)
order_cart_obj_up_result = update_order_cart_obj(order_cart_obj=order_cart_obj, repl_order_cart_line_li=repl_order_cart_line_li)
if isinstance(order_cart_obj_up_result, int):
log.info(f'Order cart update and the result was an int: {order_cart_obj_up_result}')
pass
elif isinstance(order_cart_obj_up_result, bool) and order_cart_obj_up_result:
log.info(f'Order cart update and the result was an bool: {order_cart_obj_up_result}')
pass
elif isinstance(order_cart_obj_up_result, bool) and not order_cart_obj_up_result:
log.error(f'Order cart update and the result was an bool: {order_cart_obj_up_result}')
return mk_resp(data=False, status_code=500) # Internal Server Error
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)
@@ -86,12 +70,6 @@ async def post_upload_file(
return mk_resp(data=True)
async def save_file(file: UploadFile = File(...)):
with open(file.filename, 'wb') as buffer:
shutil.copyfileobj(file.file, buffer)
return {'filename': file.filename}
async def save_file_li(file_li: List[UploadFile] = File(...)):
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
@@ -108,7 +86,9 @@ async def save_file_li(file_li: List[UploadFile] = File(...)):
file_hash = await get_file_object_hash(file.file)
log.debug(file_hash)
buffer_size = 16000
# 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')
@@ -118,10 +98,11 @@ async def save_file_li(file_li: List[UploadFile] = File(...)):
file_dest = f'{hosted_file_path}{file_hash}.file'
f_dest = open(file_dest, 'wb')
timer_start = time.process_time()
shutil.copyfileobj(f_src, f_dest, buffer_size)
#with open(file.filename, 'wb') as file_dest:
# shutil.copyfileobj(file.file, file_dest, buffer_size)
timer_end = time.process_time()
elapsed_time = timer_end - timer_start
log.debug(f'Elapsed time: {elapsed_time}')
result.append({ 'filename': file.filename })
@@ -131,25 +112,34 @@ async def save_file_li(file_li: List[UploadFile] = File(...)):
async def get_file_object_hash(file_object):
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
#log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
# 4096 bytes is the current block size on my workstation and Linode server
block_size = 131072 # 4096 8192 16384 32768 65536 bytes
hash_md5 = hashlib.sha256()
# 4096 8192 16384 32768 65536 131072 262144 524288 1048576 bytes
block_size = 131072
hash_value = hashlib.sha256()
timer_start = time.process_time()
for chunk in iter(lambda: file_object.read(block_size), b""):
hash_md5.update(chunk)
file_hash = hash_md5.hexdigest()
hash_value.update(chunk)
file_hash = hash_value.hexdigest()
file_object.seek(0) # The file will not properly save if seek is not reset to 0.
timer_end = time.process_time()
elapsed_time = timer_end - timer_start
log.debug(f'Elapsed time: {elapsed_time}')
#log.debug(f'Elapsed time: {elapsed_time}')
return file_hash
# def allowed_file_extension(filename):
# return False
# return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
# def guess_file_extension(filename):
# return filename.rsplit('.', 1)[1].lower()
# def copyLargeFile(src, dest, buffer_size=16000):
# with open(src, 'rb') as fsrc:
# with open(dest, 'wb') as fdest: