-
-
Save Nerian/989563 to your computer and use it in GitHub Desktop.
Merger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
module Resque | |
module InMemoryMerger | |
def self.merge_partial_images_from_tasks(image_hash) | |
puts 'Merging image' | |
top_partial = image_hash['0'] | |
image_hash.delete('0') | |
image_hash.each { |k,v| v.slice!(0,18) } | |
final_image = [top_partial,(image_hash.keys.sort{|a,b| a.to_i <=> b.to_i}).collect{ |i| image_hash[i] }.join].join | |
puts 'Image merged' | |
final_image | |
end | |
end | |
module Merger | |
def self.merge_partial_images_from_tasks(hash_OrderImage) | |
#puts 'Merging image' | |
partial_images_names = save_partial_images_to_tmp_folder(hash_OrderImage) | |
#puts partial_images_names.inspect | |
partial_images_names.each_cons 2 do |origin, destiny| | |
#puts "\ttail -c +19 #{origin} >> #{destiny}" | |
`tail -c +19 #{origin} >> #{destiny}` | |
end | |
#puts 'Image merged' | |
final_image = File.read(partial_images_names.last) | |
end | |
private | |
def self.save_partial_images_to_tmp_folder(partial_images) | |
files = [] | |
tmp_folder = '/tmp/dpovray/merger'+ rand(10000).to_s + '/' | |
system("mkdir -p #{tmp_folder}") | |
keys = partial_images.keys.sort{|a,b| a.to_i <=> b.to_i}.reverse | |
puts 'Keys: '+keys.inspect | |
keys.each do |key| | |
image = partial_images[key.to_s] | |
files << tmp_folder+'image'+key.to_s+'.tga' | |
File.open(tmp_folder+'image'+key.to_s+'.tga', "w") do |f| | |
f.write(image) | |
end | |
end | |
files | |
end | |
end | |
end | |
partial_image = JSON.parse(File.read(File.dirname(__FILE__)+"/code")) | |
image = Resque::Merger.merge_partial_images_from_tasks(partial_image) | |
File.open("/tmp/image-tail-merger.tga", "w") do |f| | |
f.write(image) | |
end | |
partial_image = JSON.parse(File.read(File.dirname(__FILE__)+"/code")) | |
image = Resque::InMemoryMerger.merge_partial_images_from_tasks(partial_image) | |
File.open("/tmp/image-"+Time.at(Time.now).usec.to_s+'.tga', "w") do |f| | |
f.write(image) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Download the hash at: | |
http://dl.dropbox.com/u/834494/hash |
@Nerian I'm glad it worked :) Thats odd that slice!(0,18) did the trick. According to the docs http://ruby-doc.org/core/classes/String.html#M001213 the second argument should be length and not an index. Maybe slice! opperates slightly different.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@diedthreetimes: Since my code is based on your solution and you helped me a lot I will accept your answer :) thanks!