Exemplo n.º 1
0
def send_file_to_karton(file: File) -> str:
    try:
        path = file.get_path()
        tmpfile = None
    except Exception:
        # If get_path doesn't work: download content to NamedTemporaryFile
        # It won't work if we use S3 storage and try to reanalyze
        # existing file (not uploaded within the same request).
        tmpfile = tempfile.NamedTemporaryFile()
        file_stream = file.open()
        shutil.copyfileobj(file_stream, tmpfile)
        File.close(file_stream)
        path = tmpfile.name

    producer = Producer(identity="karton.mwdb",
                        config=KartonConfig(config.karton.config_path))

    feed_quality = g.auth_user.feed_quality
    task_priority = TaskPriority.NORMAL if feed_quality == "high" else TaskPriority.LOW
    task = Task(headers={
        "type": "sample",
        "kind": "raw",
        "quality": feed_quality
    },
                payload={
                    "sample":
                    Resource(file.file_name, path=path, sha256=file.sha256),
                    "attributes":
                    file.get_metakeys(as_dict=True, check_permissions=False)
                },
                priority=task_priority)
    producer.send_task(task)

    if tmpfile is not None:
        tmpfile.close()

    file.add_metakey("karton", task.root_uid, check_permissions=False)
    logger.info("File sent to karton with %s", task.root_uid)
    return task.root_uid
Exemplo n.º 2
0
 def on_created_file(self, file: File) -> None:
     metakeys = file.get_metakeys(as_dict=True, check_permissions=False)
     if "karton" in metakeys:
         logger.info("Analyzed artifact - not sending to karton")
         return
     send_file_to_karton(file)