Class: Decidim::DataPortabilityExporter

Inherits:
Object
  • Object
show all
Includes:
ZipStream::Writer
Defined in:
decidim-core/app/services/decidim/data_portability_exporter.rb

Overview

Public: Generates a 7z(seven zip) file with data files ready to be persisted somewhere so users can download their data.

In fact, the 7z file wraps a ZIP file which finally contains the data files.

Constant Summary collapse

DEFAULT_EXPORT_FORMAT =
"CSV"
ZIP_FILE_NAME =
"data-portability.zip"

Instance Method Summary collapse

Methods included from ZipStream::Writer

#add_attachments_to_zip_stream, #add_user_data_to_zip_stream, #cache_attachment_from_aws

Constructor Details

#initialize(user, path, password, export_format = DEFAULT_EXPORT_FORMAT) ⇒ DataPortabilityExporter

Public: Initializes the class.

user - The user to export the data from. path - The String path where to write the zip file. password - The password to protect the zip file. export_format - The format of the data files inside the zip file. (CSV by default)


24
25
26
27
28
29
# File 'decidim-core/app/services/decidim/data_portability_exporter.rb', line 24

def initialize(user, path, password, export_format = DEFAULT_EXPORT_FORMAT)
  @user = user
  @path = File.expand_path path
  @export_format = export_format
  @password = password
end

Instance Method Details

#exportObject


31
32
33
34
35
36
37
38
39
40
# File 'decidim-core/app/services/decidim/data_portability_exporter.rb', line 31

def export
  dirname = File.dirname(@path)
  FileUtils.mkdir_p(dirname) unless File.directory?(dirname)
  File.open(@path, "wb") do |file|
    SevenZipRuby::Writer.open(file, password: @password) do |szw|
      szw.header_encryption = true
      szw.add_data(data, ZIP_FILE_NAME)
    end
  end
end