Ruby mogilefs-client 3.12.2
[ruby-mogilefs-client.git] / lib / mogilefs / paths_size.rb
blob0aa676d311262d5a6387115851518ffb32720f02
1 # -*- encoding: binary -*-
2 # This is only a hack for old MogileFS installs that didn't have file_info
3 require "net/http"
4 require "uri"
5 module MogileFS::PathsSize
6   def self.call(paths)
7     errors = {}
8     paths.each do |path|
9       uri = URI.parse(path)
10       begin
11         case r = Net::HTTP.start(uri.host, uri.port) { |x| x.head(uri.path) }
12         when Net::HTTPOK
13           return r['Content-Length'.freeze].to_i
14         else
15           errors[path] = r
16         end
17       rescue => err
18         errors[path] = err
19       end
20     end
21     errors = errors.map { |path,err| "#{path} - #{err.message} (#{err.class})" }
22     raise MogileFS::Error, "all paths failed with HEAD: #{errors.join(', ')}"
23   end
24 end