How to save base64 encoded file in Ruby? - ruby

I am trying to download image from internet using open-uri. Here is code:
require 'open-uri'
open('0RB2132__601_K3.jpg', 'wb') do |file|
file << open('http://luxonline.luxottica.com/luxpics/watermarkedextranet/med?style=0RB2132__601_K3').read
end
But it doesn't save image correctly. When I try to open it program reports:
Error interpreting JPEG image file (Improper call to JPEG library in state 200)
I opened original image on the internet in the Firefox and after examining it, found that it is base64 encoded image.
How to download this image from this address http://luxonline.luxottica.com/luxpics/watermarkedextranet/med?style=0RB2132__601_K3?

Using your script on OS X, it works as a charm. So your mistake is probably somewhere else

Related

Open URI downloading corrupt files

I am trying to download a .tar.gz file using Ruby. Upon download, the file is always corrupt in some way.
I am using this code to download the file:
require "open-uri"
File.open('img.tar.gz', 'wb') do |fo|
fo.write open('https://github.com/Arafatk/language-basics/blob/master/img.tar.gz').read
end
Is there a way to fix this?
Change the file mode in the open call:
open('https://github.com/Arafatk/language-basics/blob/master/img.tar.gz', "rb").read
It was opening the file in text mode, when you wanted binary mode.
You also needed to be using the proper URL to download a raw file from Github. In this case, the correct URL can be found by right-clicking the Raw link on the file's repo page (the original URL given), and that Raw URL is the one that contains the actual binary image that you're trying to download. Change the URL to this: https://github.com/Arafatk/language-basics/raw/master/img.tar.gz, and the change I suggested at the top of the answer works just fine.

Unable to copy image to clipboard in Firefox Addon development

I'm developing a Firefox Addon which has this functionality to copy an image to clipboard.
I'm using require("sdk/clipboard") lib, as described in the documentation
But when I try to attach a base64 image, it gives me the following error:
JPM [error] Message: Error: Invalid flavor for image/jpg
You can find an example of a command that tries to copy an image to the clipboard and is giving me thie error here: https://jsfiddle.net/g0Lff2b5/
When I try to use the example in the website, it works. I think that perhaps my base64 is invalid, even though I used many base64 generators, and setting this base64 into a tag works fine.
Anly ideas?
Well, I did a workaround fix that solves for now, but the main problem remain.
What I did in order to copy the image to the clipboard was to create a canvas object, add the image to it, and call the function that converts the image to a BASE64 string (.toDataURL('image/png')). then, I was able to copy this base64 to the clipboard.

file stored on s3 not rendering in browser

I am copying an image that I extract from an .ipa file on s3. The file is getting move correctly but when every I try to view it in a browsers it appear to broken, in google chrome. If I download the file directly to my machine and open it appears perfectly fine. It also renders ok in Safari.
Dir.mktmpdir do |dir|
Zip::File.open(tmp_ipa) do |zip_file|
# Find Icon File
icon = zip_file.find do |entry|
entry.name.include? 'AppIcon76x76#2'
end
icon.extract(File.join(dir, 'AppIcon.png'))
s3_icon = bucket.objects[icon_dest]
s3_icon.write(Pathname.new(File.join(dir, 'AppIcon.png')))
icon_url = s3_icon.public_url.to_s
end
end
The most likely problem is that you didn't set the Content-Type to image/png when you uploaded your image to S3. Try this on the command line:
curl -i http://your-bucket.s3.amazonaws.com/path/to/AppIcon.png
What's the Content-Type header? If it isn't image/png, set that at the time you upload.
This is almost certainly because Apple uses a non-standard proprietary extension of the PNG file format for the PNG files in an iPhone APP (archived link), such as the .ipa you say it was extracted from.
The reason it works in Safari, is because Safari uses the OS's image decoding libraries, which do support this non-standard format.
There are some conversion scripts out there, that work with varying success.

Downloading a file in Ruby, from a site that generates a download link?

I'm trying to write a Ruby script that would download an image file from a website, which does not have an image on it initially. The way this site works normally is that when you access the site, the site waits for about a second while it generates the link to download the image. After that's done, the browser presents the user with a download window to actually download the image. At no point is the image presented on the site itself, the site always says "Please wait", or "your image is ready to download".
How can I write a quick and dirty ruby script to download these image file links to the user's desktop?
Use Watir and nokogiri
require 'watir-webdriver'
require 'nokogiri'
$browser = Watir::Browser.new
$browser.goto "google.com"
$page_html = Nokogiri::HTML.parse($browser.html)
image = []
image = $page_html.css("img#hplogo").map{|link| link['src']}[0]
image_src = "https://www.google.com" + image
File.open("/home/user/Desktop/image.png", 'wb') do |f|
f.write open(image_src).read
end
Using the gem mechanize is a popular way of automating interaction with websites.

Cucumber embed for screenshots not linking to screenshot

Cross-posted from the Cukes Google Group:
I have experimented with a number of methods of saving screenshots,
but settled on the method that is built into watir-webdriver. No
matter which method I have used, I am not able to successfully embed a
link to this image in the Cucumber HTML report.
In c:\ruby\cucumber\project_name\features\support\hooks.rb, I'm using:
After do |scenario|
if scenario.failed?
#browser.driver.save_screenshot("screenshot.png")
embed("screenshot.png", "image/png")
end
end
A link with text "Screenshot" IS added to the report, but the URL is
the project directory path ("c:\ruby\cucumber\project_name") rather
than a direct link to the file ("c:\ruby\cucumber\project_name\screenshot.png"). I have tried a number of different image formats
and direct paths using Dir.pwd with the same results each time.
What am I missing?
Thanks
Windows XP
Ruby 1.8.7
watir-webdriver (0.2.4)
cucumber (0.10.3)
Aslak:
Try this:
After do |scenario|
if scenario.failed?
encoded_img = #browser.driver.screenshot_as(:base64)
embed("data:image/png;base64,#{encoded_img}",'image/png')
end
end
Aslak
Adam:
Aslak was able to see the embedded
image in the file that I emailed him,
while I was still unable to do so in
IE 8. I tried it out in Firefox 3.6
and the image appears as expected.
The problem may have originally been
with the embedding method itself (or
rather, my use of it), but using
Aslak's base64 solution it only fails
to work in the Internet Explorer
browser.
Aslak:
I believe Base64-encoding of images in
HTML pages [1] works in all decent
browsers (sorry, IE is not one of
them). However, it should work in
IE:
http://dean.edwards.name/weblog/2005/06/base64-ie/
(but maybe they broke it in IE8, or
maybe it only works with gifs, or
maybe IE needs a special kind of
base64 encoding, or maybe you should
just ditch IE)
If being able to read cucumber html
reports with screenshots in IE is
really important to you, you could
always write each image to disk:
png = #browser.driver.screenshot_as(:png)
path = (0..16).to_a.map{|a| rand(16).to_s(16)}.join + '.png' # Or use some GUID library to make a unique filename - scenario names are not guaranteed to be unique.
File.open(path, 'wb') {|io| io.write(png)}
embed(path, 'image/png')
Obviously you have to make sure the
relative path you pass to embed is
right (depending on where you write
the html itself)
[1]
http://en.wikipedia.org/wiki/Data_URI_scheme
HTH, Aslak

Resources