From e12a4a648b6a80470efcea71c38d9366845a04c0 Mon Sep 17 00:00:00 2001 From: Michael Weiser Date: Wed, 24 Nov 2021 08:14:27 +0000 Subject: [PATCH] analyzer: Allow for data to be provided If the user has the file content in a buffer it is more efficient to be able to hand it to the analyzer directly instead of writing it out into a temporary file just to be read back in again by the requests module. Add an observable key 'dataProvided' which can be set to True to change behaviour of run_by_id() and in turn run_by_name() so it uses the 'data' key as observable data directly instead of interpreting it as a file name and opening that file. Signed-off-by: Michael Weiser --- cortex4py/controllers/analyzers.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/cortex4py/controllers/analyzers.py b/cortex4py/controllers/analyzers.py index 1c1fb9a..0701d0a 100644 --- a/cortex4py/controllers/analyzers.py +++ b/cortex4py/controllers/analyzers.py @@ -68,11 +68,19 @@ def run_by_id(self, analyzer_id, observable, **kwargs) -> Job: post[key] = observable.get(key, None) if observable.get('dataType') == "file": - file_path = observable.get('data', None) - file_def = { - "data": (os.path.basename(file_path), open(file_path, 'rb'), - magic.Magic(mime=True).from_file(file_path)) - } + data = observable.get('data', None) + if observable.get('dataProvided') == True: + file_name = observable.get('parameters', {}).get('filename') + file_def = { + "data": (os.path.basename(file_name), data, + magic.Magic(mime=True).from_buffer(data)) + } + else: + file_path = data + file_def = { + "data": (os.path.basename(file_path), open(file_path, 'rb'), + magic.Magic(mime=True).from_file(file_path)) + } data = { '_json': json.dumps(post)