Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lib/completions/upload_encoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ def self.encode(upload_ids:, max_pixels:)
next if upload.blank?
next if upload.width.to_i == 0 || upload.height.to_i == 0

desired_extension = upload.extension
desired_extension = "png" if upload.extension == "gif"
desired_extension = "png" if upload.extension == "webp"
desired_extension = "jpeg" if upload.extension == "jpg"

# this keeps it very simple format wise given everyone supports png and jpg
next if !%w[jpeg png].include?(desired_extension)
Copy link
Contributor

Choose a reason for hiding this comment

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

should we log domething here?

Copy link
Member Author

Choose a reason for hiding this comment

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

long term maybe, for now I think skipping obscure image formats is ok


original_pixels = upload.width * upload.height

image = upload
Expand All @@ -20,12 +28,15 @@ def self.encode(upload_ids:, max_pixels:)
new_width = (ratio * upload.width).to_i
new_height = (ratio * upload.height).to_i

image = upload.get_optimized_image(new_width, new_height)
image = upload.get_optimized_image(new_width, new_height, format: desired_extension)
elsif upload.extension != desired_extension
image =
upload.get_optimized_image(upload.width, upload.height, format: desired_extension)
end

next if !image

mime_type = MiniMime.lookup_by_filename(upload.original_filename).content_type
mime_type = MiniMime.lookup_by_filename("test.#{desired_extension}").content_type

path = Discourse.store.path_for(image)
if path.blank?
Expand Down
Binary file added spec/fixtures/images/1x1.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions spec/lib/completions/upload_encoder_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true
Copy link
Contributor

Choose a reason for hiding this comment

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

we should also have webp and jpeg

Copy link
Member Author

Choose a reason for hiding this comment

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

no worries, added


RSpec.describe DiscourseAi::Completions::UploadEncoder do
let(:file) { plugin_file_from_fixtures("1x1.gif") }

it "automatically converts gifs to pngs" do
upload = UploadCreator.new(file, "1x1.gif").create_for(Discourse.system_user.id)
encoded = described_class.encode(upload_ids: [upload.id], max_pixels: 1_048_576)
expect(encoded.length).to eq(1)
expect(encoded[0][:base64]).to be_present
expect(encoded[0][:mime_type]).to eq("image/png")
end
end
Loading