docbrown/lib/data_update_scripts/20220105112823_migrate_logo_svg_data.rb
Ridhwana 81571bb0b5
Data Update Script to migrate logo_svg contents to png file (Will be merged and deployed on 10/01) (#15710)
* feat/WIP: first version of the svg logo to png logo DUS

* feat: logoSVG Uploader

* refactor: remove the if original_filename

* feat: add a test for the logo_svg_uploader

* feat: update the test and dus

* add soem error handling

* updae the rails spec helper

* feat: provide a content type

* feat: add content type

* Try https://travis-ci.community/t/build-times-out-with-no-apparent-reason/5083/4

* chore: try this suggestion https://stackoverflow.com/questions/41138404/how-to-install-newer-imagemagick-with-webp-support-in-travis-ci-container

* chore: remove libweb adn fix = in the travis.yml

* chore: undo the changes to attempt to update the version for image magick

* feat: install gsfonts for convert

* chore: add a comment for svg

* feat: use the good practices for tempfile, update error handling and update the tests

* chore: update the timestamp on the DUS

* feat: convert to png with a transparent background using Image Magick

* feat: substitute some css variables with real css colors

* chore: revert the path

* fix: set content type

* Update lib/data_update_scripts/20220105112823_migrate_logo_svg_data.rb

Co-authored-by: Jamie Gaskins <jamie@forem.com>

Co-authored-by: Jamie Gaskins <jamie@forem.com>
2022-01-10 16:09:34 +02:00

38 lines
1.5 KiB
Ruby

module DataUpdateScripts
class MigrateLogoSvgData
def run
return if ::Settings::General.try(:logo_svg).blank? ||
(::Settings::General.try(:original_logo).present? && ::Settings::General.try(:resized_logo).present?)
logo_svg = Settings::General.logo_svg
# We do our best effort here: These are some (possibly not all) of the css variables that we may see in Forem
# logos. Hence, we try and replace them before converting.
logo_svg.gsub! "currentColor", "#090909"
logo_svg.gsub! "var(--base-inverted)", "#fff"
logo_svg.gsub! "var(--link-color)", "#3d3d3d"
Tempfile.create(["logo", ".svg"]) do |file|
file.write(logo_svg)
logo_svg_uploader = LogoSvgUploader.new.tap do |uploader|
uploader.store!(file)
end
::Settings::General.original_logo = logo_svg_uploader.url
::Settings::General.resized_logo = logo_svg_uploader.resized_logo.url
end
rescue StandardError => e
# If we can't convert the logo, then alert ourselves so that we can track the Forems
# whose logos could not be converted.
Rails.logger.error("Could not convert logo_svg for #{Settings::Community.community_name} Forem to a PNG.")
Honeybadger.notify(e, context: {
community_name: Settings::Community.community_name,
app_domain: Settings::General.app_domain,
},
tags: "failed_svg_conversion")
end
end
end