mirror of
				https://github.com/scinote-eln/scinote-web.git
				synced 2025-11-04 12:07:23 +08:00 
			
		
		
		
	Merge pull request #1358 from mz3944/mz-SCI-2646
Removal of the temporary files for export all [SCI-2646]
This commit is contained in:
		
						commit
						056efe170b
					
				
					 4 changed files with 36 additions and 22 deletions
				
			
		| 
						 | 
				
			
			@ -18,19 +18,20 @@ class TeamZipExport < ZipExport
 | 
			
		|||
 | 
			
		||||
  def generate_exportable_zip(user, data, type, options = {})
 | 
			
		||||
    @user = user
 | 
			
		||||
    FileUtils.mkdir_p(File.join(Rails.root, 'tmp/zip-ready'))
 | 
			
		||||
    dir_to_zip = FileUtils.mkdir_p(
 | 
			
		||||
    zip_input_dir = FileUtils.mkdir_p(
 | 
			
		||||
      File.join(Rails.root, "tmp/temp-zip-#{Time.now.to_i}")
 | 
			
		||||
    ).first
 | 
			
		||||
    output_file = File.new(
 | 
			
		||||
      File.join(Rails.root,
 | 
			
		||||
                "tmp/zip-ready/projects-export-#{Time.now.to_i}.zip"),
 | 
			
		||||
    zip_dir = FileUtils.mkdir_p(File.join(Rails.root, 'tmp/zip-ready')).first
 | 
			
		||||
    zip_file = File.new(
 | 
			
		||||
      File.join(zip_dir, "projects-export-#{Time.now.to_i}.zip"),
 | 
			
		||||
      'w+'
 | 
			
		||||
    )
 | 
			
		||||
    fill_content(dir_to_zip, data, type, options)
 | 
			
		||||
    zip!(dir_to_zip, output_file.path)
 | 
			
		||||
    self.zip_file = File.open(output_file)
 | 
			
		||||
    fill_content(zip_input_dir, data, type, options)
 | 
			
		||||
    zip!(zip_input_dir, zip_file)
 | 
			
		||||
    self.zip_file = File.open(zip_file)
 | 
			
		||||
    generate_notification(user) if save
 | 
			
		||||
  ensure
 | 
			
		||||
    FileUtils.rm_rf([zip_input_dir, zip_file], secure: true)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  handle_asynchronously :generate_exportable_zip
 | 
			
		||||
| 
						 | 
				
			
			@ -128,6 +129,9 @@ class TeamZipExport < ZipExport
 | 
			
		|||
      file = FileUtils.touch("#{root}/#{pdf_name}").first
 | 
			
		||||
      File.open(file, 'wb') { |f| f.write(project_report_pdf) }
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    # Change current dir outside tmp_dir, since tmp_dir will be deleted
 | 
			
		||||
    Dir.chdir(File.join(Rails.root, 'tmp'))
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def generate_notification(user)
 | 
			
		||||
| 
						 | 
				
			
			@ -252,7 +256,7 @@ class TeamZipExport < ZipExport
 | 
			
		|||
    # Don't zip current/above directory
 | 
			
		||||
    files.delete_if { |el| ['.', '..'].include?(el) }
 | 
			
		||||
 | 
			
		||||
    Zip::File.open(output_file, Zip::File::CREATE) do |zipfile|
 | 
			
		||||
    Zip::File.open(output_file.path, Zip::File::CREATE) do |zipfile|
 | 
			
		||||
      write_entries(input_dir, files, '', zipfile)
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,3 +1,5 @@
 | 
			
		|||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
require 'zip'
 | 
			
		||||
require 'fileutils'
 | 
			
		||||
require 'csv'
 | 
			
		||||
| 
						 | 
				
			
			@ -47,18 +49,20 @@ class ZipExport < ApplicationRecord
 | 
			
		|||
  end
 | 
			
		||||
 | 
			
		||||
  def generate_exportable_zip(user, data, type, options = {})
 | 
			
		||||
    FileUtils.mkdir_p(File.join(Rails.root, 'tmp/zip-ready'))
 | 
			
		||||
    dir_to_zip = FileUtils.mkdir_p(
 | 
			
		||||
    zip_input_dir = FileUtils.mkdir_p(
 | 
			
		||||
      File.join(Rails.root, "tmp/temp-zip-#{Time.now.to_i}")
 | 
			
		||||
    ).first
 | 
			
		||||
    output_file = File.new(
 | 
			
		||||
      File.join(Rails.root, "tmp/zip-ready/export-#{Time.now.to_i}.zip"),
 | 
			
		||||
    zip_dir = FileUtils.mkdir_p(File.join(Rails.root, 'tmp/zip-ready')).first
 | 
			
		||||
    zip_file = File.new(
 | 
			
		||||
      File.join(zip_dir, "export-#{Time.now.to_i}.zip"),
 | 
			
		||||
      'w+'
 | 
			
		||||
    )
 | 
			
		||||
    fill_content(dir_to_zip, data, type, options)
 | 
			
		||||
    zip!(dir_to_zip, output_file.path)
 | 
			
		||||
    self.zip_file = File.open(output_file)
 | 
			
		||||
    fill_content(zip_input_dir, data, type, options)
 | 
			
		||||
    zip!(zip_input_dir, zip_file)
 | 
			
		||||
    self.zip_file = File.open(zip_file)
 | 
			
		||||
    generate_notification(user) if save
 | 
			
		||||
  ensure
 | 
			
		||||
    FileUtils.rm_rf([zip_input_dir, zip_file], secure: true)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  handle_asynchronously :generate_exportable_zip
 | 
			
		||||
| 
						 | 
				
			
			@ -96,7 +100,7 @@ class ZipExport < ApplicationRecord
 | 
			
		|||
  def zip!(input_dir, output_file)
 | 
			
		||||
    files = Dir.entries(input_dir)
 | 
			
		||||
    files.delete_if { |el| el == '..' || el == '.' }
 | 
			
		||||
    Zip::File.open(output_file, Zip::File::CREATE) do |zipfile|
 | 
			
		||||
    Zip::File.open(output_file.path, Zip::File::CREATE) do |zipfile|
 | 
			
		||||
      files.each do |filename|
 | 
			
		||||
        zipfile.add(filename, input_dir + '/' + filename)
 | 
			
		||||
      end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,4 +1,7 @@
 | 
			
		|||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
require 'csv'
 | 
			
		||||
 | 
			
		||||
module RepositoryZipExport
 | 
			
		||||
  def self.generate_zip(params, repository, current_user)
 | 
			
		||||
    # Fetch rows in the same order as in the currently viewed datatable
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -65,12 +65,15 @@ describe RepositoryZipExport, type: :background_job do
 | 
			
		|||
 | 
			
		||||
    it 'generates a zip with csv file with exported rows' do
 | 
			
		||||
      RepositoryZipExport.generate_zip(params, repository, user)
 | 
			
		||||
      zip = ZipExport.first.zip_file_file_name
 | 
			
		||||
                     .gsub('export-', '')
 | 
			
		||||
                     .gsub('.zip', '')
 | 
			
		||||
      csv_path = Rails.root.join('tmp', "temp-zip-#{zip}", 'export.csv').to_s
 | 
			
		||||
      csv_zip_file = ZipExport.first.zip_file
 | 
			
		||||
      parsed_csv_content = Zip::File.open(csv_zip_file.path) do |zip_file|
 | 
			
		||||
        csv_file = zip_file.glob('*.csv').first
 | 
			
		||||
        csv_content = csv_file.get_input_stream.read
 | 
			
		||||
        CSV.parse(csv_content, headers: true)
 | 
			
		||||
      end
 | 
			
		||||
      index = 0
 | 
			
		||||
      CSV.foreach(csv_path, headers: true) do |row|
 | 
			
		||||
 | 
			
		||||
      parsed_csv_content.each do |row|
 | 
			
		||||
        row_hash = row.to_h
 | 
			
		||||
        expect(row_hash.fetch('Sample group')).to eq 'item one'
 | 
			
		||||
        expect(row_hash.fetch('Custom items')).to eq 'custum column value'
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue