geminabox/geminabox

View on GitHub
lib/geminabox/proxy/copier.rb

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
# frozen_string_literal: true


module Geminabox
  module Proxy
    class Copier < FileHandler

      def self.copy(file_name)
        copier = new(file_name)
        copier.get_file
        copier
      end

      def get_file
        return true if proxy_file_exists?
        return copy_local if local_file_exists?
        get_remote
      end

      def copy_local
        FileUtils.cp local_path, proxy_path
      end

      def get_remote
        begin
          if rc = remote_content
            File.open(proxy_path, 'wb'){|f| f.write(rc) }
          end
        rescue
          File.unlink(proxy_path) if File.exist?(proxy_path)
          raise $!
        end
      end

    end
  end
end