Skip to content

Commit

Permalink
Separate static plug for collected images
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyalIcing committed Mar 18, 2021
1 parent ee48acf commit 53a4315
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
16 changes: 8 additions & 8 deletions apps/components_guide_web/lib/components_guide_web/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions apps/components_guide_web/lib/mix/tasks/template_assets.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 53a4315

Please sign in to comment.