Use Cases. Support Plans Status. Build the future of communications. Sample applications that cover common use cases in a variety of languages. Download, test drive, and tweak them yourself.
Plain old Ruby The most popular way to download a file without any dependencies is to use the standard library open-uri. In the directory in which you opened IRB you will now find the image you downloaded. Problems with open-uri The thing is, using open-uri like this is not ideal. Notably, open-uri : leaves you vulnerable to remote code execution sometimes returns a Tempfile other times StringIO doesn't let you limit redirects or file size To solve all of this, Janko created the Down gem.
Improving downloads with the Down gem Let's download the same image using Down. Start by installing the gem:. Advanced usage Down takes a bunch of other options too, to control the download experience. Next steps In this post we've seen how to download images using both open-uri and Down.
Rate this post. Sign up and start building Not ready yet? Talk to an expert. Thanks for rating this post! If applicable fill in the countries where you are using Twilio.
Something went wrong. All images only saved on S3, not local. Need to hide exposed s3 url as well if possible or camouflage it. To avoid extra load to your app saving dyno's time in Heroku , I would rather do something like this: add this method to your model with the attachment:.
EDIT: the correct solution for download controller action can be found here I've updated the code above : Force a link to download an MP3 rather than play it?
I'm not a Rails developer - so just Google it and you'll see plenty of examples - but it probably looks something like this:. 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. Asked 9 years, 1 month ago. Active 3 years, 11 months ago. Viewed 9k times. Need to hide exposed s3 url as well if possible or camouflage it Here is my current setup Index.
RubyNewbie RubyNewbie 4 4 silver badges 20 20 bronze badges. Add a comment. Active Oldest Votes. The thing is, using open-uri like this is not ideal. First up, the above code is not very memory efficient, it loads the entire image into memory and then writes to disk. 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. Down takes a bunch of other options too, to control the download experience.
0コメント