Skip to content

Instantly share code, notes, and snippets.

@ChrisHughes
Created February 18, 2016 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisHughes/f89a48743c99e135f285 to your computer and use it in GitHub Desktop.
Save ChrisHughes/f89a48743c99e135f285 to your computer and use it in GitHub Desktop.
# [MODIFICATION] Overrides components of asset sync to provide upload progress bar and remote directory listing caching
module AssetSync
class Storage
def get_cache_file
"#{Rails.root}/tmp/asset-sync-remote-cache"
end
def get_remote_files_from_cache
if self.config.cache_directory_listings && File.exists?(get_cache_file)
puts "Loading remote directory listing from cache"
remote_files = YAML::load(File.open(get_cache_file).read)
else
puts "Getting remote directory listing"
remote_files = get_remote_files
file_contents = YAML::dump(remote_files)
puts "Writing new remote directory listing cache file"
File.open(get_cache_file, 'w') do |f|
f.write file_contents
end
end
return remote_files
end
def update_local_cache_data(all_files)
puts "Appending uploads to remote directory listing cache"
file_contents = YAML::dump(all_files)
File.open(get_cache_file, 'w') do |f|
f.write file_contents
end
end
def upload_files
puts "Syncing Assets with patched Asset Sync"
puts "Directory listing..."
# fixes: https://github.com/rumblelabs/asset_sync/issues/19
remote_files = ignore_existing_remote_files? ? [] : get_remote_files_from_cache
local_files_and_directories_to_upload = local_files - ignored_files - remote_files + always_upload_files
local_files_to_upload = local_files_and_directories_to_upload.select{|f| File.file? "#{path}/#{f}"}
puts "Uploading " + local_files_to_upload.length.to_s + " files..."
pbar = ProgressBar.new("Uploading", local_files_to_upload.length, out=STDOUT)
# Upload new files
local_files_to_upload.each do |f|
#puts f.inspect
upload_file f
pbar.inc
end
puts "" # Newline after progressbar
# Maintains listing of remote files
all_files = remote_files + local_files_to_upload + always_upload_files
update_local_cache_data(all_files)
puts "Finished"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment