lib/rrtf/node/image_node.rb in rrtf-1.0.0 vs lib/rrtf/node/image_node.rb in rrtf-1.0.1

- old
+ new

@@ -1,6 +1,7 @@ require 'fastimage' +require 'open-uri' module RRTF # This class represents an image within a RTF document. Currently only the # PNG, JPEG and Windows Bitmap formats are supported. Efforts are made to # identify the file type but these are not guaranteed to work. @@ -58,11 +59,11 @@ end # This is the constructor for the ImageNode class. # # @param parent [Node] a reference to the node that owns the new image node. - # @param source [String, File] a reference to the image source; this must be a String or a File. + # @param source [String, File] a reference to the image source; this must be a String, String URL, or a File. # @param id [Integer] a unique 32-bit identifier for the image. # @param options [Hash] a hash of options. # @option options [String] "width" (nil) the display width of the image in twips (can be a string, see {Utilities.value2twips}). # @option options [String] "height" (nil) the display height of the image in twips (can be a string, see {Utilities.value2twips}). # @option options [String] "sizing_mode" ("ABSOLUTE") the method used to size the image (see {SIZING_MODE_DICTIONARY}). @@ -70,11 +71,11 @@ # a supported image type, something other than a String or # File or IO is passed as the source parameter or if the # specified source does not exist or cannot be accessed. def initialize(parent, source, id, options = {}) super(parent) - @source = nil + @source = source @id = id # load default options options = { "width" => nil, @@ -93,20 +94,23 @@ @border.each do |b| b.push_colours(root.colours) end # Store path to image. - @source = source if source.instance_of?(String) || source.instance_of?(Tempfile) - @source = source.path if source.instance_of?(File) + if @source.is_a?(String) + begin + @source = open(@source) + rescue OpenURI::HTTPError => error + response = error.io + RTFError.fire("Could not open '#{@source}'. Server responded with #{response.status.join(',')}.") + rescue Exception => error + RTFError.fire("Could not open '#{@source}'. #{error.message}.") + end # rescue block + elsif !@source.respond_to?(:each_byte) + RTFError.fire("A string or object that responds to :each_byte must be supplied - '#{@source}' given.") + end # unless - # Check the file's existence and accessibility. - if !File.exist?(@source) - RTFError.fire("Unable to find the #{File.basename(@source)} file.") - end - if !File.readable?(@source) - RTFError.fire("Access to the #{File.basename(@source)} file denied.") - end # Attempt to determine image type and dimensions. @type, @width, @height = self.class.inspect(@source) if @type.nil? RTFError.fire("The #{File.basename(@source)} file contains an unknown or unsupported image type.") elsif @width.nil? || @height.nil? @@ -126,22 +130,20 @@ text << "\\picwgoal#{@displayed_width}" if @displayed_width != nil text << "\\pichgoal#{@displayed_height}" if @displayed_height != nil text << "\\picw#{@width}\\pich#{@height}\\bliptag#{@id}" text << "\\#{@type}\n" - open_file do |file| - file.each_byte do |byte| - hex_str = byte.to_s(16) - hex_str.insert(0,'0') if hex_str.length == 1 - text << hex_str - count += 1 - if count == 40 - text << "\n" - count = 0 - end # if - end # each_byte - end # open_file + @source.each_byte do |byte| + hex_str = byte.to_s(16) + hex_str.insert(0,'0') if hex_str.length == 1 + text << hex_str + count += 1 + if count == 40 + text << "\n" + count = 0 + end # if + end # each_byte text << "\n}" text.string end # to_rtf() @@ -160,16 +162,8 @@ [@displayed_width, @displayed_height] else [(@width*scale_factor).to_i, (@height*scale_factor).to_i] end end # case - end - - def open_file(&block) - if block - File.open(@source, 'rb', &block) - else - File.open(@source, 'rb') - end # if - end # open_file() + end # size_image() end # End of the ImageNode class. end # module RRTF