Method: PulpContainerClient::ApiClient#deserialize_file

Defined in:
lib/pulp_container_client/api_client.rb

#deserialize_file(response, stream) ⇒ Object


172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/pulp_container_client/api_client.rb', line 172

def deserialize_file(response, stream)
  body     = response.body
  encoding = body.encoding

  # reconstruct content
  content = stream.join
  content = content.unpack('m').join if response.headers['Content-Transfer-Encoding'] == 'binary'
  content = content.force_encoding(encoding)

  # return byte stream
  return content if @config.return_binary_data == true

  # return file instead of binary data
  content_disposition = response.headers['Content-Disposition']
  if content_disposition && content_disposition =~ /filename=/i
    filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
    prefix = sanitize_filename(filename)
  else
    prefix = 'download-'
  end
  prefix = prefix + '-' unless prefix.end_with?('-')

  tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
  tempfile.write(content)
  tempfile.close

  config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
                      "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
                      "will be deleted automatically with GC. It's also recommended to delete the temp file "\
                      "explicitly with `tempfile.delete`"
  tempfile
end