[Bugfix]: Misc. bugfixes 2024-04-16 (#189)

* Manually installed mutagen

* Stopped upgrade form from submitting on enter

* Gracefully handle duplicate enqueued downloads

* Update metadata thumbnail fetcher to use the best jpg available
This commit is contained in:
Kieran 2024-04-16 17:37:39 -07:00 committed by GitHub
parent 4721957875
commit 618711691b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 60 additions and 20 deletions

View file

@ -36,6 +36,17 @@ let liveSocket = new LiveSocket('/live', Socket, {
window.Alpine.clone(from, to)
}
}
},
hooks: {
supressEnterSubmission: {
mounted() {
this.el.addEventListener('keypress', (event) => {
if (event.key === 'Enter') {
event.preventDefault()
}
})
}
}
}
})

View file

@ -29,6 +29,9 @@ RUN yt-dlp -U
# Download Apprise
RUN python3 -m pip install -U apprise --break-system-packages
# Download Mutagen for music thumbnail generation
RUN python3 -m pip install -U mutagen --break-system-packages
# Set the locale
RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && locale-gen
ENV LANG en_US.UTF-8

View file

@ -54,11 +54,18 @@ defmodule Pinchflat.Metadata.MetadataFileHelpers do
@doc """
Downloads and stores a thumbnail for a media item, returning the filepath.
Chooses the highest quality jpg thumbnail available.
Returns binary()
"""
def download_and_store_thumbnail_for(database_record, metadata_map) do
thumbnail_url = metadata_map["thumbnail"]
thumbnail_url =
metadata_map["thumbnails"]
|> Enum.filter(&(&1["preference"] && String.ends_with?(&1["url"], ".jpg")))
|> Enum.sort(&(&1["preference"] >= &2["preference"]))
|> List.first()
|> Map.get("url")
filepath = generate_filepath_for(database_record, Path.basename(thumbnail_url))
thumbnail_blob = fetch_thumbnail_from_url(thumbnail_url)

View file

@ -3,10 +3,11 @@ defmodule Pinchflat.UpgradeButtonLive do
def render(assigns) do
~H"""
<form phx-change="check_matching_text">
<form id="upgradeForm" phx-change="check_matching_text" phx-hook="supressEnterSubmission">
<.input type="text" name="unlock-pro-textbox" value="" />
</form>
<%!-- The setTimeout is so the modal has time to disappear before it's removed --%>
<.button
class="w-full mt-4"
type="button"

View file

@ -50,7 +50,13 @@ defmodule PinchflatWeb.MediaItems.MediaItemController do
def force_download(conn, %{"media_item_id" => id}) do
media_item = Media.get_media_item!(id)
{:ok, _} = MediaDownloadWorker.kickoff_with_task(media_item, %{force: true})
:ok =
case MediaDownloadWorker.kickoff_with_task(media_item, %{force: true}) do
{:ok, _} -> :ok
{:error, :duplicate_job} -> :ok
err -> err
end
conn
|> put_flash(:info, "Download task enqueued.")

View file

@ -90,6 +90,9 @@ RUN yt-dlp -U
# Download Apprise
RUN python3 -m pip install -U apprise --break-system-packages
# Download Mutagen for music thumbnail generation
RUN python3 -m pip install -U mutagen --break-system-packages
# Set the locale
RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && locale-gen
ENV LANG en_US.UTF-8

View file

@ -731,9 +731,7 @@ defmodule Pinchflat.MediaTest do
metadata: %{
metadata_filepath: MetadataFileHelpers.compress_and_store_metadata_for(media_item, %{}),
thumbnail_filepath:
MetadataFileHelpers.download_and_store_thumbnail_for(media_item, %{
"thumbnail" => "https://example.com/thumbnail.jpg"
})
MetadataFileHelpers.download_and_store_thumbnail_for(media_item, render_parsed_metadata(:media_metadata))
}
}
@ -760,9 +758,7 @@ defmodule Pinchflat.MediaTest do
metadata: %{
metadata_filepath: MetadataFileHelpers.compress_and_store_metadata_for(media_item, %{}),
thumbnail_filepath:
MetadataFileHelpers.download_and_store_thumbnail_for(media_item, %{
"thumbnail" => "https://example.com/thumbnail.jpg"
})
MetadataFileHelpers.download_and_store_thumbnail_for(media_item, render_parsed_metadata(:media_metadata))
}
}
@ -831,9 +827,7 @@ defmodule Pinchflat.MediaTest do
metadata: %{
metadata_filepath: MetadataFileHelpers.compress_and_store_metadata_for(media_item, %{}),
thumbnail_filepath:
MetadataFileHelpers.download_and_store_thumbnail_for(media_item, %{
"thumbnail" => "https://example.com/thumbnail.jpg"
})
MetadataFileHelpers.download_and_store_thumbnail_for(media_item, render_parsed_metadata(:media_metadata))
}
}

View file

@ -54,13 +54,11 @@ defmodule Pinchflat.Metadata.MetadataFileHelpersTest do
describe "download_and_store_thumbnail_for/2" do
setup do
# This tests that the HTTP endpoint is being called with every test
expect(HTTPClientMock, :get, fn url, _headers, _opts ->
assert url =~ "example.com"
expect(HTTPClientMock, :get, fn _url, _headers, _opts ->
{:ok, "thumbnail data"}
end)
metadata = %{"thumbnail" => "example.com/thumbnail.jpg"}
metadata = render_parsed_metadata(:media_metadata)
{:ok, %{metadata: metadata}}
end
@ -68,7 +66,7 @@ defmodule Pinchflat.Metadata.MetadataFileHelpersTest do
test "returns the filepath", %{media_item: media_item, metadata: metadata} do
filepath = Helpers.download_and_store_thumbnail_for(media_item, metadata)
assert filepath =~ ~r{/media_items/#{media_item.id}/thumbnail.jpg}
assert filepath =~ ~r{/media_items/#{media_item.id}/maxresdefault.jpg}
end
test "creates folder structure based on passed record", %{media_item: media_item, metadata: metadata} do
@ -77,11 +75,19 @@ defmodule Pinchflat.Metadata.MetadataFileHelpersTest do
assert File.exists?(Path.dirname(filepath))
end
test "the filename and extension is based on the URL", %{media_item: media_item} do
metadata = %{"thumbnail" => "example.com/maxres.webp"}
test "chooses the highest preference jpg thumbnail available", %{media_item: media_item} do
metadata = %{
"thumbnails" => [
%{"url" => "https://i.ytimg.com/vi/ABC123/img_1.jpg", "preference" => -1},
%{"url" => "https://i.ytimg.com/vi/ABC123/img_2.jpg", "preference" => 1},
%{"url" => "https://i.ytimg.com/vi/ABC123/img_3.jpg", "preference" => -10},
%{"url" => "https://i.ytimg.com/vi/ABC123/img_4.webp", "preference" => 10}
]
}
filepath = Helpers.download_and_store_thumbnail_for(media_item, metadata)
assert Path.basename(filepath) == "maxres.webp"
assert filepath =~ ~r{/media_items/#{media_item.id}/img_2.jpg}
end
end

View file

@ -97,6 +97,15 @@ defmodule PinchflatWeb.MediaItemControllerTest do
assert [_] = all_enqueued(worker: MediaDownloadWorker)
end
test "doesn't freak out if the task is a duplicate", %{conn: conn} do
media_item = media_item_fixture()
MediaDownloadWorker.kickoff_with_task(media_item, %{force: true})
post(conn, ~p"/sources/#{media_item.source_id}/media/#{media_item.id}/force_download")
assert [_] = all_enqueued(worker: MediaDownloadWorker)
end
test "forces a download even if one wouldn't normally run", %{conn: conn} do
media_item = media_item_fixture(%{media_filepath: nil})