FEATURE: automatically downsize large images#10
Conversation
Codoki PR ReviewSummary: Prevent in-place image overwrite, keep API arity, align limits Issues (Critical & High only)
Showing top 2 issues. Critical: 0, High: 2. See inline suggestions for more. Key Feedback (click to expand)
Confidence: 3/5 — Needs work before merge (2 high · status: Requires changes) Sequence DiagramsequenceDiagram
participant Client
participant UploadsController
participant FileHelper
participant OptimizedImage
Client->>UploadsController: create_upload(type, file, url)
alt API URL path
UploadsController->>FileHelper: download(url, 10MB, "discourse-upload-#{type}")
end
loop while size > max and attempts
UploadsController->>OptimizedImage: downsize(from, to, "80%", allow_animation)
alt animated GIF
OptimizedImage->>OptimizedImage: downsize_instructions_animated()
else static image
OptimizedImage->>OptimizedImage: downsize_instructions()
end
OptimizedImage->>OptimizedImage: convert_with(instructions, to)
end
UploadsController-->>Client: Upload.create_for(...) result
React with 👍 or 👎 if you found this review useful. |
|
|
||
| def self.optimize(operation, from, to, width, height, opts={}) | ||
| dim = dimensions(width, height) | ||
| def self.downsize(from, to, dimensions, opts={}) |
There was a problem hiding this comment.
| def self.downsize(from, to, dimensions, opts={}) | |
| def self.downsize(from, to, *args) | |
| opts = args.last.is_a?(Hash) ? args.pop : {} | |
| dimensions = args.length == 1 ? args.first : "#{args[0]}x#{args[1]}" | |
| optimize("downsize", from, to, dimensions, opts) | |
| end |
| 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) |
There was a problem hiding this comment.
| OptimizedImage.downsize(tempfile.path, tempfile.path, "80%", allow_animation: SiteSetting.allow_animated_thumbnails) | |
| tmp_out = "#{tempfile.path}.downsized" | |
| if OptimizedImage.downsize(tempfile.path, tmp_out, "80%", allow_animation: SiteSetting.allow_animated_thumbnails) | |
| FileUtils.mv(tmp_out, tempfile.path) | |
| else | |
| FileUtils.rm_f(tmp_out) | |
| break | |
| end |
| // check file size | ||
| var fileSizeKB = file.size / 1024; | ||
| var maxSizeKB = Discourse.SiteSettings['max_' + type + '_size_kb']; | ||
| var maxSizeKB = 10 * 1024; // 10MB |
There was a problem hiding this comment.
🔷 Medium: Hard-coding 10MB for all uploads diverges from site settings and per-type limits (images vs attachments), leading to user-visible mismatches (client allows, server rejects). Use 10MB only for images to enable server-side downsizing and keep attachments aligned with settings.
No description provided.