File uploads using mk_resp() to return the data.

This commit is contained in:
Scott Idem
2021-05-22 13:28:20 -04:00
parent 0f886374cf
commit 3afb81d801

View File

@@ -34,25 +34,35 @@ async def create_upload_files(
exclude_unset: Optional[bool] = True, exclude_unset: Optional[bool] = True,
): ):
response_data = {} data = {}
response_data['account_id'] = account_id data['account_id'] = account_id
response_data['filename'] = filename #data['filename'] = filename
response_data['object_type'] = object_type data['object_type'] = object_type
response_data['object_id'] = object_id data['object_id'] = object_id
data['file_li'] = []
log.debug(await save_file_li(files)) log.debug(await save_file_li(files))
response_li = [] response_li = []
for file in files: for file in files:
response_detailed = {} file_info = {}
response_detailed['filename'] = file.filename file_info['filename'] = file.filename
response_detailed['content_type'] = file.content_type file_info['content_type'] = file.content_type
response_li.append(response_detailed)
response = { 'response_data': response_data, 'response_li':response_li } 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'] = file_size
log.debug(file_size)
data['file_li'].append(file_info)
return mk_resp(data=data)
#response = { 'response_data': response_data, 'response_li':response_li }
#response['filenames'] = [file.filename for file in files] #response['filenames'] = [file.filename for file in files]
return response #return response
return {'filenames': [file.filename for file in files]} #return {'filenames': [file.filename for file in files]}
# This is not needed. Just for reference of the mk_resp() function # This is not needed. Just for reference of the mk_resp() function
@@ -81,7 +91,13 @@ async def save_file_li(file_li: List[UploadFile] = File(...)):
result = [] result = []
for file in file_li: for file in file_li:
log.debug(f'{file.filename} | {file.spool_max_size}') log.debug(f'{file.filename}')
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_hash = await get_file_object_hash(file.file) file_hash = await get_file_object_hash(file.file)
log.debug(file_hash) log.debug(file_hash)