Class: PuppetForgeServer::Backends::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_forge_server/backends/proxy.rb

Direct Known Subclasses

ProxyV1, ProxyV3

Instance Method Summary collapse

Constructor Details

#initialize(url, cache_dir, http_client, file_path) ⇒ Proxy

Returns a new instance of Proxy.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/puppet_forge_server/backends/proxy.rb', line 23

def initialize(url, cache_dir, http_client, file_path)
  @url = url
  @cache_dir = File.join(cache_dir, Digest::SHA1.hexdigest(@url))
  @http_client = http_client
  @log = PuppetForgeServer::Logger.get
  @file_path = file_path

  # Create directory structure for all alphabetic letters
  (0...36).each do |i|
    FileUtils.mkdir_p(File.join(@cache_dir, i.to_s(36)))
  end
end

Instance Method Details

#get_file_buffer(relative_path) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/puppet_forge_server/backends/proxy.rb', line 36

def get_file_buffer(relative_path)
  file_name = relative_path.split('/').last
  target_file = File.join(@cache_dir, file_name[0].downcase, file_name)
  path = Dir["#{@cache_dir}/**/#{file_name}"].first
  unless File.exist?("#{path}")
    buffer = download("#{@file_path.chomp('/')}/#{relative_path}")
    File.open(target_file, 'wb') do |file|
      bytes = buffer.read
      file.write(bytes)
      @log.debug("Saved #{bytes.size} bytes in filesystem cache for path: #{relative_path}, target file: #{target_file}")
    end
    path = target_file
  else
    @log.info("Filesystem cache HIT for path: #{relative_path}")
  end
  File.open(path, 'rb')
rescue => e
  @log.error("#{self.class.name} failed downloading file '#{relative_path}'")
  @log.error("Error: #{e}")
  return nil
end

#upload(file_data) ⇒ Object



58
59
60
61
# File 'lib/puppet_forge_server/backends/proxy.rb', line 58

def upload(file_data)
  @log.debug 'File upload is not supported by the proxy backends'
  false
end