Page MenuHomePhorge

No OneTemporary

Authored By
Unknown
Size
5 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/pleroma/upload/filter/qt_fast_start.ex b/lib/pleroma/upload/filter/qt_fast_start.ex
index 113451fa0..15dfd4566 100644
--- a/lib/pleroma/upload/filter/qt_fast_start.ex
+++ b/lib/pleroma/upload/filter/qt_fast_start.ex
@@ -1,35 +1,40 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Upload.Filter.QtFastStart do
@moduledoc """
Converts a "slow start" MP4/MOV file to a "fast start" file by relocating
the moov atom (metadata) before the mdat atom (media data) and updating
chunk offset references.
This enables video playback to start before the entire file is downloaded.
"""
@behaviour Pleroma.Upload.Filter
+ require Logger
+
@spec filter(Pleroma.Upload.t()) ::
- {:ok, :filtered} | {:ok, :noop} | {:error, String.t()}
+ {:ok, :filtered} | {:ok, :noop}
def filter(%Pleroma.Upload{tempfile: file, content_type: "video" <> _} = _upload) do
try do
binary = File.read!(file)
result = Pleroma.Helpers.QtFastStart.fix(binary)
if result != binary do
File.write!(file, result)
{:ok, :filtered}
else
{:ok, :noop}
end
rescue
- e -> {:error, "#{__MODULE__}: #{inspect(e)}"}
+ # an optimisation only: the upload goes on with the file as it is
+ e ->
+ Logger.warning("#{__MODULE__}: #{inspect(e)}")
+ {:ok, :noop}
end
end
def filter(_), do: {:ok, :noop}
end
diff --git a/test/fixtures/video_slow_start_tmp.mp4 b/test/fixtures/video_slow_start_tmp.mp4
deleted file mode 100644
index 16005fc02..000000000
Binary files a/test/fixtures/video_slow_start_tmp.mp4 and /dev/null differ
diff --git a/test/pleroma/upload/filter/qt_fast_start_test.exs b/test/pleroma/upload/filter/qt_fast_start_test.exs
index 9ec140612..a3781d85c 100644
--- a/test/pleroma/upload/filter/qt_fast_start_test.exs
+++ b/test/pleroma/upload/filter/qt_fast_start_test.exs
@@ -1,54 +1,63 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Upload.Filter.QtFastStartTest do
- use Pleroma.DataCase, async: true
+ use ExUnit.Case, async: true
+ import ExUnit.CaptureLog
alias Pleroma.Upload.Filter.QtFastStart
- test "leaves a fast-start video unchanged" do
- upload = %Pleroma.Upload{
- name: "video.mp4",
- content_type: "video/mp4",
- path: Path.absname("test/fixtures/video.mp4"),
- tempfile: Path.absname("test/fixtures/video.mp4")
+ defp upload(path, content_type \\ "video/mp4") do
+ %Pleroma.Upload{
+ name: Path.basename(path),
+ content_type: content_type,
+ path: path,
+ tempfile: path
}
+ end
- assert {:ok, :noop} = QtFastStart.filter(upload)
+ setup do
+ tmp_dir = Path.join(System.tmp_dir!(), "qt_fast_start_#{System.unique_integer([:positive])}")
+ File.mkdir_p!(tmp_dir)
+ on_exit(fn -> File.rm_rf!(tmp_dir) end)
+ %{tmp_dir: tmp_dir}
end
- test "converts a slow-start video to fast-start" do
- File.cp!(
- "test/fixtures/video_slow_start.mp4",
- "test/fixtures/video_slow_start_tmp.mp4"
- )
-
- upload = %Pleroma.Upload{
- name: "video_slow_start.mp4",
- content_type: "video/mp4",
- path: Path.absname("test/fixtures/video_slow_start_tmp.mp4"),
- tempfile: Path.absname("test/fixtures/video_slow_start_tmp.mp4")
- }
+ defp copy(fixture, tmp_dir) do
+ path = Path.join(tmp_dir, Path.basename(fixture))
+ File.cp!(fixture, path)
+ path
+ end
+
+ test "leaves a fast-start video unchanged" do
+ assert {:ok, :noop} = QtFastStart.filter(upload(Path.absname("test/fixtures/video.mp4")))
+ end
+
+ test "converts a slow-start video to fast-start", %{tmp_dir: tmp_dir} do
+ path = copy("test/fixtures/video_slow_start.mp4", tmp_dir)
- assert {:ok, :filtered} = QtFastStart.filter(upload)
+ assert {:ok, :filtered} = QtFastStart.filter(upload(path))
- fixed = File.read!("test/fixtures/video_slow_start_tmp.mp4")
+ fixed = File.read!(path)
<<atom_size::integer-big-size(32), fc1::bits-size(32), rest::bits>> = fixed
assert fc1 == "ftyp"
payload_size = (atom_size - 8) * 8
<<_payload::bits-size(payload_size), rest::bits>> = rest
<<_s2::integer-big-size(32), fc2::bits-size(32), _::bits>> = rest
assert fc2 == "moov"
end
test "noop for non-video content" do
- upload = %Pleroma.Upload{
- name: "image.jpg",
- content_type: "image/jpeg",
- path: Path.absname("test/fixtures/image.jpg"),
- tempfile: Path.absname("test/fixtures/image.jpg")
- }
-
+ upload = upload(Path.absname("test/fixtures/image.jpg"), "image/jpeg")
assert {:ok, :noop} = QtFastStart.filter(upload)
end
+
+ test "noop with a warning when the file cannot be read", %{tmp_dir: tmp_dir} do
+ log =
+ capture_log(fn ->
+ assert {:ok, :noop} = QtFastStart.filter(upload(Path.join(tmp_dir, "missing.mp4")))
+ end)
+
+ assert log =~ "QtFastStart"
+ end
end

File Metadata

Mime Type
text/x-diff
Expires
Wed, Nov 18, 9:14 AM (1 d, 6 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1840268
Default Alt Text
(5 KB)

Event Timeline