require 'digest/md5' require 'fog' # Fog loads these on demand. Preload this so we can monkey patch it below require 'fog/local/models/storage/files' module Fog module Storage class Local class Files < Fog::Collection # This method is exactly like the stock one except for using "rb" (force binary mode) # instead of "r" for reading the file. On windows it defaults to text mode which # messes everything up. def get(key, &block) requires :directory path = file_path(key) if ::File.exists?(path) data = { :content_length => ::File.size(path), :key => key, :last_modified => ::File.mtime(path) } if block_given? file = ::File.open(path, "rb") while (chunk = file.read(Excon::CHUNK_SIZE)) && yield(chunk); end file.close new(data) else body = ::File.open(path,"rb") { |io| io.read } new(data.merge!(:body => body)) end else nil end end end end end end module RightPublish module LocalStorage DEFAULT_LOCAL_CACHEDIR = File.expand_path('~/.right_publish/cache') STORAGE_KEY = :local_storage STORAGE_OPTIONS = { :cache_dir=>DEFAULT_LOCAL_CACHEDIR } def compute_md5(file) Digest::MD5.file(File.join(service.path_to(self.key), file.key)).hexdigest end def self.get_directories() Profile.log("Connecting to local cache.", :debug) conn = Fog::Storage.new( :provider=>"Local", :local_root=>Profile.config[STORAGE_KEY][:cache_dir] ) Profile.log("Attaching to local cache: [#{Profile.config[STORAGE_KEY][:cache_dir]}].", :debug) conn.directories.create(:key=>'.') unless File.exists? Profile.config[STORAGE_KEY][:cache_dir] local_dir = conn.directories.get('.') local_dir.extend(LocalStorage) local_dir end end RightPublish::StorageManager.register_storage(LocalStorage) end