VirusTotal / vt-py

The official Python 3 client library for VirusTotal
https://virustotal.github.io/vt-py/
Apache License 2.0
547 stars 127 forks source link

scan_file_async closes file object #122

Closed notjl closed 1 year ago

notjl commented 2 years ago

I have been meaning to ask this, does the function intend to close the file after the scanning?

async def virus_analysis_hash(md5_hash):
    with suppress(Exception):
        result = await client.get_object_async(f"/files/{md5_hash}")
        return result.last_analysis_stats

    return None

async def virus_analysis_file(file):
    tmp_file = file.file
    result = await client.scan_file_async(tmp_file, True)
    return result.stats

async def virus_analysis(md5_hash, file):
    if not (result := await virus_analysis_hash(md5_hash)):
        result = await virus_analysis_file(file)

    return (
        True if result["malicious"] > 0 or result["suspicious"] > 0 else False
    )

I wanted to upload the file after checking if it isn't suspicious or malicious, is there a workaround or how do I stop the function from closing the file?

mgmacias95 commented 1 year ago

Hello @notjl,

https://github.com/VirusTotal/vt-py/blob/1d0b114c7e918c87e14d9ea7a7c49cb9120db68b/vt/client.py#L685-L721

The function does not explicitly close the file object. I have checked the aiohttp lib (which we use under the hood) and does not close it either.

Regards, Marta