From 88003b2158ebf615ad2f3129331fef91194345e4 Mon Sep 17 00:00:00 2001 From: trueChazza Date: Thu, 23 Jun 2022 12:03:50 +1200 Subject: [PATCH] feat: add get subtitle helper --- lib/media_server_web/helpers.ex | 6 ++++++ test/media_server_web/helpers_test.exs | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/lib/media_server_web/helpers.ex b/lib/media_server_web/helpers.ex index 0efd6ae2..0d3d4e26 100644 --- a/lib/media_server_web/helpers.ex +++ b/lib/media_server_web/helpers.ex @@ -63,4 +63,10 @@ defmodule MediaServerWeb.Helpers do def reduce_size_for_poster_url(url) do String.replace(url, "original", "w342") end + + def get_subtitle(path) do + File.ls!(path) + |> Enum.filter(fn item -> String.contains?(item, ".srt") end) + |> List.first() + end end diff --git a/test/media_server_web/helpers_test.exs b/test/media_server_web/helpers_test.exs index de3439b1..479c5c36 100644 --- a/test/media_server_web/helpers_test.exs +++ b/test/media_server_web/helpers_test.exs @@ -6,4 +6,24 @@ defmodule MediaServerWeb.HelpersTest do "https://some.domain.url/t/p/original/somePosterIdentifier.jpg" ) === "https://some.domain.url/t/p/w342/somePosterIdentifier.jpg" end + + @path "fixtures/movies/Caminandes Llama Drama (2013)" + @expected [".DS_Store", "Caminandes.Llama.Drama.1080p.mp4", "Caminandes.Llama.Drama.1080p.srt"] + @filtered "Caminandes.Llama.Drama.1080p.srt" + + test "it should have subtitle" do + assert File.ls!(@path) == @expected + + assert MediaServerWeb.Helpers.get_subtitle(@path) == @filtered + end + + @path_without "fixtures/movies/Caminandes Gran Dillama (2013)" + @expected_without [".DS_Store", "Caminandes.Gran.Dillama.1080p.mp4"] + @filtered_without nil + + test "it should not have subtitle" do + assert File.ls!(@path_without) == @expected_without + + assert MediaServerWeb.Helpers.get_subtitle(@path_without) == @filtered_without + end end