-
Notifications
You must be signed in to change notification settings - Fork 0
FEATURE: automatically downsize large images #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: image-processing-optimization
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -52,14 +52,23 @@ def create_upload(type, file, url) | |||||
| 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 | ||||||
| filename = file.original_filename | ||||||
| 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) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. File size passed to After the downsizing loop modifies the file, 🐛 Proposed fix- 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)📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
|
|
||||||
| if upload.errors.empty? && current_user.admin? | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🌐 Web query:
Does ImageMagick convert -resize accept percentage values like "80%"?💡 Result:
Yes. -resize accepts percentage values (e.g. magick input.png -resize 80% output.png). [1][2]
Note: on Windows batch files you must escape % by doubling it (use %%). [1]
🏁 Script executed:
Repository: AI-Code-Review-Evals/coderabbit-discourse
Length of output: 226
🏁 Script executed:
Repository: AI-Code-Review-Evals/coderabbit-discourse
Length of output: 67
Improve error handling and file size tracking in the downsize loop.
The downsizing logic has several issues:
No error handling: If
OptimizedImage.downsizefails, the loop continues without logging or indication of failure.File size not refreshed after modification:
tempfile.sizemay not reflect the updated file size after in-place modification. UseFile.size(tempfile.path)to get the current file size on disk.In-place file modification: Using the same path for source and destination carries inherent risk if the operation fails mid-write, potentially corrupting the file.
The "80%" dimension string is a valid ImageMagick resize format and will correctly reduce dimensions by 20%.
🐛 Proposed fix
# 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) + while attempt > 0 && File.size(tempfile.path) > SiteSetting.max_image_size_kb.kilobytes + unless OptimizedImage.downsize(tempfile.path, tempfile.path, "80%", allow_animation: SiteSetting.allow_animated_thumbnails) + Rails.logger.warn("Failed to downsize image #{filename}, attempt #{6 - attempt}") + break + end attempt -= 1 end end