diff --git a/.rubocop.yml b/.rubocop.yml index b94e63629..af6fc7333 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -5,7 +5,7 @@ AllCops: - "vendor/**/*" - "db/schema.rb" UseCache: false - TargetRubyVersion: 2.5 + TargetRubyVersion: 2.6 ##################### Style #################################### diff --git a/app/controllers/assets_controller.rb b/app/controllers/assets_controller.rb index 00c5e918a..60caa59a6 100644 --- a/app/controllers/assets_controller.rb +++ b/app/controllers/assets_controller.rb @@ -63,7 +63,7 @@ class AssetsController < ApplicationController def file_preview response_json = { 'id' => @asset.id, - 'type' => (@asset.is_image? ? 'image' : 'file'), + 'type' => (@asset.image? ? 'image' : 'file'), 'filename' => truncate(escape_input(@asset.file_file_name), length: Constants::FILENAME_TRUNCATION_LENGTH), @@ -78,7 +78,7 @@ class AssetsController < ApplicationController can_manage_repository_rows?(@repository.team) end - if @asset.is_image? + if @asset.image? if ['image/jpeg', 'image/pjpeg'].include? @asset.file.content_type response_json['quality'] = @asset.file_image_quality || 90 end @@ -322,7 +322,7 @@ class AssetsController < ApplicationController def asset_data_type(asset) return 'wopi' if wopi_file?(asset) - return 'image' if asset.is_image? + return 'image' if asset.image? 'file' end diff --git a/app/models/asset.rb b/app/models/asset.rb index ff78b324e..77470189a 100644 --- a/app/models/asset.rb +++ b/app/models/asset.rb @@ -203,16 +203,30 @@ class Asset < ApplicationRecord end end + def previewable? + previewable_document? || previewable_image? + end + def medium_preview - file.variant(resize: Constants::MEDIUM_PIC_FORMAT) + return file.variant(resize: Constants::MEDIUM_PIC_FORMAT) if previewable_image? + + 'medium/processing.gif' + # file.preview(resize: Constants::MEDIUM_PIC_FORMAT) end def large_preview - file.variant(resize: Constants::LARGE_PIC_FORMAT) + return file.variant(resize: Constants::LARGE_PIC_FORMAT) if previewable_image? + + 'large/processing.gif' + # file.preview(resize: Constants::LARGE_PIC_FORMAT) + end + + def file_name + file.blob&.filename&.to_s end def file_size - file.blob.byte_size + file.blob&.byte_size end def extract_image_quality @@ -227,13 +241,8 @@ class Asset < ApplicationRecord Rails.logger.info "There was an error extracting image quality - #{e}" end - def previewable? - file.previewable_image? || file.previewable_document? - end - - def is_image? - %r{^image/#{Regexp.union(Constants::WHITELISTED_IMAGE_TYPES)}} === - file.content_type + def image? + file.blob.content_type == %r{^image/#{Regexp.union(Constants::WHITELISTED_IMAGE_TYPES)}} end def text? @@ -520,6 +529,29 @@ class Asset < ApplicationRecord private + def previewable_document? + previewable = Constants::PREVIEWABLE_FILE_TYPES.include?(file.blob&.content_type) + + filename = file.blob&.filename + content_type = file.blob&.content_type + + extensions = %w(.xlsx .docx .pptx .xls .doc .ppt) + # Mimetype sometimes recognizes Office files as zip files + # In this case we also check the extension of the given file + # Otherwise the conversion should fail if the file is being something else + previewable ||= (content_type == 'application/zip' && extensions.include?(File.extname(filename))) + + # Mimetype also sometimes recognizes '.xls' and '.ppt' files as + # application/x-ole-storage (https://github.com/minad/mimemagic/issues/50) + previewable ||= (content_type == 'application/x-ole-storage' && %w(.xls .ppt).include?(File.extname(filename))) + + previewable + end + + def previewable_image? + file.blob&.content_type =~ %r{^image/#{Regexp.union(Constants::WHITELISTED_IMAGE_TYPES)}} + end + def filter_paperclip_errors if errors.size > 1 temp_errors = errors[:file] diff --git a/app/models/protocol.rb b/app/models/protocol.rb index 2e857e14a..add6f26d4 100644 --- a/app/models/protocol.rb +++ b/app/models/protocol.rb @@ -236,7 +236,7 @@ class Protocol < ApplicationRecord dest.file = src.file dest.save! - if dest.is_image? + if dest.image? dest.file.reprocess!(:large) dest.file.reprocess!(:medium) end @@ -325,7 +325,7 @@ class Protocol < ApplicationRecord asset2.created_by = current_user asset2.team = dest.team asset2.last_modified_by = current_user - asset2.file_processing = true if asset.is_image? + asset2.file_processing = true if asset.image? asset2.save! step2.assets << asset2 diff --git a/app/services/repository_actions/duplicate_cell.rb b/app/services/repository_actions/duplicate_cell.rb index c6a8478dc..554638226 100644 --- a/app/services/repository_actions/duplicate_cell.rb +++ b/app/services/repository_actions/duplicate_cell.rb @@ -77,13 +77,13 @@ module RepositoryActions new_asset.created_by = old_asset.created_by new_asset.team = @team new_asset.last_modified_by = @user - new_asset.file_processing = true if old_asset.is_image? + new_asset.file_processing = true if old_asset.image? new_asset.file = old_asset.file new_asset.save return unless new_asset.valid? - if new_asset.is_image? + if new_asset.image? new_asset.file.reprocess!(:large) new_asset.file.reprocess!(:medium) end diff --git a/app/views/my_modules/archive/_result.html.erb b/app/views/my_modules/archive/_result.html.erb index 51b70b1da..64cc1d1aa 100644 --- a/app/views/my_modules/archive/_result.html.erb +++ b/app/views/my_modules/archive/_result.html.erb @@ -36,7 +36,7 @@