Download images for sources (#96)

* [WIP] hooked up photo downloading to source metadata worker

* Added new attributes for source images

* Added option to profile form
This commit is contained in:
Kieran
2024-03-19 18:34:01 -07:00
committed by GitHub
parent 05f3deebfa
commit 565a1d4972
32 changed files with 584 additions and 18 deletions
@@ -107,4 +107,37 @@ defmodule Pinchflat.Filesystem.FilesystemHelpersTest do
assert {:error, _} = FilesystemHelpers.delete_file_and_remove_empty_directories(filepath)
end
end
describe "cp_p!/2" do
test "copies a file from source to destination" do
source = "#{tmpfile_directory()}/source.json"
FilesystemHelpers.write_p!(source, "TEST")
destination = "#{tmpfile_directory()}/destination.json"
refute File.exists?(destination)
FilesystemHelpers.cp_p!(source, destination)
assert File.exists?(destination)
assert File.read!(destination) == "TEST"
File.rm!(source)
File.rm!(destination)
end
test "creates directories as needed" do
source = "#{tmpfile_directory()}/source.json"
FilesystemHelpers.write_p!(source, "TEST")
destination = "#{tmpfile_directory()}/foo/bar/destination.json"
refute File.exists?(destination)
FilesystemHelpers.cp_p!(source, destination)
assert File.exists?(destination)
File.rm!(source)
File.rm!(destination)
end
end
defp tmpfile_directory do
Application.get_env(:pinchflat, :tmpfile_directory)
end
end
@@ -0,0 +1,78 @@
defmodule Pinchflat.Metadata.SourceImageParserTest do
use Pinchflat.DataCase
alias Pinchflat.Metadata.SourceImageParser
@base_dir Application.compile_env(:pinchflat, :tmpfile_directory)
describe "store_source_images/2" do
test "returns a map of image types and locations" do
metadata = render_parsed_metadata(:channel_source_metadata)
expected = %{
banner_filepath: "#{@base_dir}/banner.jpg",
fanart_filepath: "#{@base_dir}/fanart.jpg",
poster_filepath: "#{@base_dir}/poster.jpg"
}
assert SourceImageParser.store_source_images(@base_dir, metadata) == expected
end
test "returns the avatar_uncropped as the poster" do
metadata = %{
"thumbnails" => [
%{"id" => "avatar_uncropped", "filepath" => "/app/test/support/files/channel_photos/a.0.jpg"}
]
}
expected = %{
poster_filepath: "#{@base_dir}/poster.jpg"
}
assert SourceImageParser.store_source_images(@base_dir, metadata) == expected
end
test "returns the banner_uncropped as the fanart" do
metadata = %{
"thumbnails" => [
%{"id" => "banner_uncropped", "filepath" => "/app/test/support/files/channel_photos/a.0.jpg"}
]
}
expected = %{
fanart_filepath: "#{@base_dir}/fanart.jpg"
}
assert SourceImageParser.store_source_images(@base_dir, metadata) == expected
end
test "doesn't return a banner if no suitable images are found" do
metadata = %{
"thumbnails" => [
%{
"id" => "1",
"filepath" => "foo.jpg",
"width" => 100,
"height" => 100
}
]
}
expected = %{}
assert SourceImageParser.store_source_images(@base_dir, metadata) == expected
end
test "doesn't blow up if empty metadata is passed" do
metadata = %{}
assert SourceImageParser.store_source_images(@base_dir, metadata) == %{}
end
test "doesn't blow up if no thumbnails are passed" do
metadata = %{"thumbnails" => []}
assert SourceImageParser.store_source_images(@base_dir, metadata) == %{}
end
end
end
@@ -4,6 +4,7 @@ defmodule Pinchflat.Metadata.SourceMetadataStorageWorkerTest do
import Pinchflat.SourcesFixtures
import Pinchflat.ProfilesFixtures
alias Pinchflat.Sources
alias Pinchflat.Metadata.MetadataFileHelpers
alias Pinchflat.Metadata.SourceMetadataStorageWorker
@@ -84,6 +85,80 @@ defmodule Pinchflat.Metadata.SourceMetadataStorageWorkerTest do
end
end
describe "perform/1 when testing source image downloading" do
test "downloads and stores source images" do
stub(YtDlpRunnerMock, :run, fn
_url, _opts, ot when ot == @source_details_ot ->
filename = Path.join([Application.get_env(:pinchflat, :media_directory), "Season 1", "bar.mp4"])
{:ok, source_details_return_fixture(%{filename: filename})}
_url, _opts, ot when ot == @metadata_ot ->
{:ok, render_metadata(:channel_source_metadata)}
end)
profile = media_profile_fixture(%{download_source_images: true})
source = source_fixture(media_profile_id: profile.id)
perform_job(SourceMetadataStorageWorker, %{id: source.id})
source = Repo.reload(source)
assert source.fanart_filepath
assert source.poster_filepath
assert source.banner_filepath
assert File.exists?(source.fanart_filepath)
assert File.exists?(source.poster_filepath)
assert File.exists?(source.banner_filepath)
Sources.delete_source(source, delete_files: true)
end
test "does not store source images if the profile is not set to" do
stub(YtDlpRunnerMock, :run, fn
_url, _opts, ot when ot == @source_details_ot ->
filename = Path.join([Application.get_env(:pinchflat, :media_directory), "Season 1", "bar.mp4"])
{:ok, source_details_return_fixture(%{filename: filename})}
_url, _opts, ot when ot == @metadata_ot ->
{:ok, render_metadata(:channel_source_metadata)}
end)
profile = media_profile_fixture(%{download_source_images: false})
source = source_fixture(media_profile_id: profile.id)
perform_job(SourceMetadataStorageWorker, %{id: source.id})
source = Repo.reload(source)
refute source.fanart_filepath
refute source.poster_filepath
refute source.banner_filepath
end
test "does not store source images if the series directory cannot be determined" do
stub(YtDlpRunnerMock, :run, fn
_url, _opts, ot when ot == @source_details_ot ->
filename = Path.join([Application.get_env(:pinchflat, :media_directory), "foo", "bar.mp4"])
{:ok, source_details_return_fixture(%{filename: filename})}
_url, _opts, ot when ot == @metadata_ot ->
{:ok, render_metadata(:channel_source_metadata)}
end)
profile = media_profile_fixture(%{download_source_images: true})
source = source_fixture(media_profile_id: profile.id)
perform_job(SourceMetadataStorageWorker, %{id: source.id})
source = Repo.reload(source)
refute source.fanart_filepath
refute source.poster_filepath
refute source.banner_filepath
end
end
describe "perform/1 when determining the series_directory" do
test "sets the series directory based on the returned media filepath" do
stub(YtDlpRunnerMock, :run, fn
@@ -151,5 +151,15 @@ defmodule Pinchflat.YtDlp.MediaCollectionTest do
assert {:error, %Jason.DecodeError{}} = MediaCollection.get_source_metadata(@channel_url)
end
test "allows you to pass additional opts" do
expect(YtDlpRunnerMock, :run, fn _url, opts, _ot ->
assert opts == [playlist_items: 0, real_opt: :yup]
{:ok, "{}"}
end)
assert {:ok, _} = MediaCollection.get_source_metadata(@channel_url, real_opt: :yup)
end
end
end