Getting nil from "download" method when using the "destination" option
arman-h opened this issue · comments
Hello all,
First of all, thank you for the wonderful gem.
I think I've encountered a bug when using the destination
option - the file gets downloaded to the correct location, but the return value from Down.download
is nil
. Omitting that option returns a File
instance.
For example, this works:
url = "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png"
file = Down.download(url)
# <File:/var/folders/96/q0yz5pns6f7c2y4zld38hsjd4tw3vw/T/down-net_http20190515-83878-bdxd4c.png>
but this does not:
file = Down.download(url, destination: Rails.root.join('public/downloads/' + SecureRandom.uuid))
# nil
even though the file is created (running find public
yields public/downloads/e959fba2-cc08-429c-b8df-7f9938eff8f9
).
Is this the intended behavior, or am I doing something wrong? I am using version 4.8.0
.
This is the intended behaviour, the return value is documented in the README. When you don't use :destination
, Down will create a temporary file for you, and there it makes sense to return the File instance, because the path to the file doesn't matter.
However, when you use :destination
, I imagined that you're not always interested getting the File instance, just working with the path might often be enough. In that case I didn't want the the person needs to manually close the file returned by Down, I thought it's better for the user to open the file themselves if they need to.
But you're right that it makes for an inconsistent API, because now Down.download
will either return a File or nil depending on the :destination
argument. I agree it would make sense for it to return the File object always, I'm just worried that if I add it now, it might cause open file descriptor overflow for exiting users since they won't be closing that file.
For now I'll just update the documentation to make it clearer.
Thank you for the explanation, that makes sense now. It was unexpected behavior mostly because it was unclear how to know if the file was successfully downloaded. I will update my code to simply read the file from the destination.
This ticket can be closed now. Thanks again!