Exemple #1
0
def get_file_results_for_service(srl, service, **kwargs):
    """
    Get the all the file results of a specific file and a specific query.

    Variables:
    srl         => A resource locator for the file (SHA256)

    Arguments:
    all         => if all argument is present, it will return all versions
                    NOTE: Max to 100 results...

    Data Block:
    None

    API call example:
    /api/v3/file/result/123456...654321/service_name/

    Result example:
    {"file_info": {},            # File info Block
     "results": {}}              # Full result list for the service
    """
    user = kwargs['user']
    file_obj = STORAGE.get_file(srl)

    args = [("fl", "_yz_rk"),
            ("sort", "created desc")]
    if "all" in request.args:
        args.append(("rows", "100"))
    else:
        args.append(("rows", "1"))

    if not file_obj:
        return make_api_response([], "This file does not exists", 404)

    if user and Classification.is_accessible(user['classification'], file_obj['classification']):
        res = STORAGE.direct_search("result", "_yz_rk:%s.%s*" % (srl, service), args,
                                    __access_control__=user["access_control"])['response']['docs']
        keys = [k["_yz_rk"] for k in res]

        results = []
        for r in STORAGE.get_results(keys):
            result = format_result(user['classification'], r, file_obj['classification'])
            if result:
                results.append(result)

        return make_api_response({"file_info": file_obj, "results": results})
    else:
        return make_api_response([], "You are not allowed to view this file", 403)
Exemple #2
0
def get_file_results(srl, **kwargs):
    """
    Get the all the file results of a specific file.
    
    Variables:
    srl         => A resource locator for the file (SHA256) 
    
    Arguments: 
    None
    
    Data Block:
    None

    API call example:
    /api/v3/file/result/123456...654321/
    
    Result example:
    {"file_info": {},            # File info Block
     "results": {},              # Full result list 
     "errors": {},               # Full error list
     "parents": {},              # List of possible parents
     "childrens": {},            # List of children files
     "tags": {},                 # List tags generated
     "metadata": {},             # Metadata facets results
     "file_viewer_only": True }  # UI switch to disable features
    """
    user = kwargs['user']
    file_obj = STORAGE.get_file(srl)

    if not file_obj:
        return make_api_response({}, "This file does not exists", 404)

    if user and Classification.is_accessible(user['classification'], file_obj['classification']):
        output = {"file_info": {}, "results": [], "tags": []}
        plan = [
            (STORAGE.list_file_active_keys, (srl, user["access_control"]), "results"),
            (STORAGE.list_file_parents, (srl, user["access_control"]), "parents"),
            (STORAGE.list_file_childrens, (srl, user["access_control"]), "children"),
            (STORAGE.get_file_submission_meta, (srl, user["access_control"]), "meta"),
        ]
        temp = execute_concurrently(plan)
        active_keys, alternates = temp['results']
        output['parents'] = temp['parents']
        output['childrens'] = temp['children']
        output['metadata'] = temp['meta']

        output['file_info'] = file_obj
        output['results'] = [] 
        output['alternates'] = {}
        res = STORAGE.get_results(active_keys)
        for r in res:
            res = format_result(user['classification'], r, file_obj['classification'])
            if res:
                output['results'].append(res)

        for i in alternates:
            if i['response']['service_name'] not in output["alternates"]:
                output["alternates"][i['response']['service_name']] = []
            i['response']['service_version'] = i['_yz_rk'].split(".", 3)[2].replace("_", ".")
            output["alternates"][i['response']['service_name']].append(i)
        
        output['errors'] = [] 
        output['file_viewer_only'] = True
        
        for res in output['results']:
            # noinspection PyBroadException
            try:
                if "result" in res:
                    if 'tags' in res['result']:
                        output['tags'].extend(res['result']['tags'])
            except:
                pass
        
        return make_api_response(output)
    else:
        return make_api_response({}, "You are not allowed to view this file", 403)
Exemple #3
0
def get_file_submission_results(sid, srl, **kwargs):
    """
    Get the all the results and errors of a specific file
    for a specific Submission ID
    
    Variables:
    sid         => Submission ID to get the result for
    srl         => Resource locator to get the result for
    
    Arguments (POST only): 
    extra_result_keys   =>  List of extra result keys to get
    extra_error_keys    =>  List of extra error keys to get
    
    Data Block:
    None
    
    Result example:
    {"errors": [],    # List of error blocks 
     "file_info": {}, # File information block (md5, ...)
     "results": [],   # List of result blocks
     "tags": [] }     # List of generated tags
    """
    user = kwargs['user']
    
    # Check if submission exist
    data = STORAGE.get_submission(sid)
    if data is None:
        return make_api_response("", "Submission ID %s does not exists." % sid, 404)
    
    if data and user and Classification.is_accessible(user['classification'], data['classification']):
        # Prepare output
        output = {"file_info": {}, "results": [], "tags": [], "errors": []}
        
        # Extra keys - This is a live mode optimisation
        res_keys = data.get("results", [])
        err_keys = data.get("errors", [])
            
        if request.method == "POST" and request.json is not None and data['state'] != "completed":
            extra_rkeys = request.json.get("extra_result_keys", [])
            extra_ekeys = request.json.get("extra_error_keys", [])
        
            # Load keys 
            res_keys.extend(extra_rkeys)
            err_keys.extend(extra_ekeys)
            
        res_keys = list(set(res_keys))
        err_keys = list(set(err_keys))
    
        # Get File, results and errors
        temp_file = STORAGE.get_file(srl)
        if not Classification.is_accessible(user['classification'], temp_file['classification']):
            return make_api_response("", "You are not allowed to view the data of this file", 403)
        output['file_info'] = temp_file
        
        temp_results = STORAGE.get_results(set([x for x in res_keys if x.startswith(srl)]))
        results = []
        for r in temp_results:
            r = format_result(user['classification'], r, temp_file['classification'])
            if r:
                results.append(r)
        output['results'] = results 

        output['errors'] = STORAGE.get_errors(set([x for x in err_keys if x.startswith(srl)]))
        output['metadata'] = STORAGE.get_file_submission_meta(srl, user["access_control"])
        
        # Generate tag list
        temp = {}
        for res in output['results']:
            try:
                if res.has_key('result'):
                    if res['result'].has_key('tags'):
                        temp.update({"__".join([v["type"], v['value']]): v for v in res['result']['tags']})
            except:
                pass
        
        output["tags"] = temp.values()
        
        return make_api_response(output)
    else:
        return make_api_response("", "You are not allowed to view the data of this submission", 403)