From 1b7140bb1dec398fa820057b3f993ce5c832a814 Mon Sep 17 00:00:00 2001 From: Ryan Lue Date: Wed, 17 Jul 2019 19:02:54 +0800 Subject: [PATCH] Prevent extreme import delays by skipping image preview generation --- app/models/store.rb | 63 ++++++++++++++++++++--------------------- test/unit/store_test.rb | 17 +++++++++++ 2 files changed, 47 insertions(+), 33 deletions(-) diff --git a/app/models/store.rb b/app/models/store.rb index 9be84a3e2..56b966f96 100644 --- a/app/models/store.rb +++ b/app/models/store.rb @@ -12,6 +12,8 @@ class Store < ApplicationModel store :preferences + after_create :generate_previews + =begin add an attachment to storage @@ -50,40 +52,8 @@ returns data.delete('data') data.delete('object') - data['preferences'] ||= {} - resizable = false - ['Mime-Type', 'Content-Type', 'mime_type', 'content_type'].each do |key| - next if data['preferences'][key].blank? - next if !data['preferences'][key].match(%r{image/(jpeg|jpg|png)}i) - - resizable = true - break - end - # store meta data - store = Store.create!(data) - - begin - if resizable - if store.content_preview(silence: true) - store.preferences[:resizable] = true - store.preferences[:content_preview] = true - end - if store.content_inline(silence: true) - store.preferences[:resizable] = true - store.preferences[:content_inline] = true - end - if store.preferences[:resizable] - store.save! - end - end - rescue => e - logger.error e - store.preferences[:resizable] = false - store.save! - end - - store + Store.create!(data) end =begin @@ -283,6 +253,33 @@ returns private + def generate_previews + return true if Setting.get('import_mode') + + resizable = preferences.slice('Mime-Type', 'Content-Type', 'mime_type', 'content_type') + .values.grep(%r{image/(jpeg|jpg|png)}i).any? + + begin + if resizable + if content_preview(silence: true) + preferences[:resizable] = true + preferences[:content_preview] = true + end + if content_inline(silence: true) + preferences[:resizable] = true + preferences[:content_inline] = true + end + if preferences[:resizable] + save! + end + end + rescue => e + logger.error e + preferences[:resizable] = false + save! + end + end + def image_resize(content, width) local_sha = Digest::SHA256.hexdigest(content) diff --git a/test/unit/store_test.rb b/test/unit/store_test.rb index 53049d946..4edb06c96 100644 --- a/test/unit/store_test.rb +++ b/test/unit/store_test.rb @@ -318,5 +318,22 @@ class StoreTest < ActiveSupport::TestCase assert_equal(store.preferences[:content_inline], true) assert_equal(store.preferences[:content_preview], true) + # possible, but skipped (preview and inline) + Setting.set('import_mode', true) + store = Store.add( + object: 'SomeObject3', + o_id: rand(1_234_567_890), + data: File.binread(Rails.root.join('test', 'data', 'upload', 'upload2.jpg')), + filename: 'test1.pdf', + preferences: { + content_type: 'image/jpg', + content_id: 234, + }, + created_by_id: 1, + ) + assert_nil(store.preferences[:resizable]) + assert_nil(store.preferences[:content_inline]) + assert_nil(store.preferences[:content_preview]) + end end