예제 #1
0
파일: efetch_app.py 프로젝트: alpblt/efetch
def create_app(elastic_url, cache_directory, max_file_size, plugins_file,
               debug):
    """Creates the core efetch flask app"""
    app = Flask(__name__, static_url_path=u'/static')

    if debug:
        logging.basicConfig(level=logging.DEBUG)

    max_file_size_in_bytes = max_file_size * 1000000,

    make_cache_directories(cache_directory)

    # Log a warning if plugin file is missing
    if not os.path.isfile(plugins_file):
        logging.warn(u'Plugin config file "' + plugins_file + u'" is empty')

    _helper = EfetchHelper(get_current_directory(), cache_directory,
                           max_file_size_in_bytes, plugins_file, elastic_url)

    @app.route('/')
    def home():
        """Returns the home page for efetch."""
        return render_template(u'index.html')

    @app.route('/plugins')
    @app.route('/plugins/')
    def list_plugins():
        """Returns a list of all active efetch plugins"""
        return json.dumps(_helper.plugin_manager.get_all_plugins())

    @app.route('/plugins/<plugin_name>', methods=['GET', 'POST'])
    def plugins(plugin_name):
        """Returns page of the given plugin.

        Args:
            plugin_name (str): The name of the plugin as defined in the yapsy-plugin file
        """
        plugin = _helper.plugin_manager.get_plugin_by_name(
            str(plugin_name).lower())

        index = _helper.get_request_value(request, 'index', DEFAULTS['index'])
        encoded_pathspec = _helper.get_request_value(request, 'pathspec', '')
        if not encoded_pathspec:
            encoded_pathspec = _helper.pathspec_helper.get_encoded_pathspec(
                os.path.expanduser('~'))

        logging.info('Plugin called %s, with index=%s and pathspec=%s',
                     plugin_name, index, encoded_pathspec)

        efetch_dictionary = _helper.pathspec_helper.get_evidence_item(
            encoded_pathspec, index, plugin.cache,
            hasattr(plugin, 'fast') and plugin.fast)

        return plugin.get(efetch_dictionary, _helper,
                          efetch_dictionary['file_cache_path'], request)

    return app
예제 #2
0
    def __init__(self, address, port, elastic_url, debug, cache_dir,
                 max_file_size, plugins_file):
        """Initializes Efetch variables and utils.

        Args:
            address: The hostname or IP address to listen on
            port: The port number the server is running on
            debug: The boolean that enables debug logging
            cache_dir: The directory to cache temporary files
            max_file_size: The max file size in Megabytes to cache
        """
        self._address = address
        self._port = port
        self._helper = None
        self._app = Bottle()
        self._debug = debug
        self._curr_directory = os.path.dirname(os.path.realpath(__file__))
        output_dir = cache_dir

        if debug:
            logging.basicConfig(level=logging.DEBUG)
            logging.getLogger(u'Rocket').setLevel(logging.WARNING)
        else:
            logging.basicConfig(level=logging.INFO)

        if not output_dir.endswith(os.path.sep):
            output_dir += os.path.sep
        if not os.path.isdir(output_dir):
            try:
                os.mkdir(output_dir)
                os.mkdir(output_dir + os.path.sep + u'thumbnails')
                os.mkdir(output_dir + os.path.sep + u'files')
            except IOError:
                logging.error(u'Could not find nor create output directory ' +
                              output_dir)
                sys.exit(2)

        if not os.path.isfile(plugins_file):
            logging.warn(u'Plugin config file "' + plugins_file +
                         u'" is empty')

        self._helper = EfetchHelper(self._curr_directory, output_dir,
                                    max_file_size * 1000000, plugins_file,
                                    elastic_url)

        self._route()