Module: Decidim::ZipStream::Writer

Included in:
DataPortabilityExporter
Defined in:
app/services/decidim/zip_stream/zip_stream_writer.rb

Instance Method Summary collapse

Instance Method Details

#add_attachments_to_zip_stream(out, export_attachments) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/services/decidim/zip_stream/zip_stream_writer.rb', line 19

def add_attachments_to_zip_stream(out, export_attachments)
  export_attachments.each do |attachment_block|
    next if attachment_block.last.nil?

    folder_name = attachment_block.first.parameterize
    attachment_block.last.each do |attachment_uploader|
      next if attachment_uploader.file.nil?

      case attachment_uploader.fog_provider
      when "fog" # file system
        next unless File.exist?(attachment_uploader.file.file)
      when "fog/aws"
        cache_attachment_from_aws(attachment_uploader)
      else
        Rails.logger.info "Carrierwave fog_provider not supported by DataPortabilityExporter for attachment: #{attachment_uploader}"
        next
      end

      attachment_local_path = attachment_uploader.file.file
      out.put_next_entry("#{folder_name}/#{attachment_uploader.file.filename}")
      File.open(attachment_local_path) do |f|
        out << f.read
      end
      CarrierWave.clean_cached_files!
    end
  end
end

#add_user_data_to_zip_stream(out, user_data) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/services/decidim/zip_stream/zip_stream_writer.rb', line 6

def add_user_data_to_zip_stream(out, user_data)
  user_data.each do |element|
    filename_file = element.last.filename(element.first.parameterize)

    out.put_next_entry(filename_file)
    if element.last.read.presence
      out.write element.last.read
    else
      out.write "No data"
    end
  end
end

#cache_attachment_from_aws(uploader) ⇒ Object

Retrieves the file from AWS and stores it into a temporal cache. Once the file is cached, the uploader returns a local ‘CarrierWave::SanitizedFile` instead of a fog file that acts as a proxy to the remote file.



50
51
52
53
# File 'app/services/decidim/zip_stream/zip_stream_writer.rb', line 50

def cache_attachment_from_aws(uploader)
  uploader.cache_stored_file!
  uploader.retrieve_from_cache!(uploader.cache_name)
end