7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/puushload/puush_downloader.rb', line 7
def self.download_file(id, subfolder, logger, retries=0)
logger.error "Too many retries, this file doesn't seem to exist." and return false if retries > MAX_RETRIES
begin
Net::HTTP.start("puu.sh") do |http|
http.request_get('/' + URI.encode(id)) do |response|
filename = PuushHelper.get_file_name_from_response(response)
return false if !filename
filename = id + " - " + filename
path = subfolder + "/" + filename
return filename if File.exists?(path)
FileUtils.mkdir_p(subfolder)
FileUtils.touch(path)
file = open path, 'wb'
response.read_body do |segment|
file.write(segment)
end
return filename
end
end
rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Errno::EHOSTUNREACH, SocketError,
Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
logger.warn "Connection error, retrying (#{retries})...: " + e.to_s
self.download_file(id, subfolder, logger, retries+1)
end
end
|