Module: Daytona::FileTransfer

Defined in:
lib/daytona/file_transfer.rb

Class Method Summary collapse

Class Method Details

.extract_multipart_boundary(content_type) ⇒ Object



137
138
139
140
141
142
# File 'lib/daytona/file_transfer.rb', line 137

def self.extract_multipart_boundary(content_type)
  match = content_type&.match(/boundary=(?:"([^"]+)"|([^;]+))/i)
  return unless match

  match.captures.compact.first
end

.stream_download(api_client:, remote_path:, timeout:) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength

Raises:



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/daytona/file_transfer.rb', line 144

def self.stream_download(api_client:, remote_path:, timeout:, &) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  config = api_client.config
  parser = MultipartDownloadStreamParser.new(&)
  response = nil

  request = Typhoeus::Request.new(
    "#{config.base_url}/files/bulk-download",
    method: :post,
    headers: api_client.default_headers.dup.merge(
      'Accept' => 'multipart/form-data',
      'Content-Type' => 'application/json'
    ),
    body: JSON.generate(paths: [remote_path]),
    timeout: timeout,
    ssl_verifypeer: config.verify_ssl,
    ssl_verifyhost: config.verify_ssl_host ? 2 : 0
  )

  request.on_headers do |stream_response|
    boundary = extract_multipart_boundary(stream_response.headers['Content-Type'])
    raise Sdk::Error, 'Missing multipart boundary in download response' unless boundary

    parser.boundary_token = boundary
  end

  request.on_body do |chunk|
    parser << chunk
  end

  request.on_complete do |completed_response|
    response = completed_response
    parser.finish!
  end

  request.run

  raise Sdk::Error, parser.error_message if parser.error_message
  raise Sdk::Error, "HTTP #{response.code}" if response && !response.success?
end