Commit 08a02d34 authored by hh1966's avatar hh1966
Browse files

Refactor ExportAPI and add zip creation

parent fede9dc2
......@@ -68,6 +68,9 @@
/public/json/*
!/public/json/.keep
/public/zip/*
!/public/zip/.keep
/public/docx/*
!/public/docx/.keep
......
module Chemotion
class ExportAPI < Grape::API
resource :exports do
desc "Get export json for a collection"
params do
requires :collection_id, type: Integer, desc: 'collection id'
before do
# TODO: validate collection_id, check permissions
# handle nested collections
@collection_ids = params[:collection_id]
end
post do
# TODO: validate collection_id
collections_ids = [params[:collection_id]]
desc "Export collections as json"
params do
requires :collection_id, type: Array[Integer]
end
post 'json/' do
ExportCollectionsJob.perform_later('json', @collection_ids)
end
ExportCollectionJob.perform_later collections_ids
desc "Export collections as zip"
params do
requires :collection_id, type: Array[Integer]
end
post 'zip/' do
ExportCollectionsJob.perform_later('zip', @collection_ids)
end
end
end
......
class ExportCollectionJob < ActiveJob::Base
queue_as :export_collection
# rescue_from(ActiveRecord::RecordNotFound) do; end
def perform(collection_ids)
export_file_name = "#{self.job_id}.json"
export_file_name = File.join('public', 'json', export_file_name)
export = Export::ExportCollectionJson.new collection_ids
export.prepare_data.to_file(export_file_name)
end
end
class ExportCollectionsJob < ActiveJob::Base
queue_as :export_collections
# rescue_from(ActiveRecord::RecordNotFound) do; end
def perform(format, collection_ids)
# remove debug output
unless Rails.env.production?
[
'public/json/data.json',
'public/json/uuid.json',
'public/json/attachments.json'
].each do |file_name|
File.delete(file_name) if File.exist?(file_name)
end
end
# prepare the collections for export
export = Export::ExportCollection.new
export.prepare_data collection_ids
# debug output
unless Rails.env.production?
File.write('public/json/data.json', export.data.to_json())
File.write('public/json/uuid.json', export.uuids.to_json())
File.write('public/json/attachments.json', export.attachments.to_json())
end
case format
when 'json'
# write the json file public/json/
json_file = File.join('public', 'json', "#{self.job_id}.json")
File.write(json_file, export.to_json())
when 'zip'
# create a zip buffer
zip = Zip::OutputStream.write_buffer do |zip|
# write the json file into the zip file
zip.put_next_entry 'data.json'
zip.write export.to_json()
# write all attachemnts into an attachments directory
export.attachments.each do |attachment|
zip.put_next_entry File.join('attachments', attachment.filename)
zip.write attachment.read_file
end
end
zip.rewind
# write the zip file to public/zip/
zip_file = File.join('public', 'zip', "#{self.job_id}.zip")
File.write(zip_file, zip.read)
end
end
end
module Export
class ExportCollectionJson
class ExportCollection
def initialize(collection_ids)
@collection_ids = collection_ids
@uuids = {}
attr_reader :data
attr_reader :uuids
attr_reader :attachments
def initialize
@data = []
@uuids = {}
@attachments = []
end
def to_json()
@data.to_json()
end
def to_file(file_name)
file_name += '.json' if File.extname(file_name) != '.json'
File.write(file_name, to_json)
# debug output
unless Rails.env.production?
File.write('public/json/data.json', @data.to_json())
File.write('public/json/uuid.json', @uuids.to_json())
end
end
def prepare_data
def prepare_data(collection_ids)
# loop over all collections
fetch_collections.each do |collection|
fetch_collections(collection_ids).each do |collection|
# fetch samples
fetch_many(collection.samples, {
......@@ -131,6 +124,9 @@ module Export
:attachable_id => 'research_plans',
})
# add attachments to the list of attachments
@attachments += research_plan.attachments
# fetch literature
fetch_literals(research_plan)
end
......@@ -138,8 +134,8 @@ module Export
self
end
def fetch_collections
collections = Collection.find(@collection_ids)
def fetch_collections(collection_ids)
collections = Collection.find(collection_ids)
fetch_many(collections)
collections
end
......@@ -179,6 +175,9 @@ module Export
fetch_many(attachment_container.attachments, {
:attachable_id => 'containers',
})
# add attachments to the list of attachments
@attachments += attachment_container.attachments
end
end
end
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment