From 53a43155df85ada2094d7e1aabed988126c7decc Mon Sep 17 00:00:00 2001 From: Patrick Smith Date: Thu, 18 Mar 2021 07:15:40 +0000 Subject: [PATCH] Separate static plug for collected images --- .../lib/components_guide_web/endpoint.ex | 16 ++++++++-------- .../lib/mix/tasks/template_assets.ex | 3 +++ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/apps/components_guide_web/lib/components_guide_web/endpoint.ex b/apps/components_guide_web/lib/components_guide_web/endpoint.ex index a26564b8..ab3f1738 100644 --- a/apps/components_guide_web/lib/components_guide_web/endpoint.ex +++ b/apps/components_guide_web/lib/components_guide_web/endpoint.ex @@ -13,6 +13,13 @@ defmodule ComponentsGuideWeb.Endpoint do ) socket("/live", Phoenix.LiveView.Socket, websocket: [connect_info: [session: @session_options]]) + + plug(Plug.Static, + at: "/collected/image", + from: {:components_guide_web, "priv/static/collected/image"}, + gzip: false, + only: ~w(svg png jpg gif) + ) # Serve at "/" the static files from "priv/static" directory. # @@ -22,14 +29,7 @@ defmodule ComponentsGuideWeb.Endpoint do at: "/", from: :components_guide_web, gzip: false, - only: ~w(css fonts images js favicon.ico robots.txt collected) - ) - - plug(Plug.Static, - at: "/collected/image", - from: {:components_guide_web, "priv/static/collected/image"}, - gzip: false, - only: ~w(svg png jpg gif) + only: ~w(css fonts images js favicon.ico robots.txt) ) # Code reloading can be explicitly enabled under the diff --git a/apps/components_guide_web/lib/mix/tasks/template_assets.ex b/apps/components_guide_web/lib/mix/tasks/template_assets.ex index 4e7ca7cc..24b326f2 100644 --- a/apps/components_guide_web/lib/mix/tasks/template_assets.ex +++ b/apps/components_guide_web/lib/mix/tasks/template_assets.ex @@ -29,11 +29,14 @@ defmodule Mix.Tasks.TemplateAssets do media_type = MIME.from_path(image_path) hash = :crypto.hash(:sha256, data) hash_base64 = hash |> Base.url_encode64() + destination_dir = Path.join(static_collected_dir(), media_type) File.mkdir_p!(destination_dir) + extension = MIME.extensions(media_type) |> List.first() destination_path = Path.join(destination_dir, "#{hash_base64}.#{extension}") File.copy!(image_path, destination_path) + Mix.shell().info("Copied #{image_path} to #{destination_path}") end end