Skip to content

Conversation

@everettbu
Copy link
Contributor

@everettbu everettbu commented Jul 29, 2025

Test 1

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Greptile Summary

This PR implements automatic image downsizing functionality that allows users to upload large images which are then automatically reduced to fit within configured size limits. The implementation spans three key areas:

Backend Processing (uploads_controller.rb): The core feature adds automatic downsizing logic that iteratively reduces large images by 80% using OptimizedImage.downsize() until they meet size requirements. The API download limit is increased from the site setting to a fixed 10MB to accommodate larger initial uploads.

Image Processing (optimized_image.rb): The model is refactored to simplify parameter passing by formatting dimension strings at the method entry points rather than using a separate dimensions helper. The resize and downsize methods now format dimensions as strings (e.g., "800x600") and pass them directly to the optimize method.

Frontend Validation (utilities.js): File size validation is updated to use hardcoded 10MB limits instead of dynamic site settings, both in the file validation function and HTTP 413 error handling.

This change integrates with Discourse's existing image processing pipeline that handles thumbnail generation and optimization, leveraging ImageMagick's convert command through the OptimizedImage class. The feature aims to improve user experience by preventing upload failures and eliminating the need for manual image resizing.

Confidence score: 1/5

• This PR contains critical bugs that will break existing functionality and should not be merged
• The OptimizedImage model has duplicate method definitions that will cause runtime failures
• Multiple files contain potential infinite loops, missing error handling, and security vulnerabilities that need immediate attention

3 files reviewed, 4 comments

Edit Code Review Bot Settings | Greptile

Comment on lines +149 to +151
def self.downsize(from, to, dimensions, opts={})
optimize("downsize", from, to, dimensions, opts)
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Duplicate method definition - this downsize method will override the previous one on lines 145-147, breaking the API that expects separate max_width and max_height parameters

Suggested change
def self.downsize(from, to, dimensions, opts={})
optimize("downsize", from, to, dimensions, opts)
end
def self.downsize_with_dimensions(from, to, dimensions, opts={})
optimize("downsize", from, to, dimensions, opts)
end

// check file size
var fileSizeKB = file.size / 1024;
var maxSizeKB = Discourse.SiteSettings['max_' + type + '_size_kb'];
var maxSizeKB = 10 * 1024; // 10MB
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Hardcoding file size limits bypasses site settings and breaks configurability. Consider using a larger site setting or removing frontend validation for images since backend now handles downsizing.

Comment on lines +66 to +69
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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Potential infinite loop if OptimizedImage.downsize fails to reduce file size. The loop only decrements attempt but doesn't verify size reduction occurred.

end
end

upload = Upload.create_for(current_user.id, tempfile, filename, tempfile.size, content_type: content_type, image_type: type)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Using potentially stale tempfile.size after downsizing operations. The size should be refreshed to reflect actual file size changes.

Suggested change
upload = Upload.create_for(current_user.id, tempfile, filename, tempfile.size, content_type: content_type, image_type: type)
# Refresh tempfile size after potential downsizing
tempfile.rewind if tempfile.respond_to?(:rewind)
actual_size = File.size(tempfile.path)
upload = Upload.create_for(current_user.id, tempfile, filename, actual_size, content_type: content_type, image_type: type)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants