lib/morandi/profiled_pixbuf.rb in morandi-0.11.0 vs lib/morandi/profiled_pixbuf.rb in morandi-0.11.1
- old
+ new
@@ -10,10 +10,19 @@
type && type.name.eql?('jpeg')
rescue
false
end
+ def self.from_string(string, loader: nil, chunk_size: 4096)
+ loader ||= Gdk::PixbufLoader.new
+ ((string.bytesize + chunk_size - 1) / chunk_size).times do |i|
+ loader.write(string.byteslice(i * chunk_size, chunk_size))
+ end
+ loader.close
+ loader.pixbuf
+ end
+
def self.default_icc_path(path)
"#{path}.icc.jpg"
end
def initialize(*args)
@@ -28,9 +37,21 @@
args[0] = icc_file if valid_jpeg?(icc_file) || system("jpgicc", "-q97", @file, icc_file)
end
end
super(*args)
+ rescue Gdk::PixbufError::CorruptImage => e
+ if args[0].is_a?(String) && defined? Tempfile
+ temp = Tempfile.new
+ pixbuf = self.class.from_string(File.read(args[0]))
+ pixbuf.save(temp.path, 'jpeg')
+ args[0] = temp.path
+ super(*args)
+ temp.close
+ temp.unlink
+ else
+ throw e
+ end
end
protected
def suitable_for_jpegicc?