<commit_before>"""
Simple directory tree view
"""

from yapsy.IPlugin import IPlugin
from jinja2 import Template
import json
import logging
import os

class Directory(IPlugin):

    def __init__(self):
        self.display_name = 'Navigate'
        self.popularity = 9
        self.cache = False
        self.fast = False
        self.action = False
        self.icon = 'fa-folder-o'

        self._file_plugin = 'analyze'
        self._dir_plugin = 'directory'
        self._evidence_plugin = 'directory'

        IPlugin.__init__(self)

    def activate(self):
        IPlugin.activate(self)
        return

    def deactivate(self):
        IPlugin.deactivate(self)
        return

    def check(self, evidence, path_on_disk):
        """Checks if the file is compatible with this plugin"""
        return True

    def mimetype(self, mimetype):
        """Returns the mimetype of this plugins get command"""
        return "text/plain"

    @staticmethod
    def human_readable_size(num, suffix='B'):
        for unit in ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']:
            if abs(num) < 1000.0:
                return "%3.1f%s%s" % (num, unit, suffix)
            num /= 1000.0
        return "%.1f%s%s" % (num, 'Yi', suffix)

    def get(self, evidence, helper, path_on_disk, request):
        """Returns the result of this plugin to be displayed in a browser"""
        dir_table = []
        file_table = []

        row_template = Template("""
            <tr>
                <!-- {{ file_name }} -->
                <td style="padding-left: 24px;"><a href="/plugins/{{ plugin }}?{{ url_query }}" {{ target }}>
                    <img src="{{ icon }}" style="width:32px;height:32px;"
                    alt="{{ order }} {{ file_name }}"></a></td>
                <td><a href="/plugins/{{ plugin }}?{{ url_query }}" {{ target }}>{{file_name}}</a></td>
                <td>{{ mtime_no_nano }}</td>
                <td>{{ atime_no_nano }}</td>
                <td>{{ ctime_no_nano }}</td>
                <td>{{ crtime_no_nano }}</td>
                <td>{{ size }}</td>
                <td>
                    {{ analyze }}
                    {{ preview }}
                    {{ download }}
                </td>
            </tr>
        """)
        analyze_template = Template("""
                    <a href="/plugins/analyze?{{ url_query }}" target="_top" style="padding-right:10px">
                        <span class="fa-stack fa-md">
                            <i class="fa fa-square fa-stack-2x"></i>
                            <i class="fa fa-info-circle fa-stack-1x fa-inverse"></i>
                        </span>
                    </a>""")
        download_template = Template("""
                    <a href="/plugins/download?{{ url_query }}">
                        <span class="fa-stack fa-md">
                            <i class="fa fa-square fa-stack-2x"></i>
                            <i class="fa fa-download fa-stack-1x fa-inverse"></i>
                        </span>
                    </a>""")
        preview_template = Template("""
                    <a href="/plugins/preview?{{ url_query }}&redirect=True" target="_blank" style="padding-right:10px">
                        <span class="fa-stack fa-md">
                            <i class="fa fa-square fa-stack-2x"></i>
                            <i class="fa fa-eye fa-stack-1x fa-inverse"></i>
                        </span>
                    </a>""")

        # ORDER:
        #   1 - Up
        #   2 - Folders
        #   3 - Evidence and files

        # if ('volume_type' in evidence or 'storage_type' in evidence or 'compression_type' in evidence or \
        #        'archive_type' in evidence) and not evidence['mimetype'].startswith('application/vnd'):
        #     if u'archive_type' in evidence and u'ZIP' in evidence['archive_type']:
        #         initial_pathspec = [helper.pathspec_helper.get_zip_base_pathspec(evidence['pathspec'])]
        #     else:
        #         initial_pathspec = helper.pathspec_helper.get_new_base_pathspecs(evidence['pathspec'])

        if ('volume_type' in evidence or 'storage_type' in evidence or \
               'archive_type' in evidence) and not evidence['mimetype'].startswith('application/vnd'):
            initial_pathspec = helper.pathspec_helper.get_base_pathspecs(evidence)

            if len(initial_pathspec) > 1:
                for item in initial_pathspec:
                    item['icon'] = '/resources/icons/_evidence.png'
                    item['order'] = 3
                    item['plugin'] = self._evidence_plugin
                    item['download'] = download_template.render(item)
                    item['preview'] = preview_template.render(item)
                    item['analyze'] = analyze_template.render(item)
                    if 'size' in item:
                        item['size'] = Directory.human_readable_size(int(item['size']))
                    file_table.append(row_template.render(item))
            else:
                for item in helper.pathspec_helper.list_directory(initial_pathspec[0]['pathspec']):
                    if 'file_name' not in item or not item['file_name']:
                        item['file_name'] = '-'
                    item['icon'] = helper.get_icon(item)
                    if item['meta_type'] == 'Directory':
                        item['order'] = 2
                        item['plugin'] = self._dir_plugin
                        item['analyze'] = analyze_template.render(item)
                        if 'size' in item:
                            item['size'] = Directory.human_readable_size(int(item['size']))
                        dir_table.append(row_template.render(item))
                    else:
                        item['order'] = 3
                        item['target'] = 'target="_top"'
                        item['plugin'] = self._file_plugin
                        item['download'] = download_template.render(item)
                        item['preview'] = preview_template.render(item)
                        item['analyze'] = analyze_template.render(item)
                        if 'size' in item:
                            item['size'] = Directory.human_readable_size(int(item['size']))
                        file_table.append(row_template.render(item))

            initial_pathspec = evidence['pathspec']
        elif 'compression_type' in evidence:
            initial_pathspec = helper.pathspec_helper.get_base_pathspecs(evidence)[0]
            item = helper.pathspec_helper.get_evidence_item(initial_pathspec['pathspec'])
            if 'file_name' not in item or not item['file_name']:
                item['file_name'] = os.path.splitext(evidence['file_name'])[0]
            item['icon'] = helper.get_icon(item)
            if ('volume_type' in item or 'storage_type' in item or 'compression_type' in item) or \
                    ('archive_type' in item and not item['mimetype'].startswith('application/vnd') \
                             and item['extension'].lower() not in helper.standard_office_2007_extensions):
                item['order'] = 3
                item['plugin'] = self._evidence_plugin
                item['download'] = download_template.render(item)
                item['preview'] = preview_template.render(item)
                item['analyze'] = analyze_template.render(item)<commit_msg>Updated directory to show time stamps in evidence properly.<commit_after>"""
Simple directory tree view
"""

from yapsy.IPlugin import IPlugin
from jinja2 import Template
import json
import logging
import os

class Directory(IPlugin):

    def __init__(self):
        self.display_name = 'Navigate'
        self.popularity = 9
        self.cache = False
        self.fast = False
        self.action = False
        self.icon = 'fa-folder-o'

        self._file_plugin = 'analyze'
        self._dir_plugin = 'directory'
        self._evidence_plugin = 'directory'

        IPlugin.__init__(self)

    def activate(self):
        IPlugin.activate(self)
        return

    def deactivate(self):
        IPlugin.deactivate(self)
        return

    def check(self, evidence, path_on_disk):
        """Checks if the file is compatible with this plugin"""
        return True

    def mimetype(self, mimetype):
        """Returns the mimetype of this plugins get command"""
        return "text/plain"

    @staticmethod
    def human_readable_size(num, suffix='B'):
        for unit in ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']:
            if abs(num) < 1000.0:
                return "%3.1f%s%s" % (num, unit, suffix)
            num /= 1000.0
        return "%.1f%s%s" % (num, 'Yi', suffix)

    def get(self, evidence, helper, path_on_disk, request):
        """Returns the result of this plugin to be displayed in a browser"""
        dir_table = []
        file_table = []

        row_template = Template("""
            <tr>
                <!-- {{ file_name }} -->
                <td style="padding-left: 24px;"><a href="/plugins/{{ plugin }}?{{ url_query }}" {{ target }}>
                    <img src="{{ icon }}" style="width:32px;height:32px;"
                    alt="{{ order }} {{ file_name }}"></a></td>
                <td><a href="/plugins/{{ plugin }}?{{ url_query }}" {{ target }}>{{file_name}}</a></td>
                <td>{{ mtime_no_nano }}</td>
                <td>{{ atime_no_nano }}</td>
                <td>{{ ctime_no_nano }}</td>
                <td>{{ crtime_no_nano }}</td>
                <td>{{ size }}</td>
                <td>
                    {{ analyze }}
                    {{ preview }}
                    {{ download }}
                </td>
            </tr>
        """)
        analyze_template = Template("""
                    <a href="/plugins/analyze?{{ url_query }}" target="_top" style="padding-right:10px">
                        <span class="fa-stack fa-md">
                            <i class="fa fa-square fa-stack-2x"></i>
                            <i class="fa fa-info-circle fa-stack-1x fa-inverse"></i>
                        </span>
                    </a>""")
        download_template = Template("""
                    <a href="/plugins/download?{{ url_query }}">
                        <span class="fa-stack fa-md">
                            <i class="fa fa-square fa-stack-2x"></i>
                            <i class="fa fa-download fa-stack-1x fa-inverse"></i>
                        </span>
                    </a>""")
        preview_template = Template("""
                    <a href="/plugins/preview?{{ url_query }}&redirect=True" target="_blank" style="padding-right:10px">
                        <span class="fa-stack fa-md">
                            <i class="fa fa-square fa-stack-2x"></i>
                            <i class="fa fa-eye fa-stack-1x fa-inverse"></i>
                        </span>
                    </a>""")

        # ORDER:
        #   1 - Up
        #   2 - Folders
        #   3 - Evidence and files

        # if ('volume_type' in evidence or 'storage_type' in evidence or 'compression_type' in evidence or \
        #        'archive_type' in evidence) and not evidence['mimetype'].startswith('application/vnd'):
        #     if u'archive_type' in evidence and u'ZIP' in evidence['archive_type']:
        #         initial_pathspec = [helper.pathspec_helper.get_zip_base_pathspec(evidence['pathspec'])]
        #     else:
        #         initial_pathspec = helper.pathspec_helper.get_new_base_pathspecs(evidence['pathspec'])

        if ('volume_type' in evidence or 'storage_type' in evidence or \
               'archive_type' in evidence) and not evidence['mimetype'].startswith('application/vnd'):
            initial_pathspec = helper.pathspec_helper.get_base_pathspecs(evidence)

            if len(initial_pathspec) > 1:
                for item in initial_pathspec:
                    item['icon'] = '/resources/icons/_evidence.png'
                    item['order'] = 3
                    item['plugin'] = self._evidence_plugin
                    item['download'] = download_template.render(item)
                    item['preview'] = preview_template.render(item)
                    item['analyze'] = analyze_template.render(item)
                    if 'size' in item:
                        item['size'] = Directory.human_readable_size(int(item['size']))
                    file_table.append(row_template.render(item))
            else:
                for item in helper.pathspec_helper.list_directory(initial_pathspec[0]['pathspec']):
                    if 'file_name' not in item or not item['file_name']:
                        item['file_name'] = '-'
                    for time in ['mtime', 'atime', 'ctime', 'crtime']:
                        if time in item:
                            item[time + '_no_nano'] = item[time].split('.')[0].replace('T', ' ')
                    item['icon'] = helper.get_icon(item)
                    if item['meta_type'] == 'Directory':
                        item['order'] = 2
                        item['plugin'] = self._dir_plugin
                        item['analyze'] = analyze_template.render(item)
                        if 'size' in item:
                            item['size'] = Directory.human_readable_size(int(item['size']))
                        dir_table.append(row_template.render(item))
                    else:
                        item['order'] = 3
                        item['target'] = 'target="_top"'
                        item['plugin'] = self._file_plugin
                        item['download'] = download_template.render(item)
                        item['preview'] = preview_template.render(item)
                        item['analyze'] = analyze_template.render(item)
                        if 'size' in item:
                            item['size'] = Directory.human_readable_size(int(item['size']))
                        file_table.append(row_template.render(item))

            initial_pathspec = evidence['pathspec']
        elif 'compression_type' in evidence:
            initial_pathspec = helper.pathspec_helper.get_base_pathspecs(evidence)[0]
            item = helper.pathspec_helper.get_evidence_item(initial_pathspec['pathspec'])
            if 'file_name' not in item or not item['file_name']:
                item['file_name'] = os.path.splitext(evidence['file_name'])[0]
            for time in ['mtime', 'atime', 'ctime', 'crtime']:
                if time in item:
                    item[time + '_no_nano'] = item[time].split('.')[0].replace('T', ' ')
            item['icon'] = helper.get_icon(item)
            if ('volume_type' in item or 'storage_type' in item or 'compression_type' in item) or \
                    ('archive_type' in item and not item['mimetype'].startswith('application/vnd') \
                             and item['extension'].lower() not in helper.standard_office_2007_extensions):
                item['order'] = 3
                item['plugin'] = self._evidence_plugin
                item['download'] = download_template.render(item)
                item['preview'] = preview_template.render(item)
                item['analyze'] = analyze_template.render(item)
{'fraction_non_alphanumeric': 0.1339320833879638, 'fraction_numerical': 0.0061623180805034745, 'mean_word_length': 1.6511991657977059, 'pattern_counts': {'":': 0, '<': 101, '<?xml version=': 0, '>': 101, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4885525', 'n_tokens_mistral': 4327, 'n_tokens_neox': 4046, 'n_words': 889}