docbrown/app/uploaders/logo_svg_uploader.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

65 lines
1.5 KiB
Ruby

class LogoSvgUploader < BaseUploader
# This Uploader is being used by a Data Update Script
# lib/data_update_scripts/20211208074428_migrate_logo_svg_data.rb
# to convert logo_svg contents to a png file.
STORE_DIRECTORY = "uploads/logos/".freeze
EXTENSION_ALLOWLIST = %w[svg png].freeze
IMAGE_TYPE_ALLOWLIST = %i[svg png].freeze
CONTENT_TYPE_ALLOWLIST = %w[image/svg+xml image/png].freeze
process :convert_to_png
def store_dir
STORE_DIRECTORY
end
def extension_allowlist
EXTENSION_ALLOWLIST
end
def image_type_whitelist
# this is needed by CarrierWave::BombShelter
IMAGE_TYPE_ALLOWLIST
end
def content_type_allowlist
CONTENT_TYPE_ALLOWLIST
end
def filename
# random_string in the filename to avoid caching issues
"original_logo_#{random_string}.png"
end
def convert_to_png
temp_png_file = Tempfile.new ["output", ".png"]
begin
MiniMagick::Tool::Convert.new do |convert|
convert.background("none")
convert << file.path
convert << temp_png_file.path
end
# https://github.com/minimagick/minimagick/issues/332
file.instance_variable_set(:@file, temp_png_file)
file.instance_variable_set(:@content_type, "image/png")
ensure
temp_png_file.close
end
end
version :resized_logo do
process resize_to_limit: [nil, 80]
def full_filename(_for_file = file)
"resized_logo_#{random_string}.png"
end
end
private
def random_string
SecureRandom.alphanumeric(20)
end
end