From 9fb30c76616d78578e933ed395d0f87fd8cbf010 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Zrim=C5=A1ek?= Date: Tue, 6 Sep 2016 10:03:27 +0200 Subject: [PATCH] Fixes files deletion when rails worker is running [fixes SCI-411] and fixes picture's style files deletion. --- app/models/asset.rb | 14 ++++++++++++-- app/models/asset_text_datum.rb | 2 +- app/models/result_asset.rb | 3 +-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/app/models/asset.rb b/app/models/asset.rb index f95259b55..8327e21bf 100644 --- a/app/models/asset.rb +++ b/app/models/asset.rb @@ -141,6 +141,10 @@ class Asset < ActiveRecord::Base !(self.file.content_type =~ /^image/).nil? end + def text? + TEXT_EXTRACT_FILE_TYPES.any? { |v| file_content_type.start_with? v } + end + # TODO: get the current_user # before_save do # if current_user @@ -158,7 +162,7 @@ class Asset < ActiveRecord::Base self.update(file_present: true) # Extract asset text if it's of correct type - if TEXT_EXTRACT_FILE_TYPES.any? { |v| file_content_type.start_with? v } + if text? Rails.logger.info "Asset #{id}: Creating extract text job" # The extract_asset_text also includes # estimated size calculation @@ -215,13 +219,19 @@ class Asset < ActiveRecord::Base # this automatically, so this is not needed in such cases) key = file.path[1..-1] S3_BUCKET.object(key).delete + Rails.logger.info "Asset #{id} (original): Asset file "\ + "successfully deleted from S3 (" + key.to_s + ')' if (file_content_type =~ %r{^image\/}) == 0 - file.options[:styles].each do |style| + file.options[:styles].each do |style, _| key = file.path(style)[1..-1] S3_BUCKET.object(key).delete + Rails.logger.info "Asset #{id} (" + style.to_s + '): Asset file '\ + 'successfully deleted from S3 (' + key.to_s + ')' end end + report_elements.destroy_all + asset_text_datum.destroy if asset_text_datum.present? delete end diff --git a/app/models/asset_text_datum.rb b/app/models/asset_text_datum.rb index 47d2f326e..e4471e4e0 100644 --- a/app/models/asset_text_datum.rb +++ b/app/models/asset_text_datum.rb @@ -3,7 +3,7 @@ class AssetTextDatum < ActiveRecord::Base validates :data, presence: true validates :asset, presence: true, uniqueness: true - belongs_to :asset + belongs_to :asset, inverse_of: :asset_text_datum after_save :update_ts_index diff --git a/app/models/result_asset.rb b/app/models/result_asset.rb index d683a9dca..a2411892e 100644 --- a/app/models/result_asset.rb +++ b/app/models/result_asset.rb @@ -2,8 +2,7 @@ class ResultAsset < ActiveRecord::Base validates :result, :asset, presence: true belongs_to :result, inverse_of: :result_asset - belongs_to :asset, inverse_of: :result_asset, - dependent: :destroy + belongs_to :asset, inverse_of: :result_asset, dependent: :destroy def space_taken asset.present? ? asset.estimated_size : 0