def finalize_download(url, download_to_file, content_type, request):
<EOL>if content_type and content_type not in request.headers['<STR_LIT>']:<EOL><INDENT>msg = '<STR_LIT>'<EOL>raise InvenioFileDownloadError(msg)<EOL><DEDENT>to_file = open(download_to_file, '<STR_LIT:w>')<EOL>try:<EOL><INDENT>try:<EOL><INDENT>while True:<EOL><INDENT>block = request.read(CFG_FILEUTILS_BLOCK_SIZE)<EOL>if not block:<EOL><INDENT>break<EOL><DEDENT>to_file.write(block)<EOL><DEDENT><DEDENT>except Exception as e:<EOL><INDENT>msg = "<STR_LIT>" %(url, download_to_file, e)<EOL>raise InvenioFileDownloadError(msg)<EOL><DEDENT><DEDENT>finally:<EOL><INDENT>to_file.close()<EOL><DEDENT>filesize = os.path.getsize(download_to_file)<EOL>if filesize == <NUM_LIT:0>:<EOL><INDENT>raise InvenioFileDownloadError("<STR_LIT>" % (url,))<EOL><DEDENT>return download_to_file<EOL>
Finalizes the download operation by doing various checks, such as format
type, size check etc.