mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-09-20 14:45:56 +08:00
Add export all functionality (basic skeleton)
This commit is contained in:
parent
27e0d27862
commit
295c6a9432
|
@ -223,6 +223,15 @@ class TeamsController < ApplicationController
|
|||
redirect_back(fallback_location: root_path)
|
||||
end
|
||||
|
||||
def export_all
|
||||
if export_params[:project_ids]
|
||||
TeamZipExporter.generate_zip(export_params, @team, current_user)
|
||||
else
|
||||
flash[:alert] = t('zip_export.export_error')
|
||||
end
|
||||
redirect_back(fallback_location: root_path)
|
||||
end
|
||||
|
||||
def routing_error(error = 'Routing error', status = :not_found, exception=nil)
|
||||
redirect_to root_path
|
||||
end
|
||||
|
@ -280,4 +289,5 @@ class TeamsController < ApplicationController
|
|||
:samples
|
||||
)
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -60,16 +60,58 @@ class ZipExport < ApplicationRecord
|
|||
generate_notification(user) if save
|
||||
end
|
||||
|
||||
handle_asynchronously :generate_exportable_zip
|
||||
def generate_export_all_zip(user, data, type, options = {})
|
||||
FileUtils.mkdir_p(File.join(Rails.root, 'tmp/zip-ready'))
|
||||
dir_to_zip = 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/export123-#{Time.now.to_i}.zip"),
|
||||
'w+'
|
||||
)
|
||||
fill_content(dir_to_zip, data, type, options)
|
||||
zip_all!(dir_to_zip, output_file.path)
|
||||
self.zip_file = File.open(output_file)
|
||||
generate_notification(user) if save
|
||||
end
|
||||
|
||||
#handle_asynchronously :generate_exportable_zip
|
||||
|
||||
private
|
||||
|
||||
def method_missing(m, *args, &block)
|
||||
puts 'Method is missing! To use this zip_export you have to ' \
|
||||
'define a method: generate_( type )_zip.'
|
||||
object.public_send(m, *args, &block)
|
||||
# Zip the input directory.
|
||||
def write()
|
||||
entries = Dir.entries(@inputDir); entries.delete("."); entries.delete("..")
|
||||
io = Zip::File.open(@outputFile, Zip::File::CREATE);
|
||||
|
||||
writeEntries(entries, "", io)
|
||||
io.close();
|
||||
end
|
||||
|
||||
# A helper method to make the recursion work.
|
||||
private
|
||||
def writeEntries(input_dir, entries, path, io)
|
||||
|
||||
entries.each { |e|
|
||||
zipFilePath = path == "" ? e : File.join(path, e)
|
||||
diskFilePath = File.join(input_dir, zipFilePath)
|
||||
puts "Deflating " + diskFilePath
|
||||
if File.directory?(diskFilePath)
|
||||
io.mkdir(zipFilePath)
|
||||
subdir =Dir.entries(diskFilePath); subdir.delete("."); subdir.delete("..")
|
||||
writeEntries(input_dir, subdir, zipFilePath, io)
|
||||
else
|
||||
io.get_output_stream(zipFilePath) { |f| f.puts(File.open(diskFilePath, "rb").read())}
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
#def method_missing(m, *args, &block)
|
||||
#puts 'Method is missing! To use this zip_export you have to ' \
|
||||
#'define a method: generate_( type )_zip.'
|
||||
#object.public_send(m, *args, &block)
|
||||
#end
|
||||
|
||||
def respond_to_missing?(method_name, include_private = false)
|
||||
method_name.to_s.start_with?(' generate_') || super
|
||||
end
|
||||
|
@ -102,6 +144,25 @@ class ZipExport < ApplicationRecord
|
|||
end
|
||||
end
|
||||
|
||||
def zip_all!(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|
|
||||
#files.each do |filename|
|
||||
#zipfile.add(filename, input_dir + '/' + filename)
|
||||
#end
|
||||
|
||||
writeEntries(input_dir, files, "", zipfile)
|
||||
end
|
||||
end
|
||||
|
||||
def handle_asset_name(name, type)
|
||||
if type == :step
|
||||
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
def generate_samples_zip(tmp_dir, data, _options = {})
|
||||
file = FileUtils.touch("#{tmp_dir}/export.csv").first
|
||||
File.open(file, 'wb') { |f| f.write(data) }
|
||||
|
@ -111,4 +172,90 @@ class ZipExport < ApplicationRecord
|
|||
file = FileUtils.touch("#{tmp_dir}/export.csv").first
|
||||
File.open(file, 'wb') { |f| f.write(data) }
|
||||
end
|
||||
|
||||
MAX_NAME_SIZE = 20
|
||||
def handle_name(name)
|
||||
# Handle reserved directories
|
||||
if name == '..'
|
||||
return '__'
|
||||
elsif name == '.'
|
||||
return '.'
|
||||
end
|
||||
|
||||
# Truncate and replace reserved characters
|
||||
name = name[0, MAX_NAME_SIZE].gsub(/[*":<>?\/\\|~]/, '_')
|
||||
|
||||
# Remove control characters
|
||||
name = name.chars.map{|s| s.ord}.select{|s| (s > 31 && s < 127) || s > 127}.pack("U*")
|
||||
|
||||
# Remove leading hyphens, trailing dots/spaces
|
||||
name.gsub(/^-|\.+$| +$/, '_')
|
||||
end
|
||||
|
||||
def export_assets(assets, directory)
|
||||
# Helper method to extract given assets to the directory
|
||||
assets.each do |asset|
|
||||
file = FileUtils.touch("#{directory}/#{asset.file_file_name}").first
|
||||
File.open(file, 'wb') { |f| f.write(asset.open.read) }
|
||||
end
|
||||
end
|
||||
|
||||
def export_tables(tables, directory)
|
||||
# Helper method to extract given tables to the directory
|
||||
tables.each do |table|
|
||||
file = FileUtils.touch("#{directory}/#{table.name}.csv").first
|
||||
File.open(file, 'wb') { |f| f.write(table.contents) }
|
||||
end
|
||||
end
|
||||
|
||||
def generate_teams_zip(tmp_dir, data, _options = {})
|
||||
# Export all functionality
|
||||
# Create team folder
|
||||
team = _options[:team]
|
||||
team_path = "#{tmp_dir}/#{handle_name(team.name)}"
|
||||
FileUtils.mkdir_p(team_path)
|
||||
|
||||
# Create Projects folders
|
||||
FileUtils.mkdir_p("#{team_path}/Projects")
|
||||
FileUtils.mkdir_p("#{team_path}/Archived projects")
|
||||
|
||||
# Iterate through every project
|
||||
data.each do |id, p|
|
||||
project_name = handle_name(p.name)
|
||||
root = p.archived ? "#{team_path}/Archived projects" : "#{team_path}/Projects"
|
||||
root += "/#{project_name}"
|
||||
FileUtils.mkdir_p(root)
|
||||
|
||||
file = FileUtils.touch("#{root}/#{project_name}_REPORT.pdf").first
|
||||
|
||||
inventories = "#{root}/Inventories"
|
||||
FileUtils.mkdir_p(inventories)
|
||||
|
||||
# Include all experiments
|
||||
p.experiments.each do |ex|
|
||||
experiment_path = "#{root}/#{handle_name(ex.name)}"
|
||||
FileUtils.mkdir_p(experiment_path)
|
||||
|
||||
# Include all modules
|
||||
ex.my_modules.each do |my_module|
|
||||
my_module_path = "#{experiment_path}/#{handle_name(my_module.name)}"
|
||||
FileUtils.mkdir_p(my_module_path)
|
||||
|
||||
protocol_path = "#{my_module_path}/Protocol attachments"
|
||||
result_path = "#{my_module_path}/Result attachments"
|
||||
FileUtils.mkdir_p(protocol_path)
|
||||
FileUtils.mkdir_p(result_path)
|
||||
|
||||
|
||||
steps = my_module.protocols.map{ |p| p.steps }.flatten
|
||||
export_assets(StepAsset.where(step: steps).map {|s| s.asset}, protocol_path)
|
||||
export_tables(StepTable.where(step: steps).map {|s| s.table}, protocol_path)
|
||||
|
||||
export_assets(ResultAsset.where(result: my_module.results).map {|r| r.asset}, result_path)
|
||||
export_tables(ResultTable.where(result: my_module.results).map {|r| r.table}, result_path)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
20
app/services/team_zip_exporter.rb
Normal file
20
app/services/team_zip_exporter.rb
Normal file
|
@ -0,0 +1,20 @@
|
|||
module TeamZipExporter
|
||||
def self.generate_zip(params, team, current_user)
|
||||
Rails.logger.warn('Exporting team zip')
|
||||
|
||||
project_ids = params[:project_ids]
|
||||
ids = Project.where(id: project_ids,
|
||||
team_id: team)
|
||||
.index_by(&:id)
|
||||
|
||||
options = {team: team}
|
||||
zip = ZipExport.create(user: current_user)
|
||||
zip.generate_export_all_zip(
|
||||
current_user,
|
||||
ids,
|
||||
:teams,
|
||||
options
|
||||
)
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in a new issue