Sha256: 0fcbf94b647d7ac69928ffec2b3efb421115bc1ec4d35ec3e0996c09e5cb2be0

Contents?: true

Size: 1.7 KB

Versions: 28

Compression:

Stored size: 1.7 KB

Contents

require 'open-uri'

require_relative '../../module'

module Match
  module Storage
    class GitLab
      class SecureFile
        attr_reader :client, :file

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

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

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

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

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

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

        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

        def delete
          url = URI(file_url)

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

          @client.execute_request(url, request)
        end
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 4 rubygems

Version Path
fastlane-2.225.0 match/lib/match/storage/gitlab/secure_file.rb
fastlane-2.224.0 match/lib/match/storage/gitlab/secure_file.rb
fastlane-2.223.1 match/lib/match/storage/gitlab/secure_file.rb
fastlane-2.223.0 match/lib/match/storage/gitlab/secure_file.rb
fastlane-2.222.0 match/lib/match/storage/gitlab/secure_file.rb
fastlane-2.221.1 match/lib/match/storage/gitlab/secure_file.rb
fastlane-2.221.0 match/lib/match/storage/gitlab/secure_file.rb
fastlane-2.220.0 match/lib/match/storage/gitlab/secure_file.rb
fastlane-2.219.0 match/lib/match/storage/gitlab/secure_file.rb
fastlane-2.218.0 match/lib/match/storage/gitlab/secure_file.rb
fastlane-security-patched-2.216.0 match/lib/match/storage/gitlab/secure_file.rb
fastlane-2.217.0 match/lib/match/storage/gitlab/secure_file.rb
fastlane-2.216.0 match/lib/match/storage/gitlab/secure_file.rb
fastlane-2.215.1 match/lib/match/storage/gitlab/secure_file.rb
fastlane-2.215.0 match/lib/match/storage/gitlab/secure_file.rb
fastlane-mercafacil-2.214.0 match/lib/match/storage/gitlab/secure_file.rb
fastlane-2.214.0 match/lib/match/storage/gitlab/secure_file.rb
fastlane-2.213.0 match/lib/match/storage/gitlab/secure_file.rb
fastlane-2.212.2 match/lib/match/storage/gitlab/secure_file.rb
fastlane_pricing_fix-2.212.1 match/lib/match/storage/gitlab/secure_file.rb