Guest User

Untitled

a guest
Nov 18th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require 'digest/md5'
  4. require 'optparse'
  5. require 'stringio'
  6.  
  7. module S3Etag
  8.  
  9. def calc opt = {}
  10. threshold = opt[:threshold] || 16 * 1024 * 1024
  11. max_parts = opt[:max_parts] || 10000
  12. min_part_size = opt[:min_part_size] || 5 * 1024 * 1024
  13.  
  14. if opt[:file]
  15. total_size = File.stat(opt[:file]).size
  16. io = open(opt[:file])
  17. elsif opt[:data]
  18. total_size = opt[:data].size
  19. io = StringIO.new opt[:data]
  20. else
  21. raise ':file or :data is required'
  22. end
  23.  
  24. r = nil
  25. begin
  26. if total_size <= threshold
  27. r = Digest::MD5.hexdigest(io.read)
  28. else
  29. part_size = [(total_size.to_f / max_parts).ceil, min_part_size].max.to_i
  30. md5s = []
  31. while !io.eof?
  32. md5s << Digest::MD5.digest(io.read(part_size))
  33. end
  34. r = Digest::MD5.hexdigest(md5s.join('')) + '-' + md5s.size.to_s
  35. end
  36. rescue Exception => e
  37. raise e
  38. ensure
  39. io.close
  40. end
  41. end
  42.  
  43. module_function :calc
  44. end
  45.  
  46. if __FILE__ == $0
  47. opt = {}
  48. op = OptionParser.new
  49. op.on('-t', '--threshold threshold'){ |v| opt[:threshold] = v.to_i }
  50. op.on('-p', '--max-parts max-parts'){ |v| opt[:max_parts] = v.to_i }
  51. op.on('-s', '--min_part_size min_part_size'){ |v| opt[:min_part_size] = v.to_i }
  52.  
  53. path = ARGV.last
  54. if path && File.file?(path)
  55. begin
  56. op.parse ARGV
  57. rescue OptionParser::InvalidOption => e
  58. puts op.summarize(['invalid option.', 's3etag file'], 100)
  59. else
  60. puts S3Etag.calc(opt.merge(:file => path))
  61. end
  62. else
  63. puts op.summarize(['s3etag file'], 100)
  64. end
  65. end
Add Comment
Please, Sign In to add comment