<commit_before>class ExportSite

  def initialize(options = {})
    @site = options[:site]
    @templates = @site.templates.includes(:template_fields, :webpages => [:template])
  end

  def call
    @key = SecureRandom.hex(24)
    dir = Rails.root.join('tmp', 'exports', @key, @site.slug).to_s
    zip_file = Rails.root.join('tmp', 'exports', @key, "#{@site.slug}.zip").to_s
    FileUtils.mkdir_p(dir)
    CSV.open("#{dir}/#{@site.slug}.csv", 'w+') do |csv|
      csv << %w(Title Path Template)
      @site.webpages.each do |page|
        csv << [page.title, page.page_path, page.template.title]
      end
    end
    @templates.each do |template|
      fields = { 'page_path' => 'Page Path' }
      template.fields.each { |field| fields[field.slug] = field.title }
      fields = fields.sort.to_h
      FileUtils.mkdir_p("#{dir}/templates")
      CSV.open("#{dir}/templates/#{template.slug}.csv", 'w+') do |csv|
        csv << fields.values
        template.webpages.each do |page|
          attrs = []
          fields.each { |field, label| attrs << page.send(field) }
          csv << attrs
        end
      end
    end
    ZipFileGenerator.call(dir, zip_file)
    true
  end

  def self.call(options = {})
    new(options).call
  end

end
<commit_msg>add media to package (#313)<commit_after>class ExportSite

  def initialize(options = {})
    @site = options[:site]
    @templates = @site.templates.includes(:template_fields, :webpages => [:template])
  end

  def call
    @key = SecureRandom.hex(24)
    dir = Rails.root.join('tmp', 'exports', @key, @site.slug).to_s
    zip_file = Rails.root.join('tmp', 'exports', @key, "#{@site.slug}.zip").to_s
    FileUtils.mkdir_p(dir)
    CSV.open("#{dir}/#{@site.slug}.csv", 'w+') do |csv|
      csv << %w(Title Path Template)
      @site.webpages.each do |page|
        csv << [page.title, page.page_path, page.template.title]
      end
    end
    @templates.each do |template|
      fields = { 'page_path' => 'Page Path' }
      template.fields.each { |field| fields[field.slug] = field.title }
      fields = fields.sort.to_h
      FileUtils.mkdir_p("#{dir}/templates")
      CSV.open("#{dir}/templates/#{template.slug}.csv", 'w+') do |csv|
        csv << fields.values
        template.webpages.each do |page|
          attrs = []
          fields.each { |field, label| attrs << page.send(field) }
          csv << attrs
        end
      end
    end
    FileUtils.mkdir_p(docs_dir = "#{dir}/media")
    @site.documents.each do |doc|
      open("#{docs_dir}/#{doc.document.name}", 'wb') do |file|
        file << open(doc.document.url).read
      end
    end
    ZipFileGenerator.call(dir, zip_file)
    true
  end

  def self.call(options = {})
    new(options).call
  end

end

{'fraction_non_alphanumeric': 0.16788588149231895, 'fraction_numerical': 0.002560351133869788, 'mean_word_length': 2.6662198391420913, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2104201', 'n_tokens_mistral': 953, 'n_tokens_neox': 937, 'n_words': 189}