From 9b7429dd9febb67cb06006279100dfa6d8b07932 Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Mon, 30 Sep 2024 20:20:32 +0100 Subject: [PATCH] Store image hashes in the db - part 2 --- lib/music_library/records.ex | 2 +- lib/music_library_web/controllers/image_controller.ex | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/music_library/records.ex b/lib/music_library/records.ex index 92df6a26..ef232b4b 100644 --- a/lib/music_library/records.ex +++ b/lib/music_library/records.ex @@ -58,7 +58,7 @@ defmodule MusicLibrary.Records do q = from r in Record, where: r.id == ^id, - select: r.image_data + select: %{image_data: r.image_data, image_data_hash: r.image_data_hash} Repo.one!(q) end diff --git a/lib/music_library_web/controllers/image_controller.ex b/lib/music_library_web/controllers/image_controller.ex index d653052e..4838ad49 100644 --- a/lib/music_library_web/controllers/image_controller.ex +++ b/lib/music_library_web/controllers/image_controller.ex @@ -8,10 +8,7 @@ defmodule MusicLibraryWeb.ImageController do nil -> send_resp(conn, 404, "Not found") - image_data -> - # TODO: move hash result to database - etag = :crypto.hash(:sha256, image_data) |> Base.encode16() - + %{image_data: image_data, image_data_hash: etag} -> case get_req_header(conn, "if-none-match") do [^etag] -> send_resp(conn, 304, "")