Sha256: 902e2be23564874db561143b641858a4a99b2ad34219c9450e02900ff5047ebe

Contents?: true

Size: 1.89 KB

Versions: 12

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true

require "zip"

module Decidim
  # This class performs a task: Creating a Zip file of all DataPortability classes. Originally
  # meant for DataPortability functionality and adding user images to this file, but other usage can be found.
  class DataPortabilityFileZipper < Decidim::DataPortabilityFileReader
    # Public: Initializes the zipper with a user, data, and images to
    # be zipped.
    #
    # user     - The user of data portability to be zipped.
    # data     - An array of all data to be zipped.
    # images   - An array of image urls to be inclueded in the zipped file.
    def initialize(user, data, images, token = nil)
      super(user, token)
      @export_data = data
      @export_images = images
    end

    # Public: Zips the file.
    #
    # Returns a String with the zipped version of the file.
    def make_zip
      Zip::OutputStream.open(file_path) do |zos|
        @export_data.each do |element|
          filename_file = element.last.filename(element.first.parameterize)

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

      zipfile = Zip::File.open(file_path)
      @export_images.each do |image_block|
        next if image_block.last.nil?
        image_block.last.each do |image|
          next if image.file.nil?
          folder_name = image_block.first.parameterize
          uploader = Decidim::ApplicationUploader.new(image.model, image.mounted_as)
          uploader.cache!(File.open(image.file.file))
          uploader.retrieve_from_store!(image.file.filename)
          my_image_path = File.open(image.file.file)
          next unless File.exist?(my_image_path)
          zipfile.add("#{folder_name}/#{image.file.filename}", my_image_path)
        end
      end
      zipfile.close
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
decidim-core-0.16.1 lib/decidim/data_portability_file_zipper.rb
decidim-core-0.16.0 lib/decidim/data_portability_file_zipper.rb
decidim-core-0.15.2 lib/decidim/data_portability_file_zipper.rb
decidim-core-0.15.1 lib/decidim/data_portability_file_zipper.rb
decidim-core-0.15.0 lib/decidim/data_portability_file_zipper.rb
decidim-core-0.14.4 lib/decidim/data_portability_file_zipper.rb
decidim-core-0.14.3 lib/decidim/data_portability_file_zipper.rb
decidim-core-0.14.2 lib/decidim/data_portability_file_zipper.rb
decidim-core-0.14.1 lib/decidim/data_portability_file_zipper.rb
decidim-core-0.13.1 lib/decidim/data_portability_file_zipper.rb
decidim-core-0.13.0 lib/decidim/data_portability_file_zipper.rb
decidim-core-0.13.0.pre1 lib/decidim/data_portability_file_zipper.rb