Skip to content

Commit

Permalink
crude file upload with automatic splitting into smaller parts and reb…
Browse files Browse the repository at this point in the history
…uilding files from them. CRUDE CRUDE CRUDE (working proof of concept that needs refactoring) but awesome
  • Loading branch information
tomash committed Nov 4, 2010
1 parent 4c9f8a5 commit 77e3a11
Showing 1 changed file with 48 additions and 3 deletions.
51 changes: 48 additions & 3 deletions lib/flyingv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,53 @@ def self.post(key, value)
end

def self.post_file(key, path)
value = File.open(path).read
self.post(key, value)
size = File.size(path)
content = File.open(path).read
original_name = File.basename(path)
self.post(key, {"content" => content, "original_name" => original_name})
end

def self.put_file(key, path)
size = File.size(path)
chunks = (size / 2048.0).ceil
return self.post_file(key, path) if(chunks == 1)
original_name = File.basename(path)
content = File.open(path).read
next_chunk_key = nil
chunk_keys = []
sanity_check = ""
chunks.downto(1) do |i|
k = "#{key}_chunk_#{i}"
chunk_keys << k
if(i == chunks)
chunk_content = content[((i-1)*2048)..-1]
else
chunk_content = content[((i-1)*2048)..(i*2048-1)]
end
sanity_check += chunk_content
puts "storing key: #{k}, content size #{chunk_content.size}"
self.post(k, {
"content" => chunk_content,
"master_key" => key,
"chunk" => i,
"next" => next_chunk_key})
next_chunk_key = k
end
puts "sanity check size = #{sanity_check.size}, content size = #{content.size}"
self.post(key, {
"original_name" => original_name,
"chunk_keys" => chunk_keys.reverse})
end

def self.get_file(key, target_path)
master = self.get(key)
f = File.open(target_path, "w")
master["chunk_keys"].each do |chunk_key|
chunk = self.get(chunk_key)
puts "master key sanity check PASSED! -- getting #{chunk_key}" if(chunk["master_key"] == key)
f.write(chunk["content"])
end
f.close
puts "complete"
end

end

0 comments on commit 77e3a11

Please sign in to comment.