def lines(tree): """Return lines start:end of path in tree, where start, end, path are URL params. """ req = request.values path = req.get('path', '') from_line = max(0, int(req.get('start', ''))) to_line = int(req.get('end', '')) ctx_found = [] possible_hits = current_app.es.search( { 'filter': { 'and': [ {'term': {'path': path}}, {'range': {'number': {'gte': from_line, 'lte': to_line}}} ] }, '_source': {'include': ['content']}, 'sort': ['number'] }, size=max(0, to_line - from_line + 1), # keep it non-negative doc_type=LINE, index=es_alias_or_not_found(tree)) if 'hits' in possible_hits and len(possible_hits['hits']['hits']) > 0: for hit in possible_hits['hits']['hits']: ctx_found.append({'line_number': hit['sort'][0], 'line': hit['_source']['content'][0]}) return jsonify({'lines': ctx_found, 'path': path})
def raw(tree, path): """Send raw data at path from tree, for binary things like images.""" query = {'filter': {'term': {'path': path}}} results = current_app.es.search(query, index=es_alias_or_not_found(tree), doc_type=FILE, size=1) try: # we explicitly get index 0 because there should be exactly 1 result data = results['hits']['hits'][0]['_source']['raw_data'][0] except IndexError: # couldn't find the image raise NotFound data_file = StringIO(data.decode('base64')) return send_file(data_file, mimetype=guess_type(path)[0])
def parallel(tree, path=''): """If a file or dir parallel to the given path exists in the given tree, redirect to it. Otherwise, redirect to the root of the given tree. Deferring this test lets us avoid doing 50 queries when drawing the Switch Tree menu when 50 trees are indexed: we check only when somebody actually chooses something. """ config = current_app.dxr_config files = filtered_query(es_alias_or_not_found(tree), FILE, filter={'path': path.rstrip('/')}, size=1, include=[]) # We don't really need anything. return redirect(('{root}/{tree}/source/{path}' if files else '{root}/{tree}/source/').format(root=config.www_root, tree=tree, path=path))
def parallel(tree, path=''): """If a file or dir parallel to the given path exists in the given tree, redirect to it. Otherwise, redirect to the root of the given tree. Deferring this test lets us avoid doing 50 queries when drawing the Switch Tree menu when 50 trees are indexed: we check only when somebody actually chooses something. """ config = current_app.dxr_config files = filtered_query( es_alias_or_not_found(tree), FILE, filter={'path': path.rstrip('/')}, size=1, include=[]) # We don't really need anything. return redirect(('{root}/{tree}/source/{path}' if files else '{root}/{tree}/source/').format(root=config.www_root, tree=tree, path=path))
def raw(tree, path): """Send raw data at path from tree, for binary things like images.""" query = { 'filter': { 'term': { 'path': path } } } results = current_app.es.search( query, index=es_alias_or_not_found(tree), doc_type=FILE, size=1) try: # we explicitly get index 0 because there should be exactly 1 result data = results['hits']['hits'][0]['_source']['raw_data'][0] except IndexError: # couldn't find the image raise NotFound data_file = StringIO(data.decode('base64')) return send_file(data_file, mimetype=guess_type(path)[0])
def tree_root(tree): """Redirect requests for the tree root instead of giving 404s.""" # Don't do a redirect and then 404; that's tacky: es_alias_or_not_found(tree) return redirect(tree + '/source/')