Compare commits
4 Commits
v2024.9.12
...
v2024.10.2
| Author | SHA1 | Date | |
|---|---|---|---|
| 85feb67299 | |||
| 4843d7d6c2 | |||
| a0b9e49486 | |||
| 0163e85e76 |
@@ -12,6 +12,7 @@ defmodule Pinchflat.Downloading.MediaDownloadWorker do
|
||||
alias Pinchflat.Tasks
|
||||
alias Pinchflat.Repo
|
||||
alias Pinchflat.Media
|
||||
alias Pinchflat.Media.FileSyncing
|
||||
alias Pinchflat.Downloading.MediaDownloader
|
||||
|
||||
alias Pinchflat.Lifecycle.UserScripts.CommandRunner, as: UserScriptRunner
|
||||
@@ -85,6 +86,7 @@ defmodule Pinchflat.Downloading.MediaDownloadWorker do
|
||||
media_redownloaded_at: get_redownloaded_at(is_quality_upgrade)
|
||||
})
|
||||
|
||||
:ok = FileSyncing.delete_outdated_files(media_item, updated_media_item)
|
||||
run_user_script(:media_downloaded, updated_media_item)
|
||||
|
||||
:ok
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
defmodule Pinchflat.Media.FileSyncing do
|
||||
@moduledoc """
|
||||
Functions for ensuring file state is accurately reflected in the database.
|
||||
"""
|
||||
|
||||
alias Pinchflat.Media
|
||||
alias Pinchflat.Utils.MapUtils
|
||||
alias Pinchflat.Media.MediaItem
|
||||
alias Pinchflat.Utils.FilesystemUtils, as: FSUtils
|
||||
|
||||
@doc """
|
||||
Deletes files that are no longer needed by a media item.
|
||||
|
||||
This means that if a media item has been updated, the old and new versions
|
||||
can be passed and any files that are no longer needed will be deleted.
|
||||
|
||||
An example is a video that gets its quality upgraded and its name changes
|
||||
between original download and re-download. The old file will exist on-disk
|
||||
with the old name but the database entry will point to the new file. This
|
||||
function can be used to delete the old file in this case.
|
||||
|
||||
Returns :ok
|
||||
"""
|
||||
def delete_outdated_files(old_media_item, new_media_item) do
|
||||
non_subtitle_keys = MediaItem.filepath_attributes() -- [:subtitle_filepaths]
|
||||
|
||||
old_non_subtitles = Map.take(old_media_item, non_subtitle_keys)
|
||||
old_subtitles = MapUtils.from_nested_list(old_media_item.subtitle_filepaths)
|
||||
new_non_subtitles = Map.take(new_media_item, non_subtitle_keys)
|
||||
new_subtitles = MapUtils.from_nested_list(new_media_item.subtitle_filepaths)
|
||||
|
||||
handle_file_deletion(old_non_subtitles, new_non_subtitles)
|
||||
handle_file_deletion(old_subtitles, new_subtitles)
|
||||
|
||||
:ok
|
||||
end
|
||||
|
||||
@doc """
|
||||
Nillifies any media item filepaths that don't exist on disk for a list of media items
|
||||
|
||||
returns [%MediaItem{}]
|
||||
"""
|
||||
def sync_file_presence_on_disk(media_items) do
|
||||
Enum.map(media_items, fn media_item ->
|
||||
new_attributes = sync_media_item_files(media_item)
|
||||
# Doing this one-by-one instead of batching since this process
|
||||
# can take time and a batch could let MediaItem state get out of sync
|
||||
{:ok, updated_media_item} = Media.update_media_item(media_item, new_attributes)
|
||||
|
||||
updated_media_item
|
||||
end)
|
||||
end
|
||||
|
||||
defp handle_file_deletion(old_attributes, new_attributes) do
|
||||
# The logic:
|
||||
# - A file should only be deleted if it exists and the new file is different
|
||||
# - The new attributes are the ones we're interested in keeping
|
||||
# - If the old attributes have a key that doesn't exist in the new attributes, don't touch it.
|
||||
# This is good for archiving but may be unpopular for other users so this may change.
|
||||
|
||||
Enum.each(new_attributes, fn {key, new_filepath} ->
|
||||
old_filepath = Map.get(old_attributes, key)
|
||||
files_have_changed = old_filepath && new_filepath && old_filepath != new_filepath
|
||||
files_exist_on_disk = files_have_changed && File.exists?(old_filepath) && File.exists?(new_filepath)
|
||||
|
||||
if files_exist_on_disk && !FSUtils.filepaths_reference_same_file?(old_filepath, new_filepath) do
|
||||
FSUtils.delete_file_and_remove_empty_directories(old_filepath)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
defp sync_media_item_files(media_item) do
|
||||
non_subtitle_keys = MediaItem.filepath_attributes() -- [:subtitle_filepaths]
|
||||
subtitle_keys = MapUtils.from_nested_list(media_item.subtitle_filepaths)
|
||||
non_subtitles = Map.take(media_item, non_subtitle_keys)
|
||||
|
||||
# This one is checking for the negative (ie: only update if the file doesn't exist)
|
||||
new_non_subtitle_attrs =
|
||||
Enum.reduce(non_subtitles, %{}, fn {key, filepath}, acc ->
|
||||
if filepath && File.exists?(filepath), do: acc, else: Map.put(acc, key, nil)
|
||||
end)
|
||||
|
||||
# This one is checking for the positive (ie: only update if the file exists)
|
||||
# This is because subtitles, being an array type in the DB, are most easily updated
|
||||
# by a full replacement rather than finding the actual diff
|
||||
new_subtitle_attrs =
|
||||
Enum.reduce(subtitle_keys, [], fn {key, filepath}, acc ->
|
||||
if filepath && File.exists?(filepath), do: acc ++ [[key, filepath]], else: acc
|
||||
end)
|
||||
|
||||
Map.put(new_non_subtitle_attrs, :subtitle_filepaths, new_subtitle_attrs)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,38 @@
|
||||
defmodule Pinchflat.Media.FileSyncingWorker do
|
||||
@moduledoc false
|
||||
|
||||
use Oban.Worker,
|
||||
queue: :local_data,
|
||||
tags: ["sources", "local_data"]
|
||||
|
||||
alias __MODULE__
|
||||
alias Pinchflat.Repo
|
||||
alias Pinchflat.Tasks
|
||||
alias Pinchflat.Sources
|
||||
alias Pinchflat.Media.FileSyncing
|
||||
|
||||
@doc """
|
||||
Starts the source file syncing worker.
|
||||
|
||||
Returns {:ok, %Task{}} | {:error, %Ecto.Changeset{}}
|
||||
"""
|
||||
def kickoff_with_task(source, opts \\ []) do
|
||||
%{id: source.id}
|
||||
|> FileSyncingWorker.new(opts)
|
||||
|> Tasks.create_job_with_task(source)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes a profile and optionally deletes its files
|
||||
|
||||
Returns :ok
|
||||
"""
|
||||
@impl Oban.Worker
|
||||
def perform(%Oban.Job{args: %{"id" => source_id}}) do
|
||||
source = Repo.preload(Sources.get_source!(source_id), :media_items)
|
||||
|
||||
FileSyncing.sync_file_presence_on_disk(source.media_items)
|
||||
|
||||
:ok
|
||||
end
|
||||
end
|
||||
@@ -20,6 +20,24 @@ defmodule Pinchflat.Utils.FilesystemUtils do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if two filepaths reference the same file.
|
||||
|
||||
Useful if you have a relative and absolute filepath and want to be sure they're the same file.
|
||||
Also works with symlinks.
|
||||
|
||||
Returns boolean()
|
||||
"""
|
||||
def filepaths_reference_same_file?(filepath_1, filepath_2) do
|
||||
{:ok, stat_1} = File.stat(filepath_1)
|
||||
{:ok, stat_2} = File.stat(filepath_2)
|
||||
|
||||
identifier_1 = "#{stat_1.major_device}:#{stat_1.minor_device}:#{stat_1.inode}"
|
||||
identifier_2 = "#{stat_2.major_device}:#{stat_2.minor_device}:#{stat_2.inode}"
|
||||
|
||||
identifier_1 == identifier_2
|
||||
end
|
||||
|
||||
@doc """
|
||||
Generates a temporary file and returns its path. The file is empty and has the given type.
|
||||
Generates all the directories in the path if they don't exist.
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
defmodule Pinchflat.Utils.MapUtils do
|
||||
@moduledoc """
|
||||
Utility methods for working with maps
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Converts a nested list of 2-element tuples or lists into a map.
|
||||
|
||||
Returns map()
|
||||
"""
|
||||
def from_nested_list(list) do
|
||||
Enum.reduce(list, %{}, fn
|
||||
[key, value], acc -> Map.put(acc, key, value)
|
||||
{key, value}, acc -> Map.put(acc, key, value)
|
||||
end)
|
||||
end
|
||||
end
|
||||
@@ -8,6 +8,7 @@ defmodule PinchflatWeb.Sources.SourceController do
|
||||
alias Pinchflat.Sources.Source
|
||||
alias Pinchflat.Media.MediaItem
|
||||
alias Pinchflat.Profiles.MediaProfile
|
||||
alias Pinchflat.Media.FileSyncingWorker
|
||||
alias Pinchflat.Sources.SourceDeletionWorker
|
||||
alias Pinchflat.Downloading.DownloadingHelpers
|
||||
alias Pinchflat.SlowIndexing.SlowIndexingHelpers
|
||||
@@ -175,6 +176,15 @@ defmodule PinchflatWeb.Sources.SourceController do
|
||||
)
|
||||
end
|
||||
|
||||
def sync_files_on_disk(conn, %{"source_id" => id}) do
|
||||
wrap_forced_action(
|
||||
conn,
|
||||
id,
|
||||
"File sync enqueued.",
|
||||
&FileSyncingWorker.kickoff_with_task/1
|
||||
)
|
||||
end
|
||||
|
||||
defp wrap_forced_action(conn, source_id, message, fun) do
|
||||
source = Sources.get_source!(source_id)
|
||||
fun.(source)
|
||||
|
||||
@@ -38,7 +38,7 @@
|
||||
<.link
|
||||
href={~p"/sources/#{@source}/force_download_pending"}
|
||||
method="post"
|
||||
data-confirm="Are you sure you want to force a download of all *pending* media items? This isn't normally needed."
|
||||
data-confirm="Are you sure you want to force a download of all pending media items? This isn't normally needed."
|
||||
>
|
||||
Download Pending
|
||||
</.link>
|
||||
@@ -47,7 +47,7 @@
|
||||
<.link
|
||||
href={~p"/sources/#{@source}/force_redownload"}
|
||||
method="post"
|
||||
data-confirm="Are you sure you want to re-download all currently downloaded media items? This isn't normally needed and won't change anything if the files already exist."
|
||||
data-confirm="Are you sure you want to re-download all currently downloaded media items? This doesn't upgrade your media, but will download any missing files if your settings have changed. This isn't normally needed."
|
||||
>
|
||||
Redownload Existing
|
||||
</.link>
|
||||
@@ -70,6 +70,15 @@
|
||||
Refresh Metadata
|
||||
</.link>
|
||||
</:option>
|
||||
<:option>
|
||||
<.link
|
||||
href={~p"/sources/#{@source}/sync_files_on_disk"}
|
||||
method="post"
|
||||
data-confirm="Are you sure you want to sync files? This will update media items if their files have been deleted. File addition or moves are not detected. This isn't normally needed."
|
||||
>
|
||||
Sync Files on Disk
|
||||
</.link>
|
||||
</:option>
|
||||
<:option>
|
||||
<div class="h-px w-full bg-bodydark2"></div>
|
||||
</:option>
|
||||
|
||||
@@ -40,6 +40,7 @@ defmodule PinchflatWeb.Router do
|
||||
post "/force_redownload", Sources.SourceController, :force_redownload
|
||||
post "/force_index", Sources.SourceController, :force_index
|
||||
post "/force_metadata_refresh", Sources.SourceController, :force_metadata_refresh
|
||||
post "/sync_files_on_disk", Sources.SourceController, :sync_files_on_disk
|
||||
|
||||
resources "/media", MediaItems.MediaItemController, only: [:show, :edit, :update, :delete] do
|
||||
post "/force_download", MediaItems.MediaItemController, :force_download
|
||||
|
||||
@@ -4,7 +4,7 @@ defmodule Pinchflat.MixProject do
|
||||
def project do
|
||||
[
|
||||
app: :pinchflat,
|
||||
version: "2024.9.12",
|
||||
version: "2024.10.2",
|
||||
elixir: "~> 1.17",
|
||||
elixirc_paths: elixirc_paths(Mix.env()),
|
||||
start_permanent: Mix.env() == :prod,
|
||||
|
||||
@@ -61,7 +61,7 @@
|
||||
"telemetry_metrics": {:hex, :telemetry_metrics, "0.6.2", "2caabe9344ec17eafe5403304771c3539f3b6e2f7fb6a6f602558c825d0d0bfb", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "9b43db0dc33863930b9ef9d27137e78974756f5f198cae18409970ed6fa5b561"},
|
||||
"telemetry_poller": {:hex, :telemetry_poller, "1.0.0", "db91bb424e07f2bb6e73926fcafbfcbcb295f0193e0a00e825e589a0a47e8453", [:rebar3], [{:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "b3a24eafd66c3f42da30fc3ca7dda1e9d546c12250a2d60d7b81d264fbec4f6e"},
|
||||
"timex": {:hex, :timex, "3.7.11", "bb95cb4eb1d06e27346325de506bcc6c30f9c6dea40d1ebe390b262fad1862d1", [:mix], [{:combine, "~> 0.10", [hex: :combine, repo: "hexpm", optional: false]}, {:gettext, "~> 0.20", [hex: :gettext, repo: "hexpm", optional: false]}, {:tzdata, "~> 1.1", [hex: :tzdata, repo: "hexpm", optional: false]}], "hexpm", "8b9024f7efbabaf9bd7aa04f65cf8dcd7c9818ca5737677c7b76acbc6a94d1aa"},
|
||||
"tzdata": {:hex, :tzdata, "1.1.1", "20c8043476dfda8504952d00adac41c6eda23912278add38edc140ae0c5bcc46", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "a69cec8352eafcd2e198dea28a34113b60fdc6cb57eb5ad65c10292a6ba89787"},
|
||||
"tzdata": {:hex, :tzdata, "1.1.2", "45e5f1fcf8729525ec27c65e163be5b3d247ab1702581a94674e008413eef50b", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "cec7b286e608371602318c414f344941d5eb0375e14cfdab605cca2fe66cba8b"},
|
||||
"unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"},
|
||||
"websock": {:hex, :websock, "0.5.3", "2f69a6ebe810328555b6fe5c831a851f485e303a7c8ce6c5f675abeb20ebdadc", [:mix], [], "hexpm", "6105453d7fac22c712ad66fab1d45abdf049868f253cf719b625151460b8b453"},
|
||||
"websock_adapter": {:hex, :websock_adapter, "0.5.7", "65fa74042530064ef0570b75b43f5c49bb8b235d6515671b3d250022cb8a1f9e", [:mix], [{:bandit, ">= 0.6.0", [hex: :bandit, repo: "hexpm", optional: true]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.6", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:websock, "~> 0.5", [hex: :websock, repo: "hexpm", optional: false]}], "hexpm", "d0f478ee64deddfec64b800673fd6e0c8888b079d9f3444dd96d2a98383bdbd1"},
|
||||
|
||||
@@ -236,6 +236,26 @@ defmodule Pinchflat.Downloading.MediaDownloadWorkerTest do
|
||||
|
||||
perform_job(MediaDownloadWorker, %{id: media_item.id, force: true})
|
||||
end
|
||||
|
||||
test "deletes old files if the media item has been updated" do
|
||||
expect(YtDlpRunnerMock, :run, 1, fn _url, _opts, _ot, _addl ->
|
||||
tmp_media_item = media_item_with_attachments()
|
||||
metadata = render_parsed_metadata(:media_metadata)
|
||||
metadata = Map.put(metadata, "filepath", tmp_media_item.media_filepath)
|
||||
|
||||
{:ok, Phoenix.json_library().encode!(metadata)}
|
||||
end)
|
||||
|
||||
expect(YtDlpRunnerMock, :run, 1, fn _url, _opts, _ot, _addl -> {:ok, ""} end)
|
||||
|
||||
old_media_item = media_item_with_attachments()
|
||||
perform_job(MediaDownloadWorker, %{id: old_media_item.id, force: true})
|
||||
updated_media_item = Repo.reload(old_media_item)
|
||||
|
||||
assert updated_media_item.media_filepath != old_media_item.media_filepath
|
||||
refute File.exists?(old_media_item.media_filepath)
|
||||
assert File.exists?(updated_media_item.media_filepath)
|
||||
end
|
||||
end
|
||||
|
||||
describe "perform/1 when testing user script callbacks" do
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
defmodule Pinchflat.Media.FileSyncingTest do
|
||||
use Pinchflat.DataCase
|
||||
|
||||
import Pinchflat.MediaFixtures
|
||||
|
||||
alias Pinchflat.Media.FileSyncing
|
||||
|
||||
describe "delete_outdated_files/2" do
|
||||
test "deletes outdated non-subtitle files" do
|
||||
new_media_item = media_item_with_attachments()
|
||||
old_media_item = media_item_with_attachments()
|
||||
|
||||
assert :ok = FileSyncing.delete_outdated_files(old_media_item, new_media_item)
|
||||
|
||||
assert File.exists?(new_media_item.media_filepath)
|
||||
refute File.exists?(old_media_item.media_filepath)
|
||||
end
|
||||
|
||||
test "doesn't delete non-subtitle files if the new file is the same" do
|
||||
new_media_item = media_item_with_attachments()
|
||||
old_media_item = media_item_fixture(%{media_filepath: new_media_item.media_filepath})
|
||||
|
||||
assert :ok = FileSyncing.delete_outdated_files(old_media_item, new_media_item)
|
||||
|
||||
assert File.exists?(new_media_item.media_filepath)
|
||||
assert File.exists?(old_media_item.media_filepath)
|
||||
end
|
||||
|
||||
test "doesn't delete the old file if the new file is missing that key" do
|
||||
new_media_item = media_item_fixture(%{media_filepath: nil})
|
||||
old_media_item = media_item_with_attachments()
|
||||
|
||||
assert :ok = FileSyncing.delete_outdated_files(old_media_item, new_media_item)
|
||||
|
||||
assert File.exists?(old_media_item.media_filepath)
|
||||
end
|
||||
|
||||
test "deletes outdated subtitle files" do
|
||||
new_media_item = media_item_with_attachments()
|
||||
old_media_item = media_item_with_attachments()
|
||||
|
||||
assert :ok = FileSyncing.delete_outdated_files(old_media_item, new_media_item)
|
||||
|
||||
assert File.exists?(get_subtitle_filepath(new_media_item, "en"))
|
||||
refute File.exists?(get_subtitle_filepath(old_media_item, "en"))
|
||||
end
|
||||
|
||||
test "keeps old subtitle files if the new file is the same" do
|
||||
new_media_item = media_item_with_attachments()
|
||||
old_media_item = media_item_fixture(%{subtitle_filepaths: new_media_item.subtitle_filepaths})
|
||||
|
||||
assert :ok = FileSyncing.delete_outdated_files(old_media_item, new_media_item)
|
||||
|
||||
assert File.exists?(get_subtitle_filepath(new_media_item, "en"))
|
||||
assert File.exists?(get_subtitle_filepath(old_media_item, "en"))
|
||||
end
|
||||
|
||||
test "doesn't delete old subtitle files if the new file is missing that key" do
|
||||
new_media_item = media_item_fixture(%{subtitle_filepaths: []})
|
||||
old_media_item = media_item_with_attachments()
|
||||
|
||||
assert :ok = FileSyncing.delete_outdated_files(old_media_item, new_media_item)
|
||||
|
||||
assert File.exists?(get_subtitle_filepath(old_media_item, "en"))
|
||||
end
|
||||
end
|
||||
|
||||
describe "sync_file_presence_on_disk/1" do
|
||||
test "removes attributes whose files are missing" do
|
||||
media_item = media_item_fixture(%{media_filepath: "/tmp/missing_file.mp4"})
|
||||
|
||||
assert media_item.media_filepath
|
||||
assert [updated_media_item] = FileSyncing.sync_file_presence_on_disk([media_item])
|
||||
refute updated_media_item.media_filepath
|
||||
end
|
||||
|
||||
test "doesn't remove attributes where the files still exist" do
|
||||
media_item = media_item_with_attachments()
|
||||
|
||||
assert media_item.media_filepath
|
||||
assert [updated_media_item] = FileSyncing.sync_file_presence_on_disk([media_item])
|
||||
assert updated_media_item.media_filepath
|
||||
end
|
||||
|
||||
test "doesn't touch other attributes if some are missing and some aren't" do
|
||||
media_item = media_item_with_attachments()
|
||||
File.rm(media_item.media_filepath)
|
||||
|
||||
assert media_item.thumbnail_filepath
|
||||
assert media_item.media_filepath
|
||||
assert [updated_media_item] = FileSyncing.sync_file_presence_on_disk([media_item])
|
||||
assert updated_media_item.thumbnail_filepath
|
||||
refute updated_media_item.media_filepath
|
||||
end
|
||||
|
||||
test "removes subtitle files that are missing" do
|
||||
media_item = media_item_fixture(%{subtitle_filepaths: [["en", "/tmp/missing_file.srt"]]})
|
||||
|
||||
assert get_subtitle_filepath(media_item, "en")
|
||||
assert [updated_media_item] = FileSyncing.sync_file_presence_on_disk([media_item])
|
||||
refute get_subtitle_filepath(updated_media_item, "en")
|
||||
end
|
||||
|
||||
test "doesn't remove subtitle files that still exist" do
|
||||
media_item = media_item_with_attachments()
|
||||
|
||||
assert get_subtitle_filepath(media_item, "en")
|
||||
assert [updated_media_item] = FileSyncing.sync_file_presence_on_disk([media_item])
|
||||
assert get_subtitle_filepath(updated_media_item, "en")
|
||||
end
|
||||
end
|
||||
|
||||
defp get_subtitle_filepath(media_item, language) do
|
||||
Enum.reduce_while(media_item.subtitle_filepaths, nil, fn [lang, filepath], acc ->
|
||||
if lang == language do
|
||||
{:halt, filepath}
|
||||
else
|
||||
{:cont, acc}
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,37 @@
|
||||
defmodule Pinchflat.Media.FileSyncingWorkerTest do
|
||||
use Pinchflat.DataCase
|
||||
|
||||
import Pinchflat.MediaFixtures
|
||||
import Pinchflat.SourcesFixtures
|
||||
|
||||
alias Pinchflat.Media.FileSyncingWorker
|
||||
|
||||
describe "kickoff_with_task/3" do
|
||||
test "starts the worker" do
|
||||
source = source_fixture()
|
||||
|
||||
assert [] = all_enqueued(worker: FileSyncingWorker)
|
||||
assert {:ok, _} = FileSyncingWorker.kickoff_with_task(source)
|
||||
assert [_] = all_enqueued(worker: FileSyncingWorker)
|
||||
end
|
||||
|
||||
test "attaches a task" do
|
||||
source = source_fixture()
|
||||
|
||||
assert {:ok, task} = FileSyncingWorker.kickoff_with_task(source)
|
||||
assert task.source_id == source.id
|
||||
end
|
||||
end
|
||||
|
||||
describe "perform/1" do
|
||||
test "syncs file presence on disk" do
|
||||
source = source_fixture()
|
||||
media_item = media_item_fixture(%{media_filepath: "/tmp/missing.mp4", source_id: source.id})
|
||||
|
||||
perform_job(FileSyncingWorker, %{"id" => source.id})
|
||||
updated_media_item = Repo.reload!(media_item)
|
||||
|
||||
refute updated_media_item.media_filepath
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -37,6 +37,46 @@ defmodule Pinchflat.Utils.FilesystemUtilsTest do
|
||||
end
|
||||
end
|
||||
|
||||
describe "filepaths_reference_same_file?/2" do
|
||||
setup do
|
||||
filepath = FilesystemUtils.generate_metadata_tmpfile(:json)
|
||||
|
||||
on_exit(fn -> File.rm!(filepath) end)
|
||||
|
||||
{:ok, %{filepath: filepath}}
|
||||
end
|
||||
|
||||
test "returns true if the files are the same", %{filepath: filepath} do
|
||||
assert FilesystemUtils.filepaths_reference_same_file?(filepath, filepath)
|
||||
end
|
||||
|
||||
test "returns true if different filepaths point to the same file", %{filepath: filepath} do
|
||||
short_path = Path.expand(filepath)
|
||||
long_path = Path.join(["/tmp", "..", filepath])
|
||||
|
||||
assert short_path != long_path
|
||||
assert FilesystemUtils.filepaths_reference_same_file?(short_path, long_path)
|
||||
end
|
||||
|
||||
test "returns true if the files are symlinked", %{filepath: filepath} do
|
||||
tmpfile_directory = Application.get_env(:pinchflat, :tmpfile_directory)
|
||||
other_filepath = Path.join([tmpfile_directory, "symlink.json"])
|
||||
:ok = File.ln_s!(filepath, other_filepath)
|
||||
|
||||
assert FilesystemUtils.filepaths_reference_same_file?(filepath, other_filepath)
|
||||
|
||||
File.rm!(other_filepath)
|
||||
end
|
||||
|
||||
test "returns false if the files are different", %{filepath: filepath} do
|
||||
other_filepath = FilesystemUtils.generate_metadata_tmpfile(:json)
|
||||
|
||||
refute FilesystemUtils.filepaths_reference_same_file?(filepath, other_filepath)
|
||||
|
||||
File.rm!(other_filepath)
|
||||
end
|
||||
end
|
||||
|
||||
describe "generate_metadata_tmpfile/1" do
|
||||
test "creates a tmpfile and returns its path" do
|
||||
res = FilesystemUtils.generate_metadata_tmpfile(:json)
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
defmodule Pinchflat.Utils.MapUtilsTest do
|
||||
use Pinchflat.DataCase
|
||||
|
||||
alias Pinchflat.Utils.MapUtils
|
||||
|
||||
describe "from_nested_list/1" do
|
||||
test "creates a map from a nested 2-element tuple list" do
|
||||
list = [
|
||||
{"key1", "value1"},
|
||||
{"key2", "value2"}
|
||||
]
|
||||
|
||||
assert MapUtils.from_nested_list(list) == %{
|
||||
"key1" => "value1",
|
||||
"key2" => "value2"
|
||||
}
|
||||
end
|
||||
|
||||
test "creates a map from a nested 2-element list of lists" do
|
||||
list = [
|
||||
["key1", "value1"],
|
||||
["key2", "value2"]
|
||||
]
|
||||
|
||||
assert MapUtils.from_nested_list(list) == %{
|
||||
"key1" => "value1",
|
||||
"key2" => "value2"
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -7,6 +7,7 @@ defmodule PinchflatWeb.SourceControllerTest do
|
||||
|
||||
alias Pinchflat.Repo
|
||||
alias Pinchflat.Settings
|
||||
alias Pinchflat.Media.FileSyncingWorker
|
||||
alias Pinchflat.Sources.SourceDeletionWorker
|
||||
alias Pinchflat.Downloading.MediaDownloadWorker
|
||||
alias Pinchflat.Metadata.SourceMetadataStorageWorker
|
||||
@@ -268,6 +269,23 @@ defmodule PinchflatWeb.SourceControllerTest do
|
||||
end
|
||||
end
|
||||
|
||||
describe "sync_files_on_disk" do
|
||||
test "forces a file sync", %{conn: conn} do
|
||||
source = source_fixture()
|
||||
|
||||
assert [] = all_enqueued(worker: FileSyncingWorker)
|
||||
post(conn, ~p"/sources/#{source.id}/sync_files_on_disk")
|
||||
assert [_] = all_enqueued(worker: FileSyncingWorker)
|
||||
end
|
||||
|
||||
test "redirects to the source page", %{conn: conn} do
|
||||
source = source_fixture()
|
||||
|
||||
conn = post(conn, ~p"/sources/#{source.id}/sync_files_on_disk")
|
||||
assert redirected_to(conn) == ~p"/sources/#{source.id}"
|
||||
end
|
||||
end
|
||||
|
||||
defp create_source(_) do
|
||||
source = source_fixture()
|
||||
media_item = media_item_with_attachments(%{source_id: source.id})
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
1
|
||||
00:00:00,000 --> 00:00:02,500
|
||||
Welcome to the Example Subtitle File!
|
||||
|
||||
2
|
||||
00:00:03,000 --> 00:00:06,000
|
||||
This is a demonstration of SRT subtitles.
|
||||
|
||||
3
|
||||
00:00:07,000 --> 00:00:10,500
|
||||
You can use SRT files to add subtitles to your videos.
|
||||
|
||||
4
|
||||
00:00:12,000 --> 00:00:15,000
|
||||
Each subtitle entry consists of a number, a timecode,
|
||||
and the subtitle text.
|
||||
|
||||
5
|
||||
00:00:16,000 --> 00:00:20,000
|
||||
The timecode format is hours:minutes:seconds,milliseconds.
|
||||
|
||||
6
|
||||
00:00:21,000 --> 00:00:25,000
|
||||
You can adjust the timing to match your video.
|
||||
|
||||
7
|
||||
00:00:26,000 --> 00:00:30,000
|
||||
Make sure the subtitle text is clear and readable.
|
||||
|
||||
8
|
||||
00:00:31,000 --> 00:00:35,000
|
||||
And that's how you create an SRT subtitle file!
|
||||
|
||||
9
|
||||
00:00:36,000 --> 00:00:40,000
|
||||
Enjoy adding subtitles to your videos!
|
||||
@@ -73,16 +73,19 @@ defmodule Pinchflat.MediaFixtures do
|
||||
"#{:rand.uniform(1_000_000)}"
|
||||
])
|
||||
|
||||
stored_media_filepath = Path.join(base_dir, "#media.mp4")
|
||||
stored_media_filepath = Path.join(base_dir, "media.mp4")
|
||||
thumbnail_filepath = Path.join(base_dir, "thumbnail.jpg")
|
||||
subtitle_filepath = Path.join(base_dir, "subtitle.en.srt")
|
||||
|
||||
FilesystemUtils.cp_p!(media_filepath_fixture(), stored_media_filepath)
|
||||
FilesystemUtils.cp_p!(thumbnail_filepath_fixture(), thumbnail_filepath)
|
||||
FilesystemUtils.cp_p!(subtitle_filepath_fixture(), subtitle_filepath)
|
||||
|
||||
merged_attrs =
|
||||
Map.merge(attrs, %{
|
||||
media_filepath: stored_media_filepath,
|
||||
thumbnail_filepath: thumbnail_filepath
|
||||
thumbnail_filepath: thumbnail_filepath,
|
||||
subtitle_filepaths: [["en", subtitle_filepath]]
|
||||
})
|
||||
|
||||
media_item_fixture(merged_attrs)
|
||||
@@ -124,6 +127,16 @@ defmodule Pinchflat.MediaFixtures do
|
||||
])
|
||||
end
|
||||
|
||||
def subtitle_filepath_fixture do
|
||||
Path.join([
|
||||
File.cwd!(),
|
||||
"test",
|
||||
"support",
|
||||
"files",
|
||||
"subtitle.srt"
|
||||
])
|
||||
end
|
||||
|
||||
def infojson_filepath_fixture do
|
||||
Path.join([
|
||||
File.cwd!(),
|
||||
|
||||
Reference in New Issue
Block a user