FEATURE: automatically correct extension for bad uploads

This fixes with post thumbnails on the fly
This commit is contained in:
Sam
2018-08-17 14:00:27 +10:00
parent eacb2593ee
commit 9628c3cf97
3 changed files with 64 additions and 40 deletions

View File

@ -184,45 +184,10 @@ class UserAvatarsController < ApplicationController
# consider removal of hacks some time in 2019
def get_optimized_image(upload, size)
if (!upload.extension || upload.extension.length == 0)
fix_extension(upload)
end
return if !upload
begin
try_optimize(upload, size, true)
rescue
if fix_extension(upload)
try_optimize(upload, size, false)
# TODO decide if we want to detach faulty avatar here?
else
nil
end
end
end
def fix_extension(upload)
# this is relatively cheap
original_path = Discourse.store.path_for(upload)
if original_path.blank?
external_copy = Discourse.store.download(upload) rescue nil
original_path = external_copy.try(:path)
end
image_info = FastImage.new(original_path) rescue nil
if image_info && image_info.type.to_s != upload.extension
upload.update_columns(extension: image_info.type.to_s)
true
end
end
def try_optimize(upload, size, raise_on_error)
OptimizedImage.create_for(
upload,
size,
size,
allow_animation: SiteSetting.allow_animated_avatars,
raise_on_error: raise_on_error
)
upload.get_optimized_image(size, size, allow_animation: SiteSetting.allow_animated_avatars)
# TODO decide if we want to detach here
end
end