def METHOD_NAME(self, data):
    """Remove internal fields from the record metadata."""
    external_pids = []
    bucket = None
    record = None
    # differentiating between search results and
    # single record requests
    if hasattr(g, 'record'):
        record = g.record
        if record.files:
            bucket = record.files.bucket
        if is_deposit(record.model):
            external_pids = generate_external_pids(record)
        # if it is a published record don't generate external pids
        # as they are immutable and stored in _deposit
        else:
            external_pids = record.model.json[
                '_deposit'].get('external_pids')
        user_has_permission = \
            allow_public_file_metadata(data['metadata']) if bucket \
            is None else files_permission_factory(
                bucket, 'bucket-read').can()
    elif hasattr(g, 'record_hit'):
        user_has_permission = allow_public_file_metadata(
            g.record_hit['_source'])
    if '_deposit' in data['metadata']:
        if hasattr(g, 'record') and is_deposit(record.model) and current_app.config.get('AUTOMATICALLY_ASSIGN_DOI', False):
            # add future DOI string
            data['metadata'].update({'$future_doi': generate_doi(data['metadata']['_deposit']['id']) })
        data['metadata']['owners'] = data['metadata']['_deposit']['owners']
        # Add the external_pids only if the
        # user is allowed to read the bucket
        if external_pids and bucket and user_has_permission:
            data['metadata']['external_pids'] = external_pids
        del data['metadata']['_deposit']
    if '_files' in data['metadata']:
        # Also add the files field only if the user is allowed
        if user_has_permission:
            data['files'] = data['metadata']['_files']
            if external_pids and bucket:
                external_dict = {x['key']: x['ePIC_PID']
                                 for x in external_pids}
                for _file in data['files']:
                    if _file['key'] in external_dict:
                        _file['b2safe'] = True
                        _file['ePIC_PID'] = external_dict[_file['key']]
        del data['metadata']['_files']
    if '_pid' in data['metadata']:
        # move PIDs to metadata top level
        epic_pids = [p for p in data['metadata']['_pid']
                     if p.get('type') == 'ePIC_PID']
        dois = [p for p in data['metadata']['_pid']
                if p.get('type') == 'DOI']
        if len(epic_pids) > 0:
            data['metadata']['ePIC_PID'] = epic_pids[0].get('value')
        if len(dois) > 0:
            data['metadata']['DOI'] = DOI_URL_PREFIX + dois[0].get('value')
        # add parent version pid
        # data['metadata']['parent_id'] = next(
        #     pid['value'] for pid in data['metadata']['_pid']
        #     if pid['type'] == RecordUUIDProvider.parent_pid_type
        # )
        del data['metadata']['_pid']
    if '_oai' in data['metadata']:
        del data['metadata']['_oai']
    if '_internal' in data['metadata']:
        del data['metadata']['_internal']
    return data