FEATURE: automatically downsize large images#1
Conversation
|
Claude finished @ShashankFC's task in 1m 26s —— View job Code Review CompleteTasks
SummaryThis PR implements automatic downsizing of large images before upload processing. The feature allows users to upload images larger than the configured Issues Found🔴 Critical: Hardcoded File Size LimitsLocation: The PR hardcodes the file size limit to // Before:
var maxSizeKB = Discourse.SiteSettings['max_' + type + '_size_kb'];
// After:
var maxSizeKB = 10 * 1024; // 10MBProblem: This breaks the dynamic site setting behavior. The JavaScript now enforces a fixed 10MB limit regardless of the server-side Impact:
Recommendation: Revert these JavaScript changes and keep the dynamic site settings. The automatic downsizing feature on the server side (added in 🟡 Moderate: Duplicate Method DefinitionLocation: The PR introduces two def self.downsize(from, to, max_width, max_height, opts={})
optimize("downsize", from, to, "#{max_width}x#{max_height}", opts)
end
def self.downsize(from, to, dimensions, opts={})
optimize("downsize", from, to, dimensions, opts)
endProblem: In Ruby, the second method definition overwrites the first. The 4-parameter version ( Impact:
Recommendation: Remove the first 🟢 Minor: Missing Error HandlingLocation: The automatic downsizing loop lacks error handling: 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
endConcerns:
Recommendation: Add error handling and logging: if tempfile && tempfile.size > 0 && SiteSetting.max_image_size_kb > 0 && FileHelper.is_image?(filename)
original_size = tempfile.size
attempt = 5
while attempt > 0 && tempfile.size > SiteSetting.max_image_size_kb.kilobytes
previous_size = tempfile.size
if OptimizedImage.downsize(tempfile.path, tempfile.path, "80%", allow_animation: SiteSetting.allow_animated_thumbnails)
tempfile.rewind # Ensure file pointer is reset
# Check if size actually decreased
if tempfile.size >= previous_size
Rails.logger.warn("Image downsizing did not reduce file size for #{filename}")
break
end
else
Rails.logger.error("Failed to downsize image #{filename}")
break
end
attempt -= 1
end
Rails.logger.info("Downsized image #{filename} from #{original_size} to #{tempfile.size} bytes") if tempfile.size < original_size
end🟢 Minor: Code QualityLocation: The refactoring improves code quality by:
This is good, but the duplicate method issue noted above needs resolution. Positive Aspects
Recommendations
|
Test 1nn---nReplicated from ai-code-review-evaluation/discourse-greptile#1