(
            self, file_storage, form, sketch, index_name, chunk_index_name='')
return self._upload_and_index(
            file_path=file_path,
            file_extension=file_extension,
            original_filename=_filename,
            timeline_name=timeline_name,
            index_name=index_name,
            sketch=sketch,
            form=form,
            data_label=data_label,
            enable_stream=enable_stream,
            meta=meta)
Upload a file.

        Args:
            file_storage: a FileStorage object.
            form: a dict with the configuration for the upload.
            sketch: Instance of timesketch.models.sketch.Sketch
            index_name: the OpenSearch index name for the timeline.
            chunk_index_name: A unique identifier for a file if
                chunks are used.

        Returns:
            A timeline if created otherwise a search index in JSON (instance
            of flask.wrappers.Response)
Upload a file.
Upload a file .
def _upload_file(
            self, file_storage, form, sketch, index_name, chunk_index_name=''):
        """Upload a file.

        Args:
            file_storage: a FileStorage object.
            form: a dict with the configuration for the upload.
            sketch: Instance of timesketch.models.sketch.Sketch
            index_name: the OpenSearch index name for the timeline.
            chunk_index_name: A unique identifier for a file if
                chunks are used.

        Returns:
            A timeline if created otherwise a search index in JSON (instance
            of flask.wrappers.Response)
        """
        _filename, _extension = os.path.splitext(file_storage.filename)
        file_extension = _extension.lstrip('.')
        timeline_name = form.get('name', _filename.rstrip('.'))

        # We do not need a human readable filename or
        # datastore index name, so we use UUIDs here.
        filename = uuid.uuid4().hex
        if not isinstance(filename, str):
            filename = codecs.decode(filename, 'utf-8')

        upload_folder = current_app.config['UPLOAD_FOLDER']
        file_path = os.path.join(upload_folder, filename)

        chunk_index = form.get('chunk_index')
        if isinstance(chunk_index, str) and chunk_index.isdigit():
            chunk_index = int(chunk_index)
        chunk_byte_offset = form.get('chunk_byte_offset')
        if isinstance(
                chunk_byte_offset, str) and chunk_byte_offset.isdigit():
            chunk_byte_offset = int(chunk_byte_offset)
        chunk_total_chunks = form.get('chunk_total_chunks')
        if isinstance(
                chunk_total_chunks, str) and chunk_total_chunks.isdigit():
            chunk_total_chunks = int(chunk_total_chunks)
        file_size = form.get('total_file_size')
        if isinstance(file_size, str) and file_size.isdigit():
            file_size = int(file_size)
        enable_stream = form.get('enable_stream', False)

        data_label = form.get('data_label', '')

        if chunk_total_chunks is None:
            file_storage.save(file_path)
            return self._upload_and_index(
                file_path=file_path,
                file_extension=file_extension,
                original_filename=file_storage.filename,
                timeline_name=timeline_name,
                index_name=index_name,
                sketch=sketch,
                form=form,
                data_label=data_label,
                enable_stream=enable_stream)

        # For file chunks we need the correct filepath, otherwise each chunk
        # will get their own UUID as a filename.
        if index_name:
            file_path = os.path.join(upload_folder, index_name)
        elif chunk_index_name:
            file_path = os.path.join(upload_folder, chunk_index_name)
        else:
            file_path = os.path.join(upload_folder, uuid.uuid4().hex)

        try:
            with open(file_path, 'ab') as fh:
                fh.seek(chunk_byte_offset)
                fh.write(file_storage.read())
        except OSError as e:
            abort(
                HTTP_STATUS_CODE_BAD_REQUEST,
                'Unable to write data with error: {0!s}.'.format(e))

        if (chunk_index + 1) != chunk_total_chunks:
            schema = {
                'meta': {
                    'file_upload': True,
                    'upload_complete': False,
                    'total_chunks': chunk_total_chunks,
                    'chunk_index': chunk_index,
                    'file_size': file_size},
                'objects': []}
            response = jsonify(schema)
            response.status_code = HTTP_STATUS_CODE_CREATED
            return response

        if os.path.getsize(file_path) != file_size:
            abort(
                HTTP_STATUS_CODE_BAD_REQUEST,
                'Unable to save file correctly, inconsistent file size '
                '({0:d} but should have been {1:d})'.format(
                    os.path.getsize(file_path), file_size))

        meta = {
            'file_upload': True,
            'upload_complete': True,
            'file_size': file_size,
            'total_chunks': chunk_total_chunks,
        }

        return self._upload_and_index(
            file_path=file_path,
            file_extension=file_extension,
            original_filename=_filename,
            timeline_name=timeline_name,
            index_name=index_name,
            sketch=sketch,
            form=form,
            data_label=data_label,
            enable_stream=enable_stream,
            meta=meta)
def _upload_file ( self , file_storage , form , sketch , index_name , chunk_index_name = '' ) : _filename , _extension = os . path . splitext ( file_storage . filename ) file_extension = _extension . lstrip ( '.' ) timeline_name = form . get ( 'name' , _filename . rstrip ( '.' ) ) # We do not need a human readable filename or # datastore index name, so we use UUIDs here. filename = uuid . uuid4 ( ) . hex if not isinstance ( filename , str ) : filename = codecs . decode ( filename , 'utf-8' ) upload_folder = current_app . config [ 'UPLOAD_FOLDER' ] file_path = os . path . join ( upload_folder , filename ) chunk_index = form . get ( 'chunk_index' ) if isinstance ( chunk_index , str ) and chunk_index . isdigit ( ) : chunk_index = int ( chunk_index ) chunk_byte_offset = form . get ( 'chunk_byte_offset' ) if isinstance ( chunk_byte_offset , str ) and chunk_byte_offset . isdigit ( ) : chunk_byte_offset = int ( chunk_byte_offset ) chunk_total_chunks = form . get ( 'chunk_total_chunks' ) if isinstance ( chunk_total_chunks , str ) and chunk_total_chunks . isdigit ( ) : chunk_total_chunks = int ( chunk_total_chunks ) file_size = form . get ( 'total_file_size' ) if isinstance ( file_size , str ) and file_size . isdigit ( ) : file_size = int ( file_size ) enable_stream = form . get ( 'enable_stream' , False ) data_label = form . get ( 'data_label' , '' ) if chunk_total_chunks is None : file_storage . save ( file_path ) return self . _upload_and_index ( file_path = file_path , file_extension = file_extension , original_filename = file_storage . filename , timeline_name = timeline_name , index_name = index_name , sketch = sketch , form = form , data_label = data_label , enable_stream = enable_stream ) # For file chunks we need the correct filepath, otherwise each chunk # will get their own UUID as a filename. if index_name : file_path = os . path . join ( upload_folder , index_name ) elif chunk_index_name : file_path = os . path . join ( upload_folder , chunk_index_name ) else : file_path = os . path . join ( upload_folder , uuid . uuid4 ( ) . hex ) try : with open ( file_path , 'ab' ) as fh : fh . seek ( chunk_byte_offset ) fh . write ( file_storage . read ( ) ) except OSError as e : abort ( HTTP_STATUS_CODE_BAD_REQUEST , 'Unable to write data with error: {0!s}.' . format ( e ) ) if ( chunk_index + 1 ) != chunk_total_chunks : schema = { 'meta' : { 'file_upload' : True , 'upload_complete' : False , 'total_chunks' : chunk_total_chunks , 'chunk_index' : chunk_index , 'file_size' : file_size } , 'objects' : [ ] } response = jsonify ( schema ) response . status_code = HTTP_STATUS_CODE_CREATED return response if os . path . getsize ( file_path ) != file_size : abort ( HTTP_STATUS_CODE_BAD_REQUEST , 'Unable to save file correctly, inconsistent file size ' '({0:d} but should have been {1:d})' . format ( os . path . getsize ( file_path ) , file_size ) ) meta = { 'file_upload' : True , 'upload_complete' : True , 'file_size' : file_size , 'total_chunks' : chunk_total_chunks , } return self . _upload_and_index ( file_path = file_path , file_extension = file_extension , original_filename = _filename , timeline_name = timeline_name , index_name = index_name , sketch = sketch , form = form , data_label = data_label , enable_stream = enable_stream , meta = meta )