From 827ea641b04396e2cc3f5c8298c3ad475b1a4eff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Mon, 17 Aug 2015 18:57:28 +0200 Subject: [PATCH] FIX: Use File.size instead of IO.size --- app/controllers/uploads_controller.rb | 6 +++--- app/jobs/regular/pull_hotlinked_images.rb | 4 ++-- app/models/user_avatar.rb | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/controllers/uploads_controller.rb b/app/controllers/uploads_controller.rb index 57a1442ed66..3c3eef684dd 100644 --- a/app/controllers/uploads_controller.rb +++ b/app/controllers/uploads_controller.rb @@ -61,15 +61,15 @@ class UploadsController < ApplicationController 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) + if tempfile && File.size(tempfile.path) > 0 && SiteSetting.max_image_size_kb > 0 && FileHelper.is_image?(filename) attempt = 5 - while attempt > 0 && tempfile.size > SiteSetting.max_image_size_kb.kilobytes + while attempt > 0 && File.size(tempfile.path) > 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) + upload = Upload.create_for(current_user.id, tempfile, filename, File.size(tempfile.path), content_type: content_type, image_type: type) if upload.errors.empty? && current_user.admin? retain_hours = params[:retain_hours].to_i diff --git a/app/jobs/regular/pull_hotlinked_images.rb b/app/jobs/regular/pull_hotlinked_images.rb index 7d114653d41..87a63b98550 100644 --- a/app/jobs/regular/pull_hotlinked_images.rb +++ b/app/jobs/regular/pull_hotlinked_images.rb @@ -36,9 +36,9 @@ module Jobs rescue Discourse::InvalidParameters end if hotlinked - if hotlinked.size <= @max_size + if File.size(hotlinked.path) <= @max_size filename = File.basename(URI.parse(src).path) - upload = Upload.create_for(post.user_id, hotlinked, filename, hotlinked.size, { origin: src }) + upload = Upload.create_for(post.user_id, hotlinked, filename, File.size(hotlinked.path), { origin: src }) downloaded_urls[src] = upload.url else Rails.logger.error("Failed to pull hotlinked image: #{src} - Image is bigger than #{@max_size}") diff --git a/app/models/user_avatar.rb b/app/models/user_avatar.rb index a2e6d1e6312..f61df736bf7 100644 --- a/app/models/user_avatar.rb +++ b/app/models/user_avatar.rb @@ -20,7 +20,7 @@ class UserAvatar < ActiveRecord::Base max = Discourse.avatar_sizes.max gravatar_url = "http://www.gravatar.com/avatar/#{email_hash}.png?s=#{max}&d=404" tempfile = FileHelper.download(gravatar_url, SiteSetting.max_image_size_kb.kilobytes, "gravatar") - upload = Upload.create_for(user.id, tempfile, 'gravatar.png', tempfile.size, origin: gravatar_url, image_type: "avatar") + upload = Upload.create_for(user.id, tempfile, 'gravatar.png', File.size(tempfile.path), origin: gravatar_url, image_type: "avatar") if gravatar_upload_id != upload.id gravatar_upload.try(:destroy!) @@ -68,7 +68,7 @@ class UserAvatar < ActiveRecord::Base ext = FastImage.type(tempfile).to_s tempfile.rewind - upload = Upload.create_for(user.id, tempfile, "external-avatar." + ext, tempfile.size, origin: avatar_url, image_type: "avatar") + upload = Upload.create_for(user.id, tempfile, "external-avatar." + ext, File.size(tempfile.path), origin: avatar_url, image_type: "avatar") user.uploaded_avatar_id = upload.id unless user.user_avatar