Skip to content

Commit

Permalink
Better handling of custom SVG sprites in themes when using S3
Browse files Browse the repository at this point in the history
  • Loading branch information
pmusaraj committed May 28, 2019
1 parent 9bdfaa1 commit 42818b8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/svg_sprite/svg_sprite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,13 @@ def self.custom_svg_sprites(theme_ids = [])
ThemeField.where(type_id: ThemeField.types[:theme_upload_var], name: THEME_SPRITE_VAR_NAME, theme_id: Theme.transform_ids(theme_ids))
.pluck(:upload_id).each do |upload_id|

upload = Upload.find(upload_id)
original_path = Discourse.store.path_for(upload)
if original_path.blank?
upload = Upload.find(upload_id) rescue nil

if Discourse.store.external?
external_copy = Discourse.store.download(upload) rescue nil
original_path = external_copy.try(:path)
else
original_path = Discourse.store.path_for(upload)
end

custom_sprite_paths << original_path if original_path.present?
Expand Down
24 changes: 24 additions & 0 deletions spec/components/svg_sprite/svg_sprite_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,30 @@
expect(SvgSprite.bundle([theme.id])).to match(/my-custom-theme-icon/)
end

context "s3" do
let(:upload_s3) { Fabricate(:upload_s3) }

before do
SiteSetting.enable_s3_uploads = true
SiteSetting.s3_upload_bucket = "s3bucket"
SiteSetting.s3_access_key_id = "s3_access_key_id"
SiteSetting.s3_secret_access_key = "s3_secret_access_key"

stub_request(:get, upload_s3.url).to_return(status: 200, body: "Hello world")
end

it 'includes svg sprites in themes stored in s3' do
theme = Fabricate(:theme)
theme.set_field(target: :common, name: SvgSprite.theme_sprite_variable_name, upload_id: upload_s3.id, type: :theme_upload_var)
theme.save!

sprite_files = SvgSprite.custom_svg_sprites([theme.id]).join("|")

expect(sprite_files).to match(/#{upload_s3.sha1}/)
expect(sprite_files).not_to match(/amazonaws/)
end
end

it 'includes icons from SiteSettings' do
SiteSetting.svg_icon_subset = "blender|drafting-compass|fab-bandcamp"

Expand Down

0 comments on commit 42818b8

Please sign in to comment.