'Upload and prepare an exported repository capsule for validation.'

def upload_capsule(self, **kwd):

    file_data = kwd.get('file_data', '')
    url = kwd.get('url', '')
    uploaded_file = None
    return_dict = dict(error_message='', encoded_file_path=None, status='ok', tar_archive=None, uploaded_file=None, capsule_file_name=None)
    if url:
        valid_url = True
        try:
            stream = urlopen(url)
        except Exception as e:
            valid_url = False
            return_dict['error_message'] = ('Error    importing    file    via    http:    %s' % str(e))
            return_dict['status'] = 'error'
            return return_dict
        if valid_url:
            (fd, uploaded_file_name) = tempfile.mkstemp()
            uploaded_file = open(uploaded_file_name, 'wb')
            while 1:
                chunk = stream.read(CHUNK_SIZE)
                if (not chunk):
                    break
                uploaded_file.write(chunk)
            uploaded_file.flush()
            uploaded_file_filename = url.split('/')[(-1)]
    elif (file_data not in ('', None)):
        uploaded_file = file_data.file
        uploaded_file_name = uploaded_file.name
        uploaded_file_filename = os.path.split(file_data.filename)[(-1)]
    if (uploaded_file is not None):
        if (os.path.getsize(os.path.abspath(uploaded_file_name)) == 0):
            uploaded_file.close()
            return_dict['error_message'] = 'Your    uploaded    capsule    file    is    empty.'
            return_dict['status'] = 'error'
            return return_dict
        try:
            tar_archive = tarfile.open(uploaded_file_name, 'r:*')
        except tarfile.ReadError as e:
            error_message = ('Error    opening    file    %s:    %s' % (str(uploaded_file_name), str(e)))
            log.exception(error_message)
            return_dict['error_message'] = error_message
            return_dict['status'] = 'error'
            uploaded_file.close()
            return return_dict
        if (not self.validate_archive_paths(tar_archive)):
            return_dict['status'] = 'error'
            return_dict['message'] = 'This    capsule    contains    an    invalid    member    type    or    a    file    outside    the    archive    path.'
            uploaded_file.close()
            return return_dict
        return_dict['tar_archive'] = tar_archive
        return_dict['capsule_file_name'] = uploaded_file_filename
        uploaded_file.close()
    else:
        return_dict['error_message'] = 'No    files    were    entered    on    the    import    form.'
        return_dict['status'] = 'error'
        return return_dict
    return return_dict
