Skip to content

Commit

Permalink
Merge pull request #222 from pulibrary/use-image-urls
Browse files Browse the repository at this point in the history
Render indexed images.
  • Loading branch information
hackartisan authored Nov 11, 2024
2 parents 1dc8212 + c96dc6f commit 1025500
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ defmodule DpulCollections.IndexingPipeline.Figgy.HydrationCacheEntry do
"https://iiif-cloud.princeton.edu/iiif/2/#{uuid_path}"
end

defp is_derivative(%{"use" => [%{"@id" => "http://pcdm.org/use#ServiceFile"}]}), do: true
defp is_derivative(%{
"mime_type" => ["image/tiff"],
"use" => [%{"@id" => "http://pcdm.org/use#ServiceFile"}]
}),
do: true

defp is_derivative(_), do: false

defp page_count(%{"member_ids" => member_ids}) when is_list(member_ids) do
Expand Down
6 changes: 4 additions & 2 deletions lib/dpul_collections/item.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ defmodule DpulCollections.Item do
:title,
:date,
:page_count,
:url
:url,
:image_service_urls
]

def from_solr(nil), do: nil
Expand All @@ -21,7 +22,8 @@ defmodule DpulCollections.Item do
title: title,
date: doc["display_date_s"],
page_count: doc["page_count_i"],
url: generate_url(id, slug)
url: generate_url(id, slug),
image_service_urls: doc["image_service_urls_ss"] || []
}
end

Expand Down
3 changes: 2 additions & 1 deletion lib/dpul_collections/solr.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ defmodule DpulCollections.Solr do
"display_date_s",
"page_count_i",
"detectlang_ss",
"slug_s"
"slug_s",
"image_service_urls_ss"
]

@spec query(map()) :: map()
Expand Down
11 changes: 7 additions & 4 deletions lib/dpul_collections_web/live/item_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ defmodule DpulCollectionsWeb.ItemLive do
<div class="md:col-span-2 md:order-first">
<img
class="w-full"
src="https://picsum.photos/525/800/?random"
src={"#{Enum.at(@item.image_service_urls, 0)}/full/525,800/0/default.jpg"}
alt="main image display"
style="
background-color: lightgray;"
Expand All @@ -54,8 +54,9 @@ defmodule DpulCollectionsWeb.ItemLive do
<h2 class="text-l font-bold py-4">Pages (<%= @item.page_count %>)</h2>
<div class="flex flex-wrap gap-5 justify-center md:justify-start">
<.thumbs
:for={thumb_num <- 1..@item.page_count}
:for={{thumb, thumb_num} <- Enum.with_index(@item.image_service_urls)}
:if={@item.page_count}
thumb={thumb}
thumb_num={thumb_num}
/>
</div>
Expand All @@ -69,15 +70,17 @@ defmodule DpulCollectionsWeb.ItemLive do
<div>
<img
class="h-[465px] w-[350px] md:h-[300px] md:w-[225px]"
src="https://picsum.photos/350/350/?random"
src={"#{@thumb}/full/350,465/0/default.jpg"}
alt={"image #{@thumb_num}"}
style="
background-color: lightgray;"
width="350"
height="465"
/>
<button class="w-[350px] md:w-[225px] btn-primary">
Download
<a href={"#{@thumb}/full/full/0/default.jpg"} target="_blank">
Download
</a>
</button>
</div>
"""
Expand Down
15 changes: 10 additions & 5 deletions lib/dpul_collections_web/live/search_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,14 @@ defmodule DpulCollectionsWeb.SearchLive do
def search_item(assigns) do
~H"""
<hr />
<div class="item">
<div id={"item-#{@item.id}"} class="item">
<div class="flex flex-wrap gap-5 md:max-h-60 max-h-[20rem] overflow-hidden justify-center md:justify-start relative">
<.thumbs :for={thumb_num <- 1..@item.page_count} :if={@item.page_count} thumb_num={thumb_num} />
<.thumbs
:for={{thumb, thumb_num} <- Enum.with_index(@item.image_service_urls)}
:if={@item.page_count}
thumb={thumb}
thumb_num={thumb_num}
/>
<div :if={@item.page_count > 1} class="absolute right-0 top-0 bg-white px-4 py-2">
<%= @item.page_count %> Pages
</div>
Expand All @@ -151,7 +156,7 @@ defmodule DpulCollectionsWeb.SearchLive do
~H"""
<img
class="h-[350px] w-[350px] md:h-[225px] md:w-[225px]"
src="https://picsum.photos/350/350/?random"
src={"#{@thumb}/square/350,350/0/default.jpg"}
alt={"image #{@thumb_num}"}
style="
background-color: lightgray;"
Expand Down Expand Up @@ -248,7 +253,7 @@ defmodule DpulCollectionsWeb.SearchLive do
}
|> Helpers.clean_params([:page, :per_page])

socket = push_patch(socket, to: ~p"/search?#{params}")
socket = push_patch(socket, to: ~p"/search?#{params}", replace: true)
{:noreply, socket}
end

Expand All @@ -263,7 +268,7 @@ defmodule DpulCollectionsWeb.SearchLive do

def handle_event("paginate", %{"page" => page}, socket) when page != "..." do
params = %{socket.assigns.search_state | page: page} |> Helpers.clean_params()
socket = push_patch(socket, to: ~p"/search?#{params}")
socket = push_redirect(socket, to: ~p"/search?#{params}", replace: true)
{:noreply, socket}
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,56 @@ defmodule DpulCollections.IndexingPipeline.Figgy.HydrationCacheEntryTest do
assert doc2[:page_count_i] == 0
end

test "transforms related member image service urls" do
# Add FileMetadata with both a JP2/Pyramidal derivative, to ensure it
# picks the pyramidal
{:ok, entry} =
IndexingPipeline.write_hydration_cache_entry(%{
cache_version: 0,
record_id: "0cff895a-01ea-4895-9c3d-a8c6eaab4013",
source_cache_order: ~U[2018-03-09 20:19:35.465203Z],
related_data: %{
"member_ids" => %{
"1" => %{
"internal_resource" => "FileSet",
"metadata" => %{
"file_metadata" => [
# Not this one - it's an old JP2
%{
"id" => %{"id" => "0cff895a-01ea-4895-9c3d-a8c6eaab4014"},
"internal_resource" => "FileMetadata",
"mime_type" => ["image/jp2"],
"use" => [%{"@id" => "http://pcdm.org/use#ServiceFile"}]
},
%{
"id" => %{"id" => "0cff895a-01ea-4895-9c3d-a8c6eaab4017"},
"internal_resource" => "FileMetadata",
"mime_type" => ["image/tiff"],
"use" => [%{"@id" => "http://pcdm.org/use#ServiceFile"}]
}
]
}
}
}
},
data: %{
"id" => "0cff895a-01ea-4895-9c3d-a8c6eaab4013",
"internal_resource" => "EphemeraFolder",
"metadata" => %{
"member_ids" => [%{"id" => "1"}],
"title" => ["test title 4"]
}
}
})

doc = HydrationCacheEntry.to_solr_document(entry)

# This is the pyramidal derivative.
assert doc[:image_service_urls_ss] == [
"https://iiif-cloud.princeton.edu/iiif/2/0c%2Fff%2F89%2F0cff895a01ea48959c3da8c6eaab4017%2Fintermediate_file"
]
end

test "includes date range if found, date if not" do
entries =
FiggyTestFixtures.hydration_cache_entries()
Expand Down
13 changes: 13 additions & 0 deletions test/dpul_collections/item_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule DpulCollections.ItemTest do
use DpulCollections.DataCase

alias DpulCollections.Item

describe ".from_solr/1" do
test "populates an empty array for image_service_urls if empty" do
item = DpulCollections.Item.from_solr(%{"title_ss" => ["Title"]})

assert item.image_service_urls == []
end
end
end
47 changes: 44 additions & 3 deletions test/dpul_collections_web/live/item_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,31 @@ defmodule DpulCollectionsWeb.ItemLiveTest do
id: 1,
title_txtm: "Învăţămîntul trebuie să urmărească dezvoltarea deplină a personalităţii",
display_date_s: "2022",
page_count_i: 17
page_count_i: 17,
image_service_urls_ss: [
"https://example.com/iiif/2/image1",
"https://example.com/iiif/2/image2"
]
},
%{
id: 2,
title_txtm: "زلزلہ",
display_date_s: "2024",
page_count_i: 14
page_count_i: 14,
image_service_urls_ss: [
"https://example.com/iiif/2/image1",
"https://example.com/iiif/2/image2"
]
},
%{
id: 3,
title_txtm: "اب كوئى جنگ نه هوگى نه كبهى رات گئے، خون كى آگ كو اشكوں سے بجهانا هوگا",
display_date_s: "2022",
page_count_i: 1
page_count_i: 1,
image_service_urls_ss: [
"https://example.com/iiif/2/image1",
"https://example.com/iiif/2/image2"
]
}
],
active_collection()
Expand Down Expand Up @@ -75,6 +87,35 @@ defmodule DpulCollectionsWeb.ItemLiveTest do
assert response =~ "Învăţămîntul trebuie să urmărească dezvoltarea deplină a personalităţii"
assert response =~ "2022"
assert response =~ "17"
# Thumbnails render.
assert view
|> has_element?(
"img[src='https://example.com/iiif/2/image1/full/350,465/0/default.jpg']"
)

assert view
|> has_element?(
"img[src='https://example.com/iiif/2/image2/full/350,465/0/default.jpg']"
)

# Download links for each thumbnail
assert view
|> has_element?(
"a[href='https://example.com/iiif/2/image1/full/full/0/default.jpg']",
"Download"
)

assert view
|> has_element?(
"a[href='https://example.com/iiif/2/image2/full/full/0/default.jpg']",
"Download"
)

# Large thumbnail renders
assert view
|> has_element?(
"img[src='https://example.com/iiif/2/image2/full/350,465/0/default.jpg']"
)
end

test "/i/{:slug}/item/{:id} does not redirect with a bad id", %{conn: conn} do
Expand Down
30 changes: 30 additions & 0 deletions test/dpul_collections_web/live/search_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,30 @@ defmodule DpulCollectionsWeb.SearchLiveTest do
|> Enum.any?()
end

test "GET /search renders thumbnails for each resource", %{conn: conn} do
{:ok, view, _html} = live(conn, "/search?")

assert view
|> has_element?(
"#item-1 img[src='https://example.com/iiif/2/image1/square/350,350/0/default.jpg']"
)

assert view
|> has_element?(
"#item-1 img[src='https://example.com/iiif/2/image2/square/350,350/0/default.jpg']"
)

assert view
|> has_element?(
"#item-2 img[src='https://example.com/iiif/2/image1/square/350,350/0/default.jpg']"
)

assert view
|> has_element?(
"#item-2 img[src='https://example.com/iiif/2/image2/square/350,350/0/default.jpg']"
)
end

test "searching filters results", %{conn: conn} do
{:ok, view, _html} = live(conn, "/search?")

Expand Down Expand Up @@ -130,14 +154,20 @@ defmodule DpulCollectionsWeb.SearchLiveTest do
assert view
|> element("#paginator-previous")
|> render_click()
|> follow_redirect(conn)
|> elem(2)
|> Floki.parse_document()
|> elem(1)
|> Floki.find(~s{a[href="/i/document40/item/40"]})
|> Enum.any?()

{:ok, view, _html} = live(conn, ~p"/search?page=4")

assert view
|> element("#paginator-next")
|> render_click()
|> follow_redirect(conn)
|> elem(2)
|> Floki.parse_document()
|> elem(1)
|> Floki.find(~s{a[href="/i/document50/item/50"]})
Expand Down
6 changes: 5 additions & 1 deletion test/support/solr_test_support.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ defmodule SolrTestSupport do
title_txtm: "Document-#{n}",
display_date_s: date |> Integer.to_string(),
years_is: [date],
page_count_i: page_count
page_count_i: page_count,
image_service_urls_ss: [
"https://example.com/iiif/2/image1",
"https://example.com/iiif/2/image2"
]
}
end
end
Expand Down

0 comments on commit 1025500

Please sign in to comment.