Improve files handling in protocol clonning [SCI-2003]

This commit is contained in:
Oleksii Kriuchykhin 2018-02-01 15:44:08 +01:00
parent def08a3faa
commit 16e1d072b9
4 changed files with 46 additions and 47 deletions

View file

@ -73,7 +73,6 @@ gem 'scenic', '~> 1.4'
gem 'paperclip', '~> 5.1' # File attachment, image attachment library
gem 'aws-sdk', '~> 2'
gem 'aws-sdk-v1'
gem 'delayed_job_active_record'
gem 'devise-async',

View file

@ -110,9 +110,6 @@ GEM
jmespath (~> 1.0)
aws-sdk-resources (2.10.123)
aws-sdk-core (= 2.10.123)
aws-sdk-v1 (1.67.0)
json (~> 1.4)
nokogiri (~> 1)
aws-sigv4 (1.0.2)
babel-source (5.8.35)
babel-transpiler (0.7.0)
@ -287,7 +284,7 @@ GEM
mimemagic (0.3.2)
mini_mime (1.0.0)
mini_portile2 (2.3.0)
minitest (5.11.1)
minitest (5.11.3)
momentjs-rails (2.17.1)
railties (>= 3.1)
multi_json (1.13.1)
@ -307,7 +304,7 @@ GEM
hashie (>= 3.4.6, < 3.6.0)
rack (>= 1.6.2, < 3)
orm_adapter (0.5.0)
paperclip (5.1.0)
paperclip (5.2.1)
activemodel (>= 4.2.0)
activesupport (>= 4.2.0)
cocaine (~> 0.5.5)
@ -516,7 +513,6 @@ DEPENDENCIES
autosize-rails
awesome_print
aws-sdk (~> 2)
aws-sdk-v1
base62
bcrypt (~> 3.1.10)
better_errors
@ -607,7 +603,7 @@ DEPENDENCIES
yomu
RUBY VERSION
ruby 2.4.1p111
ruby 2.4.3p205
BUNDLED WITH
1.16.1

View file

@ -221,32 +221,37 @@ class Protocol < ApplicationRecord
assets_to_clone.each do |src_id, dest_id|
src = Asset.find_by_id(src_id)
dest = Asset.find_by_id(dest_id)
if src.present? and dest.present? then
# Clone file
dest.file = src.file
dest.save
dest.destroy! if src.blank? && dest.present?
next unless src.present? && dest.present?
# Clone file
dest.file = src.file
dest.save!
# Clone extracted text data if it exists
if (atd = src.asset_text_datum).present? then
atd2 = AssetTextDatum.new(
data: atd.data,
asset: dest
)
atd2.save
end
# Update estimated size of cloned asset
# (& file_present flag)
dest.update(
estimated_size: src.estimated_size,
file_present: true
)
# Update team's space taken
team.reload
team.take_space(dest.estimated_size)
team.save
if dest.is_image?
dest.file.reprocess!(:large)
dest.file.reprocess!(:medium)
end
# Clone extracted text data if it exists
if (atd = src.asset_text_datum).present?
atd2 = AssetTextDatum.new(
data: atd.data,
asset: dest
)
atd2.save!
end
# Update estimated size of cloned asset
# (& file_present flag)
dest.update(
estimated_size: src.estimated_size,
file_present: true
)
# Update team's space taken
team.reload
team.take_space(dest.estimated_size)
team.save!
end
end
@ -271,18 +276,19 @@ class Protocol < ApplicationRecord
position: step.position,
completed: false,
user: current_user,
protocol: dest)
step2.save
protocol: dest
)
step2.save!
# Copy checklists
step.checklists.asc.each do |checklist|
checklist2 = Checklist.new(
name: checklist.name,
step: step2
)
)
checklist2.created_by = current_user
checklist2.last_modified_by = current_user
checklist2.save
checklist2.save!
checklist.checklist_items.each do |item|
item2 = ChecklistItem.new(
@ -293,7 +299,7 @@ class Protocol < ApplicationRecord
)
item2.created_by = current_user
item2.last_modified_by = current_user
item2.save
item2.save!
end
step2.checklists << checklist2
@ -309,13 +315,9 @@ class Protocol < ApplicationRecord
asset2.team = dest.team
asset2.last_modified_by = current_user
asset2.file_processing = true if asset.is_image?
asset2.save
asset2.save!
step2.assets << asset2
if asset.is_image?
asset2.file.delay.reprocess!(:large)
asset2.file.delay.reprocess!(:medium)
end
assets_to_clone << [asset.id, asset2.id]
end

View file

@ -1,8 +1,10 @@
if ENV['AWS_ACCESS_KEY_ID'] then
Aws.config.update({
if ENV['AWS_ACCESS_KEY_ID']
Aws.config.update(
region: ENV['AWS_REGION'],
credentials: Aws::Credentials.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY']),
})
credentials: Aws::Credentials.new(
ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY']
)
)
S3_BUCKET = Aws::S3::Resource.new.bucket(ENV['S3_BUCKET'])
end
end