From debc0595abe3e27ea7e6177088a45133911928f0 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Wed, 31 May 2023 14:45:46 +0200
Subject: [PATCH] Fix #1017

---
 Gemfile.lock                                  | 183 +++++++++---------
 .../websites/preview_controller.rb            |  14 ++
 .../communication/websites_controller.rb      |   4 -
 .../communication/website/with_style.rb       |  20 +-
 app/views/admin/layouts/preview.html.erb      |   8 +-
 config/routes/admin/communication.rb          |   3 +-
 6 files changed, 128 insertions(+), 104 deletions(-)
 create mode 100644 app/controllers/admin/communication/websites/preview_controller.rb

diff --git a/Gemfile.lock b/Gemfile.lock
index a93bf9338..fa04f864d 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -20,47 +20,47 @@ GIT
 GEM
   remote: https://rubygems.org/
   specs:
-    actioncable (7.0.4.3)
-      actionpack (= 7.0.4.3)
-      activesupport (= 7.0.4.3)
+    actioncable (7.0.5)
+      actionpack (= 7.0.5)
+      activesupport (= 7.0.5)
       nio4r (~> 2.0)
       websocket-driver (>= 0.6.1)
-    actionmailbox (7.0.4.3)
-      actionpack (= 7.0.4.3)
-      activejob (= 7.0.4.3)
-      activerecord (= 7.0.4.3)
-      activestorage (= 7.0.4.3)
-      activesupport (= 7.0.4.3)
+    actionmailbox (7.0.5)
+      actionpack (= 7.0.5)
+      activejob (= 7.0.5)
+      activerecord (= 7.0.5)
+      activestorage (= 7.0.5)
+      activesupport (= 7.0.5)
       mail (>= 2.7.1)
       net-imap
       net-pop
       net-smtp
-    actionmailer (7.0.4.3)
-      actionpack (= 7.0.4.3)
-      actionview (= 7.0.4.3)
-      activejob (= 7.0.4.3)
-      activesupport (= 7.0.4.3)
+    actionmailer (7.0.5)
+      actionpack (= 7.0.5)
+      actionview (= 7.0.5)
+      activejob (= 7.0.5)
+      activesupport (= 7.0.5)
       mail (~> 2.5, >= 2.5.4)
       net-imap
       net-pop
       net-smtp
       rails-dom-testing (~> 2.0)
-    actionpack (7.0.4.3)
-      actionview (= 7.0.4.3)
-      activesupport (= 7.0.4.3)
-      rack (~> 2.0, >= 2.2.0)
+    actionpack (7.0.5)
+      actionview (= 7.0.5)
+      activesupport (= 7.0.5)
+      rack (~> 2.0, >= 2.2.4)
       rack-test (>= 0.6.3)
       rails-dom-testing (~> 2.0)
       rails-html-sanitizer (~> 1.0, >= 1.2.0)
-    actiontext (7.0.4.3)
-      actionpack (= 7.0.4.3)
-      activerecord (= 7.0.4.3)
-      activestorage (= 7.0.4.3)
-      activesupport (= 7.0.4.3)
+    actiontext (7.0.5)
+      actionpack (= 7.0.5)
+      activerecord (= 7.0.5)
+      activestorage (= 7.0.5)
+      activesupport (= 7.0.5)
       globalid (>= 0.6.0)
       nokogiri (>= 1.8.5)
-    actionview (7.0.4.3)
-      activesupport (= 7.0.4.3)
+    actionview (7.0.5)
+      activesupport (= 7.0.5)
       builder (~> 3.1)
       erubi (~> 1.4)
       rails-dom-testing (~> 2.0)
@@ -70,30 +70,30 @@ GEM
       activemodel (>= 5.2.0)
       activestorage (>= 5.2.0)
       activesupport (>= 5.2.0)
-    activejob (7.0.4.3)
-      activesupport (= 7.0.4.3)
+    activejob (7.0.5)
+      activesupport (= 7.0.5)
       globalid (>= 0.3.6)
-    activemodel (7.0.4.3)
-      activesupport (= 7.0.4.3)
-    activerecord (7.0.4.3)
-      activemodel (= 7.0.4.3)
-      activesupport (= 7.0.4.3)
-    activestorage (7.0.4.3)
-      actionpack (= 7.0.4.3)
-      activejob (= 7.0.4.3)
-      activerecord (= 7.0.4.3)
-      activesupport (= 7.0.4.3)
+    activemodel (7.0.5)
+      activesupport (= 7.0.5)
+    activerecord (7.0.5)
+      activemodel (= 7.0.5)
+      activesupport (= 7.0.5)
+    activestorage (7.0.5)
+      actionpack (= 7.0.5)
+      activejob (= 7.0.5)
+      activerecord (= 7.0.5)
+      activesupport (= 7.0.5)
       marcel (~> 1.0)
       mini_mime (>= 1.1.0)
     activestorage-scaleway-service (1.0.1)
       activestorage
       aws-sdk-s3
-    activesupport (7.0.4.3)
+    activesupport (7.0.5)
       concurrent-ruby (~> 1.0, >= 1.0.2)
       i18n (>= 1.6, < 2)
       minitest (>= 5.1)
       tzinfo (~> 2.0)
-    addressable (2.8.3)
+    addressable (2.8.4)
       public_suffix (>= 2.0.2, < 6.0)
     angularjs-rails (1.8.0)
     annotate (3.2.0)
@@ -102,16 +102,16 @@ GEM
     autoprefixer-rails (10.4.13.0)
       execjs (~> 2)
     aws-eventstream (1.2.0)
-    aws-partitions (1.742.0)
-    aws-sdk-core (3.171.0)
+    aws-partitions (1.772.0)
+    aws-sdk-core (3.173.1)
       aws-eventstream (~> 1, >= 1.0.2)
       aws-partitions (~> 1, >= 1.651.0)
       aws-sigv4 (~> 1.5)
       jmespath (~> 1, >= 1.6.1)
-    aws-sdk-kms (1.63.0)
+    aws-sdk-kms (1.64.0)
       aws-sdk-core (~> 3, >= 3.165.0)
       aws-sigv4 (~> 1.1)
-    aws-sdk-s3 (1.120.1)
+    aws-sdk-s3 (1.122.0)
       aws-sdk-core (~> 3, >= 3.165.0)
       aws-sdk-kms (~> 1)
       aws-sigv4 (~> 1.4)
@@ -135,7 +135,7 @@ GEM
     builder (3.2.4)
     byebug (11.1.3)
     cancancan (3.3.0)
-    capybara (3.39.0)
+    capybara (3.39.1)
       addressable
       matrix
       mini_mime (>= 0.1.3)
@@ -144,7 +144,7 @@ GEM
       rack-test (>= 0.6.3)
       regexp_parser (>= 1.5, < 3.0)
       xpath (~> 3.2)
-    caxlsx (3.3.0)
+    caxlsx (3.4.1)
       htmlentities (~> 4.3, >= 4.3.4)
       marcel (~> 1.0)
       nokogiri (~> 1.10, >= 1.10.4)
@@ -154,7 +154,7 @@ GEM
       caxlsx (>= 3.0)
     cocoon (1.2.15)
     concurrent-ruby (1.2.2)
-    countries (5.3.2)
+    countries (5.4.0)
       unaccent (~> 0.3)
     country_select (8.0.1)
       countries (~> 5.0)
@@ -197,7 +197,7 @@ GEM
     faceted_search (3.6.2)
       font-awesome-sass
       rails (>= 5.2.0)
-    faraday (2.7.4)
+    faraday (2.7.5)
       faraday-net_http (>= 2.0, < 3.1)
       ruby2_keywords (>= 0.0.4)
     faraday-cookie_jar (0.0.7)
@@ -210,14 +210,14 @@ GEM
     faraday-gzip (1.0.0)
       faraday (>= 1.0)
       zlib (~> 2.1)
-    faraday-http-cache (2.4.1)
+    faraday-http-cache (2.5.0)
       faraday (>= 0.8)
     faraday-multipart (1.0.4)
       multipart-post (~> 2)
     faraday-net_http (3.0.2)
     faraday-retry (2.1.0)
       faraday (~> 2.0)
-    fastimage (2.2.6)
+    fastimage (2.2.7)
     ffi (1.15.5)
     figaro (1.2.0)
       thor (>= 0.14.0, < 2)
@@ -260,7 +260,7 @@ GEM
     httparty (0.21.0)
       mini_mime (>= 1.0.0)
       multi_xml (>= 0.5.2)
-    i18n (1.12.0)
+    i18n (1.13.0)
       concurrent-ruby (~> 1.0)
     image_processing (1.12.2)
       mini_magick (>= 4.9.5, < 5)
@@ -297,9 +297,9 @@ GEM
     listen (3.8.0)
       rb-fsevent (~> 0.10, >= 0.10.3)
       rb-inotify (~> 0.9, >= 0.9.10)
-    loofah (2.20.0)
+    loofah (2.21.3)
       crass (~> 1.0.2)
-      nokogiri (>= 1.5.9)
+      nokogiri (>= 1.12.0)
     mail (2.8.1)
       mini_mime (>= 0.1.1)
       net-imap
@@ -307,14 +307,14 @@ GEM
       net-smtp
     marcel (1.0.2)
     matrix (0.4.2)
-    metainspector (5.14.0)
-      addressable (~> 2.8)
+    metainspector (5.15.0)
+      addressable (~> 2.8.4)
       faraday (~> 2.5)
       faraday-cookie_jar (~> 0.0)
       faraday-encoding (~> 0.0)
       faraday-follow_redirects (~> 0.3)
       faraday-gzip (>= 0.1, < 2.0)
-      faraday-http-cache (~> 2.4)
+      faraday-http-cache (~> 2.5)
       faraday-retry (~> 2.0)
       fastimage (~> 2.2)
       nesty (~> 1.0)
@@ -323,7 +323,7 @@ GEM
     mini_magick (4.12.0)
     mini_mime (1.1.2)
     minitest (5.18.0)
-    msgpack (1.7.0)
+    msgpack (1.7.1)
     multi_xml (0.6.0)
     multipart-post (2.3.0)
     mustermann (3.0.0)
@@ -341,11 +341,11 @@ GEM
     net-smtp (0.3.3)
       net-protocol
     nio4r (2.5.9)
-    nokogiri (1.14.2-arm64-darwin)
+    nokogiri (1.15.2-arm64-darwin)
       racc (~> 1.4)
-    nokogiri (1.14.2-x86_64-darwin)
+    nokogiri (1.15.2-x86_64-darwin)
       racc (~> 1.4)
-    nokogiri (1.14.2-x86_64-linux)
+    nokogiri (1.15.2-x86_64-linux)
       racc (~> 1.4)
     oauth2 (2.0.9)
       faraday (>= 0.17.3, < 3.0)
@@ -354,7 +354,7 @@ GEM
       rack (>= 1.2, < 4)
       snaky_hash (~> 2.0)
       version_gem (~> 1.1)
-    octokit (6.1.0)
+    octokit (6.1.1)
       faraday (>= 1, < 3)
       sawyer (~> 0.9)
     omniauth (2.1.1)
@@ -374,46 +374,47 @@ GEM
     orm_adapter (0.5.0)
     pexels (0.5.0)
       requests (~> 1.0.2)
-    pg (1.4.6)
-    popper_js (2.11.6)
+    pg (1.5.3)
+    popper_js (2.11.7)
     public_suffix (5.0.1)
-    puma (6.2.1)
+    puma (6.3.0)
       nio4r (~> 2.0)
     racc (1.6.2)
-    rack (2.2.6.4)
+    rack (2.2.7)
     rack-mini-profiler (2.3.4)
       rack (>= 1.2.0)
-    rack-protection (3.0.5)
+    rack-protection (3.0.6)
       rack
     rack-test (2.1.0)
       rack (>= 1.3)
-    rails (7.0.4.3)
-      actioncable (= 7.0.4.3)
-      actionmailbox (= 7.0.4.3)
-      actionmailer (= 7.0.4.3)
-      actionpack (= 7.0.4.3)
-      actiontext (= 7.0.4.3)
-      actionview (= 7.0.4.3)
-      activejob (= 7.0.4.3)
-      activemodel (= 7.0.4.3)
-      activerecord (= 7.0.4.3)
-      activestorage (= 7.0.4.3)
-      activesupport (= 7.0.4.3)
+    rails (7.0.5)
+      actioncable (= 7.0.5)
+      actionmailbox (= 7.0.5)
+      actionmailer (= 7.0.5)
+      actionpack (= 7.0.5)
+      actiontext (= 7.0.5)
+      actionview (= 7.0.5)
+      activejob (= 7.0.5)
+      activemodel (= 7.0.5)
+      activerecord (= 7.0.5)
+      activestorage (= 7.0.5)
+      activesupport (= 7.0.5)
       bundler (>= 1.15.0)
-      railties (= 7.0.4.3)
+      railties (= 7.0.5)
     rails-autocomplete (2.0.1)
       rails (>= 4.0)
     rails-dom-testing (2.0.3)
       activesupport (>= 4.2.0)
       nokogiri (>= 1.6)
-    rails-html-sanitizer (1.5.0)
-      loofah (~> 2.19, >= 2.19.1)
-    rails-i18n (7.0.6)
+    rails-html-sanitizer (1.6.0)
+      loofah (~> 2.21)
+      nokogiri (~> 1.14)
+    rails-i18n (7.0.7)
       i18n (>= 0.7, < 2)
       railties (>= 6.0.0, < 8)
-    railties (7.0.4.3)
-      actionpack (= 7.0.4.3)
-      activesupport (= 7.0.4.3)
+    railties (7.0.5)
+      actionpack (= 7.0.5)
+      activesupport (= 7.0.5)
       method_source
       rake (>= 12.2)
       thor (~> 1.0)
@@ -423,7 +424,7 @@ GEM
     rb-fsevent (0.11.2)
     rb-inotify (0.10.1)
       ffi (~> 1.0)
-    regexp_parser (2.7.0)
+    regexp_parser (2.8.0)
     requests (1.0.2)
     require_all (3.0.0)
     responders (3.1.0)
@@ -455,7 +456,7 @@ GEM
     sawyer (0.9.2)
       addressable (>= 2.3.5)
       faraday (>= 0.17.3, < 3)
-    selenium-webdriver (4.8.6)
+    selenium-webdriver (4.9.1)
       rexml (~> 3.2, >= 3.2.5)
       rubyzip (>= 1.2.2, < 3.0)
       websocket (~> 1.0)
@@ -480,10 +481,10 @@ GEM
       simplecov_json_formatter (~> 0.1)
     simplecov-html (0.12.3)
     simplecov_json_formatter (0.1.4)
-    sinatra (3.0.5)
+    sinatra (3.0.6)
       mustermann (~> 3.0)
       rack (~> 2.2, >= 2.2.4)
-      rack-protection (= 3.0.5)
+      rack-protection (= 3.0.6)
       tilt (~> 2.0)
     snaky_hash (2.0.1)
       hashie
@@ -496,14 +497,14 @@ GEM
       actionpack (>= 5.2)
       activesupport (>= 5.2)
       sprockets (>= 3.0.0)
-    stringio (3.0.5)
+    stringio (3.0.6)
     sugar-high (0.7.3)
     sweetloader (0.1.6)
       activesupport (>= 3.0.1)
       i18n
     terminal-table (3.0.2)
       unicode-display_width (>= 1.1.1, < 3)
-    thor (1.2.1)
+    thor (1.2.2)
     tilt (2.1.0)
     time (0.2.2)
       date
@@ -545,7 +546,7 @@ GEM
     websocket-extensions (0.1.5)
     xpath (3.2.0)
       nokogiri (~> 1.8)
-    zeitwerk (2.6.7)
+    zeitwerk (2.6.8)
     zlib (2.1.1)
 
 PLATFORMS
diff --git a/app/controllers/admin/communication/websites/preview_controller.rb b/app/controllers/admin/communication/websites/preview_controller.rb
new file mode 100644
index 000000000..786c50255
--- /dev/null
+++ b/app/controllers/admin/communication/websites/preview_controller.rb
@@ -0,0 +1,14 @@
+class Admin::Communication::Websites::PreviewController < Admin::Communication::Websites::ApplicationController
+  def style
+    render  body: @website.preview_style, 
+            content_type: "text/css"
+  end
+
+  def assets
+    path = request.path.partition('/assets').last
+    url = "#{@website.url}/assets#{path}"
+    data = URI.open(url).read
+    render  body: data, 
+            content_type: request.format.to_s
+  end
+end
\ No newline at end of file
diff --git a/app/controllers/admin/communication/websites_controller.rb b/app/controllers/admin/communication/websites_controller.rb
index 5cb335ea0..959e547d1 100644
--- a/app/controllers/admin/communication/websites_controller.rb
+++ b/app/controllers/admin/communication/websites_controller.rb
@@ -15,10 +15,6 @@ class Admin::Communication::WebsitesController < Admin::Communication::Websites:
     breadcrumb
   end
 
-  def style
-    render body: @website.preview_style, content_type: "text/css"
-  end
-
   def analytics
     breadcrumb
     add_breadcrumb t('communication.website.analytics')
diff --git a/app/models/communication/website/with_style.rb b/app/models/communication/website/with_style.rb
index e60ddf1b3..958d67761 100644
--- a/app/models/communication/website/with_style.rb
+++ b/app/models/communication/website/with_style.rb
@@ -12,7 +12,7 @@ module Communication::Website::WithStyle
     @style = ''
     load_style_from_website_url if url.present?
     load_style_from_example
-    substitute_fonts_urls_in_style!
+    make_assets_relative!
     self.update_columns style: @style,
                         style_updated_at: Date.today
   end
@@ -41,16 +41,22 @@ module Communication::Website::WithStyle
     @style << URI.open(css_url).read
   end
 
-  def substitute_fonts_urls_in_style!
-    @style.gsub! "src:url(../", "src:url(#{url}/assets/"
-    @style.gsub! "url(../", "url(#{url}/assets/"
-    @style.gsub! "url('../", "url('#{url}/assets/"
-    @style.gsub! "url(\"../", "url(\"#{url}/assets/"
-    @style
+  # /assets/fonts/Aestetico-Light/font.woff2
+  # referring to 
+  # https://www.osuny.org/assets/fonts/Aestetico-Light/font.woff2
+  # becomes 
+  # assets/fonts/Aestetico-Light/font.woff2
+  # referring to 
+  # httsp://demo.osuny.org/admin/communication/websites/6d8fb0bb-0445-46f0-8954-0e25143e7a58/assets/fonts/Aestetico-Light/font.woff2
+  # which is managed by the assets route, to avoid CORS issues
+  def make_assets_relative!
+    @style.gsub! "url(/assets", "url(assets"
+    @style.gsub! "url(../", "url(assets/"
   end
 
   def style_outdated?
     style_updated_at.nil? || style_updated_at < Date.yesterday
+    true
   end
 
 end
\ No newline at end of file
diff --git a/app/views/admin/layouts/preview.html.erb b/app/views/admin/layouts/preview.html.erb
index 1ee741dd0..99406b4c9 100644
--- a/app/views/admin/layouts/preview.html.erb
+++ b/app/views/admin/layouts/preview.html.erb
@@ -5,10 +5,16 @@
     <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
     <title><%= yield :title %></title>
     <% if @website %>
-      <link rel="stylesheet" media="all" href="<%= style_admin_communication_website_path @website.id, website_id: nil %>" />
+      <link rel="stylesheet" media="all" href="<%= admin_communication_website_style_path @website.id %>" />
     <% else %>
       <%= stylesheet_link_tag 'admin/pure', media: 'all' %>
     <% end %>
+    <style>
+    .hero {
+      margin-top: 0;
+      padding-top: 0;
+    }
+    </style>
   </head>
   <body class="full-width">
     <header class="hero">
diff --git a/config/routes/admin/communication.rb b/config/routes/admin/communication.rb
index 9e52d8b64..0c1f85009 100644
--- a/config/routes/admin/communication.rb
+++ b/config/routes/admin/communication.rb
@@ -7,9 +7,10 @@ namespace :communication do
     member do
       get :import
       post :import
-      get :style
       get :analytics
     end
+    get 'style' => 'websites/preview#style', as: :style
+    get 'assets/*path' => 'websites/preview#assets'
     resources :dependencies, controller: 'websites/dependencies', only: :index
     resources :connections, controller: 'websites/connections', only: [:index, :show]
     resources :pages, controller: 'websites/pages', path: '/:lang/pages' do
-- 
GitLab