TheHive4py icon indicating copy to clipboard operation
TheHive4py copied to clipboard

Add the possibility to download files and keep them in a variable

Open TytoCapensis opened this issue 2 months ago • 2 comments

As today, all functions related to download of files take a file path as an argument, and download themselves the file on the filesystem (by using the functions in the session.py module)

This can cause issues because:

  • Downloading a file can be cumbersome in some contexts, sometimes it is preferable to keep it in memory (i.e. in a variable)
  • We do not have control about how the file is opened and written exactly

I intend to add a pull request that:

  • Allows to pass False as a "filepath" to all file download functions, to tell that we do not want the file to be written on the filesystem, but instead returned by the function so we can store it as a variable
  • Assume False as a default for all file download functions (currently there is no default, the user has to provide a file path regardless).

TytoCapensis avatar Apr 26 '24 15:04 TytoCapensis

Hi TytoCapensis!

Very nice suggestion! My original idea was to overload the download_path variable so it can be a pathlike parameter or an IO buffer.

I think that would be more elegant and probably the main modification should be done solely in _process_stream_response.

There one could check if download_path is a pathlike object or a buffer. In case it's a buffer then it should just write the chunks to it.

Can you give it a try?

Kamforka avatar Apr 26 '24 17:04 Kamforka

I just pushed a replacement commit that went with the method you suggested instead (using a io.BytesIO object passed in the argument). I still have to modify some files however, to change the type hinting definitions.

Note that I had to use a # type: ignore in session.py, because mypy got confused and was reporting that io.BytesIO was not a correct type for open() function (yet it is in another if block). If you have a better idea to go around that, I will change the code.

TytoCapensis avatar Apr 29 '24 11:04 TytoCapensis