Skip to content

Commit

Permalink
add API endpoints to get file by hash
Browse files Browse the repository at this point in the history
  • Loading branch information
gaelmuller committed Sep 12, 2017
1 parent 281076c commit 0747ab1
Showing 1 changed file with 51 additions and 8 deletions.
59 changes: 51 additions & 8 deletions web/views/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@
from web.views.mixins import UIView


def return_file(file):
analyses = list(current_user.analyses.find({'_id': {'$in': file['file']['analysis']}}))
file['av_modules'] = [m.name for m in dispatcher.get_antivirus_modules()]

for analysis in analyses:
if 'analyst' in analysis:
analyst = store.users.find_one({'_id': analysis['analyst']})
analysis['analyst'] = clean_users(analyst)

file['file']['analysis'] = clean_analyses(analyses)
return render(file, 'files/show.html', ctx={'data': file, 'options': dispatcher.options})


class FilesView(FlaskView, UIView):
def index(self):
"""Get the list of objects.
Expand Down Expand Up @@ -60,16 +73,46 @@ def get(self, id):
:>json dict antivirus: dict with antivirus names as keys.
"""
file = {'file': clean_files(get_or_404(current_user.files, _id=id))}
analyses = list(current_user.analyses.find({'_id': {'$in': file['file']['analysis']}}))
file['av_modules'] = [m.name for m in dispatcher.get_antivirus_modules()]
return return_file(file)

@route('/md5/<md5>', methods=["GET"])
def get_md5(self, md5):
"""Get the object with `md5`.
.. :quickref: File; Get an object by MD5
:param md5: md5 of the object.
:>json file file: list of files (see :http:get:`/files/(id)` for details on the format of a file).
"""
file = {'file': clean_files(get_or_404(current_user.files, md5=md5))}
return return_file(file)

for analysis in analyses:
if 'analyst' in analysis:
analyst = store.users.find_one({'_id': analysis['analyst']})
analysis['analyst'] = clean_users(analyst)
@route('/sha1/<sha1>', methods=["GET"])
def get_sha1(self, sha1):
"""Get the object with `sha1`.
file['file']['analysis'] = clean_analyses(analyses)
return render(file, 'files/show.html', ctx={'data': file, 'options': dispatcher.options})
.. :quickref: File; Get an object by SHA1
:param sha1: sha1 of the object.
:>json file file: list of files (see :http:get:`/files/(id)` for details on the format of a file).
"""
file = {'file': clean_files(get_or_404(current_user.files, sha1=sha1))}
return return_file(file)

@route('/sha256/<sha256>', methods=["GET"])
def get_sha256(self, sha256):
"""Get the object with `sha256`.
.. :quickref: File; Get an object by SHA256
:param sha256: sha256 of the object.
:>json file file: list of files (see :http:get:`/files/(id)` for details on the format of a file).
"""
file = {'file': clean_files(get_or_404(current_user.files, sha256=sha256))}
return return_file(file)

@requires_permission('worker')
def post(self):
Expand Down

0 comments on commit 0747ab1

Please sign in to comment.