-
-
Save cfeckardt/addf267196d7173fa5037e3f7ddd0b83 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SwiftSync | |
attr_reader :src_directory, :dest_directory | |
def initialize(src_directory:, dest_directory:, shallow: false, since_days: nil) | |
@src_directory = src_directory | |
@dest_directory = dest_directory | |
@since_days = since_days | |
@shallow = shallow | |
end | |
def sync(total = nil) | |
size_so_far = 0 | |
file_count = files_to_sync.length | |
files_to_sync.each.with_index do |file, idx| | |
temp = Tempfile.new("#{SecureRandom.hex(20)}") | |
# Streaming download in 1MB chunks | |
src_directory.files.get(file[:name]) do |data, remaining, size| | |
temp.syswrite data | |
end | |
temp.rewind | |
str = "Syncing file: #{file[:name]} - (#{idx+1} of #{file_count})" | |
if total != nil | |
str = str + " -- #{size_so_far.to_f / total.to_f}%" | |
end | |
puts str | |
dest_directory.files.create key: file[:name], body: temp | |
size_so_far = size_so_far + file[:content_length] | |
end | |
end | |
def files_to_sync | |
@files ||= Set.new(filtered_src_files.map { |f| { name: f.key, content_length: f.content_length }}) - | |
Set.new(dest_directory.files.map { |f| { name: f.key, content_length: f.content_length }}) | |
end | |
def filtered_src_files | |
files = src_directory.files | |
if @shallow | |
files = files.select { |x| x.key.include? 'document/' } | |
end | |
if @since_days | |
files = files.select { |x| x.last_modified > @since_days.days.ago } | |
end | |
files | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment