lib/filbunke/client.rb in filbunke-1.1.4 vs lib/filbunke/client.rb in filbunke-1.1.5
- old
+ new
@@ -1,8 +1,9 @@
require 'json'
require 'net/http'
require 'fileutils'
+require 'digest/md5'
module Filbunke
class Client
attr_reader :repository
@@ -23,23 +24,26 @@
updates = get_updated_file_list(last_checkpoint)
updated_files = updates["files"] || []
failure = false
updated_files.each do |raw_file|
file = File.new(raw_file)
- if file.url =~ /^http:\/\//
- local_file_path = ::File.join(repository.local_path, file.path)
- if update_http_file!(file, local_file_path) then
- @callbacks.each do |callback|
- callback.on_update(file)
+
+ local_file_path = ::File.join(repository.local_path, file.path)
+ if file_needs_update?(file, local_file_path)
+ if file.url =~ /^http:\/\//
+ if update_http_file!(file, local_file_path) then
+ @callbacks.each do |callback|
+ callback.on_update(file)
+ end
+ yield file
+ else
+ @logger.log "Unable to get file #{file.url}/#{file.path}!"
+ failure = true
end
- yield file
else
- @logger.log "Unable to get file #{file.url}/#{file.path}!"
- failure = true
+ raise "Unsupported protocol for file: #{file.inspect}"
end
- else
- raise "Unsupported protocol for file: #{file.inspect}"
end
end
failure ? last_checkpoint : (updates["checkpoint"] || last_checkpoint)
end
@@ -72,23 +76,34 @@
end
end
private
+ def file_needs_update?(file, local_file_path)
+ return true if file.hash.nil || file.hash == ""
+
+ return true unless File.exists?(local_file_path)
+
+ local_hash = Digest::MD5.hexdigest(File.read(local_file_path))
+ local_hash != file.hash
+ end
+
def get_updated_file_list(last_checkpoint)
+
begin
updates_http = Net::HTTP.new(@repository.host, @repository.port)
updates_http.start do |http|
begin
- updates_path = "/#{UPDATES_ACTION}/#{@repository.name}?#{FROM_CHECKPOINT_KEY}=#{last_checkpoint}"
- request = Net::HTTP::Get.new(updates_path)
- response = http.request(request)
- if response.code.to_i == 200
- JSON.parse(response.body)
- else
- @logger.log "Failed to download updates for #{@repository.name}, error code = #{response.code}"
- end
+ updates_path = "/#{UPDATES_ACTION}/#{@repository.name}?#{FROM_CHECKPOINT_KEY}=#{last_checkpoint}"
+ request = Net::HTTP::Get.new(updates_path)
+ response = http.request(request)
+ if response.code.to_i == 200
+ JSON.parse(response.body)
+ else
+ @logger.log "Failed to download updates for #{@repository.name}, error code = #{response.code}"
+ {}
+ end
rescue StandardError => e
@logger.log "Error getting file list: #{e.message}! Retrying later.."
{}
end
end
@@ -98,28 +113,27 @@
end
end
def update_http_file!(file, local_file_path)
begin
- uri = URI.parse(file.url)
- file_http=Net::HTTP.new(uri.host, uri.port)
- file_http.start do |http|
- request = Net::HTTP::Get.new(uri.path)
- request.basic_auth @repository.user, @repository.pass if @repository.user
- response = http.request(request)
- if response.code.to_i == 200
- write_file!(local_file_path, response.body)
- elsif response.code.to_i == 404
- delete_file!(local_file_path)
- else
- @logger.log "Failed to update file #{uri}, error code = #{response.code}"
+ uri = URI.parse(file.url)
+ file_http=Net::HTTP.new(uri.host, uri.port)
+ file_http.start do |http|
+ request = Net::HTTP::Get.new(uri.path)
+ request.basic_auth @repository.user, @repository.pass if @repository.user
+ response = http.request(request)
+ if response.code.to_i == 200
+ write_file!(local_file_path, response.body)
+ elsif response.code.to_i == 404
+ delete_file!(local_file_path)
+ else
+ @logger.log "Failed to update file #{uri}, error code = #{response.code}"
+ end
+ return true
end
- return true
- end
rescue StandardError => e
return false
end
-
end
def write_file!(file_path, contents)
@logger.log("Writing: #{file_path}")
::FileUtils.mkdir_p(::File.dirname(file_path))