Class: Match::Storage::GitLab::SecureFile

Inherits:
Object
  • Object
show all
Defined in:
match/lib/match/storage/gitlab/secure_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file:, client:) ⇒ SecureFile

Returns a new instance of SecureFile.



11
12
13
14
# File 'match/lib/match/storage/gitlab/secure_file.rb', line 11

def initialize(file:, client:)
  @file   = OpenStruct.new(file)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'match/lib/match/storage/gitlab/secure_file.rb', line 9

def client
  @client
end

#fileObject (readonly)

Returns the value of attribute file.



9
10
11
# File 'match/lib/match/storage/gitlab/secure_file.rb', line 9

def file
  @file
end

Instance Method Details

#create_subfolders(working_directory) ⇒ Object



20
21
22
# File 'match/lib/match/storage/gitlab/secure_file.rb', line 20

def create_subfolders(working_directory)
  FileUtils.mkdir_p("#{working_directory}/#{destination_file_path}")
end

#deleteObject



55
56
57
58
59
60
61
# File 'match/lib/match/storage/gitlab/secure_file.rb', line 55

def delete
  url = URI(file_url)

  request = Net::HTTP::Delete.new(url.request_uri)

  @client.execute_request(url, request)
end

#destination_file_pathObject



24
25
26
27
28
# File 'match/lib/match/storage/gitlab/secure_file.rb', line 24

def destination_file_path
  filename = @file.name.split('/').last

  @file.name.gsub(filename, '').gsub(%r{^/}, '')
end

#download(working_directory) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'match/lib/match/storage/gitlab/secure_file.rb', line 34

def download(working_directory)
  url = URI("#{file_url}/download")

  begin
    destination_file = "#{working_directory}/#{@file.name}"

    create_subfolders(working_directory)
    File.open(destination_file, "wb") do |saved_file|
      URI.open(url, "rb", { @client.authentication_key => @client.authentication_value }) do |data|
        saved_file.write(data.read)
      end

      FileUtils.chmod('u=rw,go-r', destination_file)
    end

    UI.crash!("Checksum validation failed for #{@file.name}") unless valid_checksum?(destination_file)
  rescue OpenURI::HTTPError => msg
    UI.error("Unable to download #{@file.name} - #{msg}")
  end
end

#file_urlObject



16
17
18
# File 'match/lib/match/storage/gitlab/secure_file.rb', line 16

def file_url
  "#{@client.base_url}/#{@file.id}"
end

#valid_checksum?(file) ⇒ Boolean

Returns:



30
31
32
# File 'match/lib/match/storage/gitlab/secure_file.rb', line 30

def valid_checksum?(file)
  Digest::SHA256.hexdigest(File.read(file)) == @file.checksum
end