consolidate image serving and clean up pipeline
Move all image URL logic into ProductImage.url/2 and thumbnail_url/1, remove dead on-demand generation code from Optimizer, strip controller routes down to SVG recolor only, fix mockup startup check to verify all variant formats, and isolate test image cache directory. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -17,13 +17,9 @@ defmodule SimpleshopTheme.Images.OptimizeWorkerTest do
|
||||
|
||||
assert :ok = perform_job(OptimizeWorker, %{image_id: image.id})
|
||||
|
||||
# Verify pre-generated variants were created (AVIF and WebP only, not JPEG)
|
||||
for w <- [400, 800, 1200], fmt <- [:avif, :webp] do
|
||||
for w <- [400, 800, 1200], fmt <- [:avif, :webp, :jpg] do
|
||||
assert File.exists?(cache_path(image.id, w, fmt))
|
||||
end
|
||||
|
||||
# JPEG is generated on-demand, not pre-generated
|
||||
refute File.exists?(cache_path(image.id, 400, :jpg))
|
||||
end
|
||||
|
||||
test "cancels for missing image" do
|
||||
|
||||
@@ -62,16 +62,14 @@ defmodule SimpleshopTheme.Images.OptimizerTest do
|
||||
|
||||
assert {:ok, [400, 800, 1200]} = Optimizer.process_for_image(image.id)
|
||||
|
||||
# Only AVIF and WebP are pre-generated (JPEG is on-demand)
|
||||
for w <- [400, 800, 1200], fmt <- [:avif, :webp] do
|
||||
# Source WebP for Plug.Static serving
|
||||
assert File.exists?(Path.join(Optimizer.cache_dir(), "#{image.id}.webp"))
|
||||
|
||||
for w <- [400, 800, 1200], fmt <- [:avif, :webp, :jpg] do
|
||||
assert File.exists?(cache_path(image.id, w, fmt)),
|
||||
"Missing #{w}.#{fmt}"
|
||||
end
|
||||
|
||||
# JPEG should NOT be pre-generated
|
||||
refute File.exists?(cache_path(image.id, 400, :jpg))
|
||||
|
||||
# Thumbnail is still pre-generated as JPEG
|
||||
assert File.exists?(cache_path(image.id, "thumb", :jpg))
|
||||
end
|
||||
|
||||
@@ -138,36 +136,4 @@ defmodule SimpleshopTheme.Images.OptimizerTest do
|
||||
refute Optimizer.disk_variants_exist?(image.id, 1200)
|
||||
end
|
||||
end
|
||||
|
||||
describe "generate_jpeg_on_demand/3" do
|
||||
test "generates JPEG variant and caches to disk" do
|
||||
image = image_fixture(%{source_width: 1200, source_height: 800})
|
||||
|
||||
# JPEG shouldn't exist yet
|
||||
refute File.exists?(cache_path(image.id, 400, :jpg))
|
||||
|
||||
# Generate on-demand
|
||||
{:ok, path} = Optimizer.generate_jpeg_on_demand(image.data, image.id, 400)
|
||||
|
||||
assert File.exists?(path)
|
||||
assert path == cache_path(image.id, 400, :jpg)
|
||||
end
|
||||
|
||||
test "returns cached path if JPEG already exists" do
|
||||
image = image_fixture(%{source_width: 1200, source_height: 800})
|
||||
|
||||
# Generate first time
|
||||
{:ok, path1} = Optimizer.generate_jpeg_on_demand(image.data, image.id, 800)
|
||||
{:ok, %{mtime: mtime1}} = File.stat(path1)
|
||||
|
||||
Process.sleep(1100)
|
||||
|
||||
# Generate second time - should return cached
|
||||
{:ok, path2} = Optimizer.generate_jpeg_on_demand(image.data, image.id, 800)
|
||||
{:ok, %{mtime: mtime2}} = File.stat(path2)
|
||||
|
||||
assert path1 == path2
|
||||
assert mtime1 == mtime2, "File was regenerated instead of cached"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -67,28 +67,49 @@ defmodule SimpleshopTheme.Products.ProductImageTest do
|
||||
# Display helpers
|
||||
# =============================================================================
|
||||
|
||||
describe "display_url/2" do
|
||||
describe "url/2" do
|
||||
test "prefers local image_id over src" do
|
||||
image = %{image_id: "abc-123", src: "https://cdn.example.com/img.jpg"}
|
||||
assert ProductImage.display_url(image) == "/images/abc-123/variant/800.webp"
|
||||
assert ProductImage.url(image) == "/image_cache/abc-123-800.webp"
|
||||
end
|
||||
|
||||
test "accepts custom size" do
|
||||
test "accepts custom width" do
|
||||
image = %{image_id: "abc-123", src: "https://cdn.example.com/img.jpg"}
|
||||
assert ProductImage.display_url(image, 400) == "/images/abc-123/variant/400.webp"
|
||||
assert ProductImage.url(image, 400) == "/image_cache/abc-123-400.webp"
|
||||
end
|
||||
|
||||
test "handles mockup URLs with size suffix" do
|
||||
image = %{image_id: nil, src: "/mockups/product-1"}
|
||||
assert ProductImage.url(image, 800) == "/mockups/product-1-800.webp"
|
||||
end
|
||||
|
||||
test "falls back to src when no image_id" do
|
||||
image = %{image_id: nil, src: "https://cdn.example.com/img.jpg"}
|
||||
assert ProductImage.display_url(image) == "https://cdn.example.com/img.jpg"
|
||||
assert ProductImage.url(image) == "https://cdn.example.com/img.jpg"
|
||||
end
|
||||
|
||||
test "returns nil when neither image_id nor src" do
|
||||
assert ProductImage.display_url(%{image_id: nil, src: nil}) == nil
|
||||
assert ProductImage.url(%{image_id: nil, src: nil}) == nil
|
||||
end
|
||||
|
||||
test "returns nil for nil input" do
|
||||
assert ProductImage.display_url(nil) == nil
|
||||
assert ProductImage.url(nil) == nil
|
||||
end
|
||||
end
|
||||
|
||||
describe "thumbnail_url/1" do
|
||||
test "returns thumb path for local image" do
|
||||
image = %{image_id: "abc-123"}
|
||||
assert ProductImage.thumbnail_url(image) == "/image_cache/abc-123-thumb.jpg"
|
||||
end
|
||||
|
||||
test "falls back to src when no image_id" do
|
||||
image = %{image_id: nil, src: "https://cdn.example.com/img.jpg"}
|
||||
assert ProductImage.thumbnail_url(image) == "https://cdn.example.com/img.jpg"
|
||||
end
|
||||
|
||||
test "returns nil for nil input" do
|
||||
assert ProductImage.thumbnail_url(nil) == nil
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -3,81 +3,9 @@ defmodule SimpleshopThemeWeb.ImageControllerTest do
|
||||
|
||||
alias SimpleshopTheme.Media
|
||||
|
||||
# Minimal valid PNG (1x1 transparent pixel)
|
||||
@png_binary <<137, 80, 78, 71, 13, 10, 26, 10>>
|
||||
@svg_content ~s(<svg xmlns="http://www.w3.org/2000/svg"><circle fill="#000000" r="10"/></svg>)
|
||||
@sample_jpeg File.read!("test/fixtures/sample_1200x800.jpg")
|
||||
|
||||
describe "show/2" do
|
||||
test "returns 404 for non-existent image", %{conn: conn} do
|
||||
conn = get(conn, ~p"/images/#{Ecto.UUID.generate()}")
|
||||
assert response(conn, 404) =~ "Image not found"
|
||||
end
|
||||
|
||||
test "serves image with proper content type and caching headers", %{conn: conn} do
|
||||
# Upload a real JPEG which gets converted to WebP
|
||||
{:ok, image} =
|
||||
Media.upload_image(%{
|
||||
image_type: "logo",
|
||||
filename: "test.jpg",
|
||||
content_type: "image/jpeg",
|
||||
file_size: byte_size(@sample_jpeg),
|
||||
data: @sample_jpeg
|
||||
})
|
||||
|
||||
conn = get(conn, ~p"/images/#{image.id}")
|
||||
|
||||
assert response(conn, 200)
|
||||
# Image is converted to WebP for storage
|
||||
assert get_resp_header(conn, "content-type") == ["image/webp; charset=utf-8"]
|
||||
assert get_resp_header(conn, "cache-control") == ["public, max-age=31536000, immutable"]
|
||||
assert get_resp_header(conn, "etag") == [~s("#{image.id}")]
|
||||
end
|
||||
end
|
||||
|
||||
describe "thumbnail/2" do
|
||||
test "returns 404 for non-existent image", %{conn: conn} do
|
||||
conn = get(conn, ~p"/images/#{Ecto.UUID.generate()}/thumbnail")
|
||||
assert response(conn, 404) =~ "Image not found"
|
||||
end
|
||||
|
||||
test "generates thumbnail on-demand and serves as JPEG", %{conn: conn} do
|
||||
# Upload a real image to test thumbnail generation
|
||||
{:ok, image} =
|
||||
Media.upload_image(%{
|
||||
image_type: "logo",
|
||||
filename: "test.jpg",
|
||||
content_type: "image/jpeg",
|
||||
file_size: byte_size(@sample_jpeg),
|
||||
data: @sample_jpeg
|
||||
})
|
||||
|
||||
conn = get(conn, ~p"/images/#{image.id}/thumbnail")
|
||||
|
||||
assert response(conn, 200)
|
||||
# Thumbnail is served as JPEG
|
||||
assert get_resp_header(conn, "content-type") == ["image/jpeg; charset=utf-8"]
|
||||
assert get_resp_header(conn, "cache-control") == ["public, max-age=31536000, immutable"]
|
||||
end
|
||||
|
||||
test "falls back to full image when image data is invalid", %{conn: conn} do
|
||||
# This uses an invalid PNG header that can't be processed
|
||||
{:ok, image} =
|
||||
Media.upload_image(%{
|
||||
image_type: "logo",
|
||||
filename: "test.png",
|
||||
content_type: "image/png",
|
||||
file_size: byte_size(@png_binary),
|
||||
data: @png_binary
|
||||
})
|
||||
|
||||
conn = get(conn, ~p"/images/#{image.id}/thumbnail")
|
||||
|
||||
# Falls back to WebP since that's what we tried to convert to
|
||||
assert response(conn, 200)
|
||||
end
|
||||
end
|
||||
|
||||
describe "recolored_svg/2" do
|
||||
test "returns 404 for non-existent image", %{conn: conn} do
|
||||
conn = get(conn, ~p"/images/#{Ecto.UUID.generate()}/recolored/ff6600")
|
||||
@@ -88,10 +16,10 @@ defmodule SimpleshopThemeWeb.ImageControllerTest do
|
||||
{:ok, image} =
|
||||
Media.upload_image(%{
|
||||
image_type: "logo",
|
||||
filename: "test.png",
|
||||
content_type: "image/png",
|
||||
file_size: byte_size(@png_binary),
|
||||
data: @png_binary
|
||||
filename: "test.jpg",
|
||||
content_type: "image/jpeg",
|
||||
file_size: byte_size(@sample_jpeg),
|
||||
data: @sample_jpeg
|
||||
})
|
||||
|
||||
conn = get(conn, ~p"/images/#{image.id}/recolored/ff6600")
|
||||
|
||||
Reference in New Issue
Block a user