Ameba Ownd

アプリで簡単、無料ホームページ作成

Charlene Holmes's Ownd

How to make ruby downloadable file

2021.12.19 11:17






















Connect and share knowledge within a single location that is structured and easy to search. I know that this is an old question, but Google threw me here and I think I found a simpler answer. The main advantage here it is concise and simple, because open does much of the heavy lifting. And it does not read the whole response in memory. We can exploit this knowledge to implement this lean download to file method.


Please be careful with user provided input! The sleep command is a hack that can dramatically reduce CPU usage when the network is the limiting factor. Sleeping for a moment gives the buffer a chance to fill between writes, and CPU usage is comparable to a curl solution, x difference in my application.


A more robust solution might examine progress of f. This is a version that automatically adjusts itself to keep the buffer just at or below capacity. It's an inelegant solution, but it seems to be just as fast, and to use as little CPU time, as it's calling out to curl. It works in three stages. A brief learning period with a deliberately long sleep time establishes the size of a full buffer. The drop period reduces the sleep time quickly with each iteration, by multiplying it by a larger factor, until it finds an under-filled buffer.


Then, during the normal period, it adjusts up and down by a smaller factor. My Ruby's a little rusty, so I'm sure this can be improved upon. First of all, there's no error handling. Also, maybe it could be separated into an object, away from the downloading itself, so that you'd just call autosleep. I could solve the problem by using:. Stack Overflow for Teams — Collaborate and share knowledge with a private group.


Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Ask Question. Asked 11 years, 9 months ago. Active 6 months ago. Viewed k times. This will give the downloaded file a random name generated by Tempfile. If you want to keep the name of the file from the URL you need to do a bit more work. In this case we can download the file to a Tempfile and then move it to a permanent location on our drive.


Down takes a bunch of other options too, to control the download experience. This stops attacker tying up your server with giant image downloads. If you wanted to download a file that was at most 5MB in size with at most 5 redirects, you would use:. Down is much safer as it saves us from infinite redirect loops and remote code injection, and it is more user friendly as it always returns a Tempfile and lets us easily restrict file size.


Have you used alternative methods for downloading images in Ruby? Let me know how in the comments below or on Twitter at philnash. Originally published at www. Sign up or log in Sign up using Google.


Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Who owns this outage? Building intelligent escalation chains for modern SRE. Podcast Who is building clouds for the independent developer?


Featured on Meta. It also turns out that open-uri has some other quirks. Notably, open-uri :. To solve all of this, Janko created the Down gem. It allows you to avoid these issues to safely and efficiently download files. We can download the image to a Tempfile using Down. If you want to save this file to the file system, Down has an option for that.


Passing a directory as a :destination option will save the file to that directory. This will give the downloaded file a random name generated by Tempfile. If you want to keep the name of the file from the URL you need to do a bit more work. In this case we can download the file to a Tempfile and then move it to a permanent location on our drive. For this we'll use FileUtils mv which takes two arguments, the file name and the destination we want to move it to. Now we've downloaded the file safely and efficiently and stored it permanently on our hard drive.


Down takes a bunch of other options too, to control the download experience. This stops attacker tying up your server with giant image downloads.