Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Fix seeding custom logo from URL

Oliver Güntherd529b3f9447b
diff --git a/app/seeders/env_data/custom_design_seeder.rb b/app/seeders/env_data/custom_design_seeder.rb
index 339be90e759..82bd3fa0720 100644
--- a/app/seeders/env_data/custom_design_seeder.rb
+++ b/app/seeders/env_data/custom_design_seeder.rb
@@ -29,7 +29,7 @@
module EnvData
class CustomDesignSeeder < Seeder
def seed_data!
- custom_style = CustomStyle.current || CustomStyle.new
+ custom_style = CustomStyle.current || CustomStyle.create!
print_status " ↳ Setting custom design colors" do
seed_colors
@@ -56,7 +56,7 @@ module EnvData
if data.blank?
custom_style.public_send(:"remove_#{key}")
elsif data.match?(/^https?:\/\//)
- custom_style.public_send(:"remote_#{key}_url=", data)
+ seed_remote_url(custom_style, key, data)
else
io = Base64StringIO.new(data, key.to_s)
custom_style.public_send(:"#{key}=", io)
@@ -83,36 +83,64 @@ module EnvData
value = Setting.seed_design["export_cover_text_color"]
custom_style.export_cover_text_color = value.presence
end
- end
-
- class Base64StringIO < StringIO
- attr_reader :filename
- def initialize(data_url, base_name)
- metadata, encoded = data_url.split(",")
+ def seed_remote_url(custom_style, key, url)
+ response = HTTPX.get(url)
+ raise "Failed to set #{key} from #{url}: #{response}" unless response.status == 200
- if metadata.blank? || encoded.blank? || !metadata.start_with?("data:")
- raise ArgumentError, "Expected data URL, got #{data_url}"
+ build_attachable_file(key.to_s, response.body.to_s) do |file|
+ custom_style.public_send(:"#{key}=", file)
+ custom_style.save!
end
+ end
- @filename = "#{base_name}.#{extension(metadata)}"
- bytes = ::Base64.strict_decode64(encoded)
+ def build_attachable_file(file_name, data)
+ Tempfile.open(file_name) do |tempfile|
+ tempfile.binmode
+ tempfile.write(data)
+ tempfile.rewind
- super(bytes)
- end
+ content_type = OpenProject::ContentTypeDetector.new(tempfile.path).detect
+ mime_type = MIME::Types[content_type].last
+ raise ArgumentError, "Unknown mime type: #{content_type}" if mime_type.nil?
- def original_filename
- filename
+ file = OpenProject::Files.build_uploaded_file(tempfile,
+ content_type,
+ file_name: "#{file_name}.#{mime_type.preferred_extension}")
+
+ yield(file)
+ end
end
- def extension(metadata)
- content_type = metadata.match(%r{data:([^;]+)})&.captures&.first
- raise ArgumentError, "Failed to parse content type from metadata: #{metadata}" if content_type.nil?
+ class Base64StringIO < StringIO
+ attr_reader :filename
+
+ def initialize(data_url, base_name)
+ metadata, encoded = data_url.split(",")
+
+ if metadata.blank? || encoded.blank? || !metadata.start_with?("data:")
+ raise ArgumentError, "Expected data URL, got #{data_url}"
+ end
+
+ @filename = "#{base_name}.#{extension(metadata)}"
+ bytes = ::Base64.strict_decode64(encoded)
+
+ super(bytes)
+ end
- mime_type = MIME::Types[content_type].last
- raise ArgumentError, "Unknown mime type: #{content_type}" if mime_type.nil?
+ def original_filename
+ filename
+ end
+
+ def extension(metadata)
+ content_type = metadata.match(%r{data:([^;]+)})&.captures&.first
+ raise ArgumentError, "Failed to parse content type from metadata: #{metadata}" if content_type.nil?
- mime_type.preferred_extension
+ mime_type = MIME::Types[content_type].last
+ raise ArgumentError, "Unknown mime type: #{content_type}" if mime_type.nil?
+
+ mime_type.preferred_extension
+ end
end
end
end
diff --git a/spec/fixtures/files/icon_logo.svg b/spec/fixtures/files/icon_logo.svg
new file mode 100644
index 00000000000..8cfeda1fe4f
--- /dev/null
+++ b/spec/fixtures/files/icon_logo.svg
@@ -0,0 +1,56 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<svg
+ xmlns:dc="http://purl.org/dc/elements/1.1/"
+ xmlns:cc="http://creativecommons.org/ns#"
+ xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+ xmlns:svg="http://www.w3.org/2000/svg"
+ xmlns="http://www.w3.org/2000/svg"
+ xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
+ xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
+ viewBox="0 0 500 500"
+ version="1.1"
+ id="svg861"
+ sodipodi:docname="icon_logo.svg"
+ inkscape:version="1.0.2 (e86c8708, 2021-01-15)">
+ <metadata
+ id="metadata867">
+ <rdf:RDF>
+ <cc:Work
+ rdf:about="">
+ <dc:format>image/svg+xml</dc:format>
+ <dc:type
+ rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
+ </cc:Work>
+ </rdf:RDF>
+ </metadata>
+ <defs
+ id="defs865" />
+ <sodipodi:namedview
+ pagecolor="#ffffff"
+ bordercolor="#666666"
+ borderopacity="1"
+ objecttolerance="10"
+ gridtolerance="10"
+ guidetolerance="10"
+ inkscape:pageopacity="0"
+ inkscape:pageshadow="2"
+ inkscape:window-width="1792"
+ inkscape:window-height="1067"
+ id="namedview863"
+ showgrid="false"
+ inkscape:zoom="1.346"
+ inkscape:cx="250"
+ inkscape:cy="250"
+ inkscape:window-x="0"
+ inkscape:window-y="25"
+ inkscape:window-maximized="0"
+ inkscape:current-layer="svg861" />
+ <path
+ d="m 267.98948,387.74784 v 0 a 4.9312711,5.0968082 0 0 0 0,0.66712 28.761445,29.726934 0 0 0 57.49708,0 3.3047262,3.415662 0 0 0 0,-0.74718 v 0 -71.16852 h -57.49708 z"
+ style="fill:#0770b8;stroke-width:2.62479"
+ id="path857" />
+ <path
+ d="M 402.16653,3.4858609 H 363.82654 A 95.346514,98.547187 0 0 0 267.98948,101.95299 V 221.39442 H 99.319355 A 95.346514,98.547187 0 0 0 3.4822955,319.86155 v 79.25404 a 96.327605,99.561212 0 0 0 95.8370595,99.64126 h 38.339985 a 96.327605,99.561212 0 0 0 95.83706,-99.64126 v -79.25404 c 0,-1.30756 0,-3.36229 0,-3.36229 h -57.49707 v 82.72307 a 38.882169,40.187399 0 0 1 -38.33999,40.21408 H 99.319355 A 38.882169,40.187399 0 0 1 60.979367,399.22233 V 319.96829 A 37.901078,39.173374 0 0 1 99.319355,280.92834 H 402.16653 a 96.301787,99.534527 0 0 0 95.83706,-99.61458 V 101.95299 A 95.346514,98.547187 0 0 0 402.16653,3.4858609 Z M 440.50652,181.20702 a 38.856351,40.160714 0 0 1 -38.33999,40.1874 H 325.61565 V 101.95299 a 37.901078,39.173374 0 0 1 38.33998,-39.039947 h 38.33999 a 37.901078,39.173374 0 0 1 38.2109,39.039947 z"
+ style="fill:#0770b8;stroke-width:2.62479"
+ id="path859" />
+</svg>
diff --git a/spec/seeders/env_data/custom_design_seeder_spec.rb b/spec/seeders/env_data/custom_design_seeder_spec.rb
index b57aaed1f74..d110709e280 100644
--- a/spec/seeders/env_data/custom_design_seeder_spec.rb
+++ b/spec/seeders/env_data/custom_design_seeder_spec.rb
@@ -38,18 +38,26 @@ RSpec.describe EnvData::CustomDesignSeeder, :webmock do
let(:base64_data_url) do
"data:image/png;base64,#{base64_image}"
end
- let(:stub) do
+ let(:png_stub) do
stub_request(:get, "http://test.foobar.com/image.png")
.to_return(
status: 200,
body: Rails.root.join("spec/fixtures/files/image.png").read
)
end
+ let(:svg_stub) do
+ stub_request(:get, "http://test.foobar.com/image.svg")
+ .to_return(
+ status: 200,
+ body: Rails.root.join("spec/fixtures/files/icon_logo.svg").read
+ )
+ end
subject(:seeder) { described_class.new(seed_data) }
before do
- stub
+ png_stub
+ svg_stub
end
context "when not provided" do
@@ -87,6 +95,31 @@ RSpec.describe EnvData::CustomDesignSeeder, :webmock do
expect(DesignColor.find_by(variable: "main-menu-bg-color").hexcode).to eq("#FFFFFF")
expect(DesignColor.find_by(variable: "main-menu-bg-selected-background").hexcode).to eq("#571EFA")
expect(DesignColor.find_by(variable: "main-menu-bg-hover-background").hexcode).to eq("#E5E5E5")
+
+ RequestStore.clear!
+ custom_style = CustomStyle.current
+ expect(custom_style.logo.file).to be_present
+ expect(custom_style.logo.file.content_type).to eq "image/png"
+
+ expect(custom_style.touch_icon.file).to be_present
+ expect(custom_style.touch_icon.file.content_type).to eq "image/png"
+ end
+ end
+
+ context "when setting logo as svg",
+ :settings_reset,
+ with_env: {
+ OPENPROJECT_SEED_DESIGN_LOGO: "http://test.foobar.com/image.svg"
+ } do
+ it "sets the content type" do
+ reset(:seed_design)
+
+ seeder.seed!
+
+ custom_style = CustomStyle.current
+
+ expect(custom_style.logo.file).to be_present
+ expect(custom_style.logo.file.content_type).to eq "image/svg+xml"
end
end
@@ -108,6 +141,7 @@ RSpec.describe EnvData::CustomDesignSeeder, :webmock do
expect(custom_style.favicon.file).to be_nil
expect(custom_style.logo.file).to be_present
+ expect(custom_style.logo.file.content_type).to eq "image/png"
end
end