Method: OpenC3::TargetModel.download

Defined in:
lib/openc3/models/target_model.rb

.download(name, scope:) ⇒ Object



168
169
170
171
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/openc3/models/target_model.rb', line 168

def self.download(name, scope:)
  tmp_dir = Dir.mktmpdir
  zip_filename = File.join(tmp_dir, "#{name}.zip")
  Zip.continue_on_exists_proc = true
  zip = Zip::File.open(zip_filename, Zip::File::CREATE)

  rubys3_client = Aws::S3::Client.new
  token = nil
  # The trailing slash is important!
  prefix = "#{scope}/targets_modified/#{name}/"
  while true
    resp = rubys3_client.list_objects_v2({
      bucket: 'config',
      max_keys: 1000,
      prefix: prefix,
      continuation_token: token
    })
    resp.contents.each do |item|
      # item.key looks like DEFAULT/targets_modified/INST/screens/blah.txt
      base_path = item.key.sub(prefix, '') # remove prefix
      local_path = File.join(tmp_dir, base_path)
      # Ensure dir structure exists, get_object fails if not
      FileUtils.mkdir_p(File.dirname(local_path))
      rubys3_client.get_object(bucket: 'config', key: item.key, response_target: local_path)
      zip.add(base_path, local_path)
    end
    break unless resp.is_truncated
    token = resp.next_continuation_token
  end
  zip.close

  result = OpenStruct.new
  result.filename = File.basename(zip_filename)
  result.contents = File.read(zip_filename, mode: 'rb')
  return result
end