Prevent extreme import delays by skipping image preview generation

This commit is contained in:
Ryan Lue 2019-07-17 19:02:54 +08:00 committed by Thorsten Eckel
parent a9144638a9
commit 1b7140bb1d
2 changed files with 47 additions and 33 deletions

View file

@ -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)

View file

@ -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