mirror of
https://github.com/discourse/discourse.git
synced 2025-05-21 18:12:32 +08:00
FEATURE: automatically downsize large images
This commit is contained in:
@ -52,7 +52,7 @@ class UploadsController < ApplicationController
|
||||
begin
|
||||
# API can provide a URL
|
||||
if file.nil? && url.present? && is_api?
|
||||
tempfile = FileHelper.download(url, SiteSetting.max_image_size_kb.kilobytes, "discourse-upload-#{type}") rescue nil
|
||||
tempfile = FileHelper.download(url, 10.megabytes, "discourse-upload-#{type}") rescue nil
|
||||
filename = File.basename(URI.parse(url).path)
|
||||
else
|
||||
tempfile = file.tempfile
|
||||
@ -60,6 +60,15 @@ class UploadsController < ApplicationController
|
||||
content_type = file.content_type
|
||||
end
|
||||
|
||||
# allow users to upload large images that will be automatically reduced to allowed size
|
||||
if tempfile && tempfile.size > 0 && SiteSetting.max_image_size_kb > 0 && FileHelper.is_image?(filename)
|
||||
attempt = 5
|
||||
while attempt > 0 && tempfile.size > SiteSetting.max_image_size_kb.kilobytes
|
||||
OptimizedImage.downsize(tempfile.path, tempfile.path, "80%", allow_animation: SiteSetting.allow_animated_thumbnails)
|
||||
attempt -= 1
|
||||
end
|
||||
end
|
||||
|
||||
upload = Upload.create_for(current_user.id, tempfile, filename, tempfile.size, content_type: content_type, image_type: type)
|
||||
|
||||
if upload.errors.empty? && current_user.admin?
|
||||
|
Reference in New Issue
Block a user