Skip to content

Commit 86a9ab0

Browse files
committed
widgets: add PNG output format support
Widget image generation produces PNG internally — the pipeline opens OH_Partner_frame.png, composites text and kudos onto it, and returns the result via MiniMagick's to_blob, which emits PNG bytes. Previously those bytes were served as image/gif regardless of what was requested, which is a lie about the Content-Type. Add proper ?format=png support by renaming render_image_for_gif_format to render_image_for_image_format (and the "not supported" equivalent) across all five widget controllers. PNG requests now receive Content-Type: image/png with the same underlying bytes — no conversion needed since the data is already PNG. GIF requests preserve existing behavior for backward compatibility.
1 parent cdbe3b3 commit 86a9ab0

5 files changed

Lines changed: 17 additions & 11 deletions

File tree

app/controllers/account_widgets_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
class AccountWidgetsController < WidgetsController
44
before_action :set_account
5-
before_action :render_image_for_gif_format
5+
before_action :render_image_for_image_format
66
before_action :account_context, only: :index
77

88
def index

app/controllers/organization_widgets_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
class OrganizationWidgetsController < WidgetsController
44
before_action :set_organization
5-
before_action :render_not_supported_for_gif_format
5+
before_action :render_not_supported_for_image_format
66
before_action :render_iframe_for_js_format
77
before_action :organization_context, only: :index
88

app/controllers/project_widgets_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
class ProjectWidgetsController < WidgetsController
44
helper :Projects, :Analyses
55
before_action :set_project
6-
before_action :render_image_for_gif_format, only: %i[partner_badge thin_badge]
7-
before_action :render_not_supported_for_gif_format, except: %i[partner_badge thin_badge index]
6+
before_action :render_image_for_image_format, only: %i[partner_badge thin_badge]
7+
before_action :render_not_supported_for_image_format, except: %i[partner_badge thin_badge index]
88
before_action :render_iframe_for_js_format
99
before_action :project_context, only: :index
1010

app/controllers/stack_widgets_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
class StackWidgetsController < WidgetsController
44
before_action :set_widget
55
before_action :set_stack_and_account
6-
before_action :render_not_supported_for_gif_format
6+
before_action :render_not_supported_for_image_format
77
before_action :render_iframe_for_js_format
88
before_action :account_context, only: :index
99

app/controllers/widgets_controller.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,25 @@ def set_widget
2222
.new(permitted_params)
2323
end
2424

25-
def render_image_for_gif_format
26-
return unless request.format.gif?
25+
def render_image_for_image_format
26+
return unless request.format.gif? || request.format.png?
2727

2828
cache_key = "#{@widget.parent.class.name}/#{@widget.parent.id}/#{@widget.name}"
2929
image = Rails.cache.fetch(cache_key, expires_in: 4.hours) { @widget.image }
30-
send_data(image, disposition: 'inline', type: 'image/gif', filename: 'widget.gif', status: 200)
30+
if request.format.png?
31+
send_data(image, disposition: 'inline', type: 'image/png', filename: 'widget.png', status: 200)
32+
else
33+
send_data(image, disposition: 'inline', type: 'image/gif', filename: 'widget.gif', status: 200)
34+
end
3135
end
3236

33-
def render_not_supported_for_gif_format
34-
return unless request.format.gif?
37+
def render_not_supported_for_image_format
38+
return unless request.format.gif? || request.format.png?
3539

3640
image = Rails.cache.fetch('not_supported_widget') { WidgetBadge::Thin.create([text: 'Not supported']) }
37-
send_data(image, disposition: 'inline', type: 'image/gif', filename: 'widget.gif', status: 406)
41+
mime_type = request.format.png? ? 'image/png' : 'image/gif'
42+
filename = request.format.png? ? 'widget.png' : 'widget.gif'
43+
send_data(image, disposition: 'inline', type: mime_type, filename: filename, status: 406)
3844
end
3945

4046
def render_iframe_for_js_format

0 commit comments

Comments
 (0)