(self, file_path)
Upload binary data to Timesketch, potentially chunking it up.

        Args:
            file_path: a full path to the file that is about to be uploaded.
Upload binary data to Timesketch, potentially chunking it up.
Upload binary data to Timesketch potentially chunking it up .
def _upload_binary_file(self, file_path):
        """Upload binary data to Timesketch, potentially chunking it up.

        Args:
            file_path: a full path to the file that is about to be uploaded.
        """
        file_size = os.path.getsize(file_path)

        if self._timeline_name:
            timeline_name = self._timeline_name
        else:
            file_name = os.path.basename(file_path)
            file_name_no_ext, _, _ = file_name.rpartition('.')
            timeline_name = file_name_no_ext

        data = {
            'name': timeline_name,
            'sketch_id': self._sketch.id,
            'total_file_size': file_size,
            'provider': self._provider,
            'data_label': self._data_label,
        }
        if self._index:
            data['index_name'] = self._index

        if self._upload_context:
            data['context'] = self._upload_context

        if file_size <= self._threshold_filesize:
            file_dict = {
                'file': open(file_path, 'rb')}
            response = self._sketch.api.session.post(
                self._resource_url, files=file_dict, data=data)
        else:
            chunks = int(
                math.ceil(float(file_size) / self._threshold_filesize))
            data['chunk_total_chunks'] = chunks
            data['chunk_index_name'] = uuid.uuid4().hex

            for index in range(0, chunks):
                data['chunk_index'] = index
                start = self._threshold_filesize * index
                data['chunk_byte_offset'] = start
                fh = open(file_path, 'rb')
                fh.seek(start)
                binary_data = fh.read(self._threshold_filesize)
                file_stream = io.BytesIO(binary_data)
                file_stream.name = file_path
                file_dict = {'file': file_stream}

                retry_count = 0
                while True:
                    if retry_count >= self.DEFAULT_RETRY_LIMIT:
                        raise RuntimeError(
                            'Error uploading data chunk: {0:d}/{1:d}. Status '
                            'code: {2:d} - {3!s} {4!s}'.format(
                                index, chunks, response.status_code,
                                response.reason, response.text))

                    response = self._sketch.api.session.post(
                        self._resource_url, files=file_dict, data=data)

                    if response.status_code in definitions.HTTP_STATUS_CODE_20X:
                        break

                    retry_count += 1
                    logger.warning(
                        'Error uploading data chunk {0:d}/{1:d}, retry '
                        'attempt {2:d}/{3:d}'.format(
                            index, chunks, retry_count,
                            self.DEFAULT_RETRY_LIMIT))

        if response.status_code not in definitions.HTTP_STATUS_CODE_20X:
            raise RuntimeError(
                'Error uploading data: [{0:d}] {1!s} {2!s}, file: {3:s}, '
                'index {4:s}'.format(
                    response.status_code, response.reason, response.text,
                    file_path, self._index))

        response_dict = response.json()
        object_dict = response_dict.get('objects', [{}])[0]
        meta_dict = response_dict.get('meta', {})
        self._celery_task_id = meta_dict.get('task_id', '')

        self._timeline_id = object_dict.get('id')
        self._index = object_dict.get('searchindex', {}).get('index_name')
        self._last_response = response_dict
def _upload_binary_file ( self , file_path ) : file_size = os . path . getsize ( file_path ) if self . _timeline_name : timeline_name = self . _timeline_name else : file_name = os . path . basename ( file_path ) file_name_no_ext , _ , _ = file_name . rpartition ( '.' ) timeline_name = file_name_no_ext data = { 'name' : timeline_name , 'sketch_id' : self . _sketch . id , 'total_file_size' : file_size , 'provider' : self . _provider , 'data_label' : self . _data_label , } if self . _index : data [ 'index_name' ] = self . _index if self . _upload_context : data [ 'context' ] = self . _upload_context if file_size <= self . _threshold_filesize : file_dict = { 'file' : open ( file_path , 'rb' ) } response = self . _sketch . api . session . post ( self . _resource_url , files = file_dict , data = data ) else : chunks = int ( math . ceil ( float ( file_size ) / self . _threshold_filesize ) ) data [ 'chunk_total_chunks' ] = chunks data [ 'chunk_index_name' ] = uuid . uuid4 ( ) . hex for index in range ( 0 , chunks ) : data [ 'chunk_index' ] = index start = self . _threshold_filesize * index data [ 'chunk_byte_offset' ] = start fh = open ( file_path , 'rb' ) fh . seek ( start ) binary_data = fh . read ( self . _threshold_filesize ) file_stream = io . BytesIO ( binary_data ) file_stream . name = file_path file_dict = { 'file' : file_stream } retry_count = 0 while True : if retry_count >= self . DEFAULT_RETRY_LIMIT : raise RuntimeError ( 'Error uploading data chunk: {0:d}/{1:d}. Status ' 'code: {2:d} - {3!s} {4!s}' . format ( index , chunks , response . status_code , response . reason , response . text ) ) response = self . _sketch . api . session . post ( self . _resource_url , files = file_dict , data = data ) if response . status_code in definitions . HTTP_STATUS_CODE_20X : break retry_count += 1 logger . warning ( 'Error uploading data chunk {0:d}/{1:d}, retry ' 'attempt {2:d}/{3:d}' . format ( index , chunks , retry_count , self . DEFAULT_RETRY_LIMIT ) ) if response . status_code not in definitions . HTTP_STATUS_CODE_20X : raise RuntimeError ( 'Error uploading data: [{0:d}] {1!s} {2!s}, file: {3:s}, ' 'index {4:s}' . format ( response . status_code , response . reason , response . text , file_path , self . _index ) ) response_dict = response . json ( ) object_dict = response_dict . get ( 'objects' , [ { } ] ) [ 0 ] meta_dict = response_dict . get ( 'meta' , { } ) self . _celery_task_id = meta_dict . get ( 'task_id' , '' ) self . _timeline_id = object_dict . get ( 'id' ) self . _index = object_dict . get ( 'searchindex' , { } ) . get ( 'index_name' ) self . _last_response = response_dict