diff --git a/.dockerignore b/.dockerignore index f201e922..956c90ab 100644 --- a/.dockerignore +++ b/.dockerignore @@ -4,3 +4,4 @@ _build deps .elixir_ls priv +native/philomena/target diff --git a/config/runtime.exs b/config/runtime.exs index e5c35a87..64bd8090 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -27,6 +27,7 @@ config :philomena, image_url_root: System.fetch_env!("IMAGE_URL_ROOT"), badge_url_root: System.fetch_env!("BADGE_URL_ROOT"), mailer_address: System.fetch_env!("MAILER_ADDRESS"), + mediaproc_addr: System.fetch_env!("MEDIAPROC_ADDR"), tag_file_root: System.fetch_env!("TAG_FILE_ROOT"), site_domains: System.fetch_env!("SITE_DOMAINS"), tag_url_root: System.fetch_env!("TAG_URL_ROOT"), diff --git a/docker-compose.yml b/docker-compose.yml index 1f13cb2f..b1b62e48 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -30,6 +30,7 @@ services: - IMAGE_URL_ROOT=/img - BADGE_URL_ROOT=/badge-img - TAG_URL_ROOT=/tag-img + - MEDIAPROC_ADDR=mediaproc:1500 - OPENSEARCH_URL=http://opensearch:9200 - REDIS_HOST=valkey - DATABASE_URL=ecto://postgres:postgres@postgres/philomena_dev @@ -52,6 +53,7 @@ services: - app_deps_data:/srv/philomena/deps - app_native_data:/srv/philomena/priv/native depends_on: + - mediaproc - postgres - opensearch - valkey @@ -89,6 +91,18 @@ services: - .:/srv/philomena attach: false + mediaproc: + build: + context: . + dockerfile: ./docker/mediaproc/Dockerfile + attach: false + deploy: + resources: + limits: + cpus: '4' + memory: 8gb + pids: 8192 + web: build: context: . diff --git a/docker/app/Dockerfile b/docker/app/Dockerfile index 69e16ab8..4780e50b 100644 --- a/docker/app/Dockerfile +++ b/docker/app/Dockerfile @@ -1,28 +1,12 @@ FROM elixir:1.18.1-alpine -ADD https://api.github.com/repos/philomena-dev/FFmpeg/git/refs/heads/release/6.1 /tmp/ffmpeg_version.json -RUN (echo "https://github.com/philomena-dev/prebuilt-ffmpeg/raw/master"; cat /etc/apk/repositories) > /tmp/repositories \ - && cp /tmp/repositories /etc/apk/repositories \ - && apk update --allow-untrusted \ - && apk add inotify-tools build-base git ffmpeg ffmpeg-dev npm nodejs file-dev libjpeg-turbo-dev libpng-dev gifsicle optipng libjpeg-turbo-utils librsvg rsvg-convert imagemagick postgresql16-client wget rust cargo --allow-untrusted \ +RUN apk add inotify-tools build-base git npm nodejs postgresql16-client wget rust cargo \ && mix local.hex --force \ && mix local.rebar --force -ADD https://api.github.com/repos/philomena-dev/cli_intensities/git/refs/heads/master /tmp/cli_intensities_version.json -RUN git clone --depth 1 https://github.com/philomena-dev/cli_intensities /tmp/cli_intensities \ - && cd /tmp/cli_intensities \ - && make -j$(nproc) install - -ADD https://api.github.com/repos/philomena-dev/mediatools/git/refs/heads/master /tmp/mediatools_version.json -RUN git clone --depth 1 https://github.com/philomena-dev/mediatools /tmp/mediatools \ - && ln -s /usr/lib/librsvg-2.so.2 /usr/lib/librsvg-2.so \ - && cd /tmp/mediatools \ - && make -j$(nproc) install - COPY docker/app/run-development /usr/local/bin/run-development COPY docker/app/run-test /usr/local/bin/run-test -COPY docker/app/safe-rsvg-convert /usr/local/bin/safe-rsvg-convert COPY docker/app/purge-cache /usr/local/bin/purge-cache ENV PATH=$PATH:/root/.cargo/bin EXPOSE 5173 -CMD run-development +CMD ["/usr/local/bin/run-development"] diff --git a/docker/mediaproc/Dockerfile b/docker/mediaproc/Dockerfile new file mode 100644 index 00000000..25b1748a --- /dev/null +++ b/docker/mediaproc/Dockerfile @@ -0,0 +1,75 @@ +FROM rust:1.83-slim + +RUN apt update \ + && apt install -y build-essential git libmagic-dev libturbojpeg0-dev libpng-dev \ + gifsicle optipng libjpeg-turbo-progs librsvg2-bin librsvg2-dev file imagemagick \ + libx264-dev libx265-dev libvpx-dev libdav1d-dev libaom-dev libopus-dev \ + libmp3lame-dev libvorbis-dev libwebp-dev libjxl-dev yasm wget + +ADD https://api.github.com/repos/philomena-dev/FFmpeg/git/refs/heads/release/7.1 /tmp/ffmpeg_version.json +ADD https://api.github.com/repos/philomena-dev/cli_intensities/git/refs/heads/master /tmp/cli_intensities_version.json +ADD https://api.github.com/repos/philomena-dev/mediatools/git/refs/heads/master /tmp/mediatools_version.json + +RUN wget -qO /tmp/FFmpeg.tar.gz https://github.com/philomena-dev/FFmpeg/archive/refs/heads/release/7.1.tar.gz \ + && wget -qO /tmp/cli_intensities.tar.gz https://github.com/philomena-dev/cli_intensities/archive/refs/heads/master.tar.gz \ + && wget -qO /tmp/mediatools.tar.gz https://github.com/philomena-dev/mediatools/archive/refs/heads/master.tar.gz + +RUN cd /tmp \ + && tar -xf FFmpeg.tar.gz \ + && tar -xf cli_intensities.tar.gz \ + && tar -xf mediatools.tar.gz \ + && cd /tmp/FFmpeg-release-7.1 \ + && ./configure \ + --prefix=/usr \ + --disable-everything \ + --disable-stripping \ + --disable-static \ + --disable-ffplay \ + --disable-doc \ + --disable-htmlpages \ + --disable-manpages \ + --disable-podpages \ + --disable-txtpages \ + --disable-protocols \ + --enable-shared \ + --enable-pic \ + --enable-pthreads \ + --enable-gpl \ + --enable-avfilter \ + --enable-bsf=extract_extradata \ + --enable-decoder=aac,apng,av1,gif,h264,hevc,jpeg2000,jpegxl,libaom-av1,libdav1d,libvorbis,libvpx_vp8,libvpx_vp9,mp3,mjpeg,opus,png,vorbis,vp8,vp9,webvtt \ + --enable-demuxer=apng,gif,image2,image_gif_pipe,image_jpeg_pipe,image_png_pipe,image_webp_pipe,matroska,mjpeg,mjpeg_2000,mov,webm \ + --enable-encoder=aac,apng,gif,jpegxl,libmp3lame,libaom-av1,libvorbis,libopus,libvpx_vp8,libvpx_vp9,libx265,libx264,opus,mjpeg,png,vorbis,webvtt \ + --enable-filter=concat,palettegen,paletteuse,scale,setpts,setsar,settb,split,trim \ + --enable-libaom \ + --enable-libjxl \ + --enable-libdav1d \ + --enable-libopus \ + --enable-libmp3lame \ + --enable-libvpx \ + --enable-libvorbis \ + --enable-libx264 \ + --enable-libx265 \ + --enable-libwebp \ + --enable-muxer=apng,image2,gif,matroska,mp4,webp,webm \ + --enable-parser=aac,gif,h264,hevc,jpeg2000,jpegxl,mjpeg,opus,png,vorbis,vp8,vp9,webp \ + --enable-protocol=concat,data,file,subfile \ + && make -j$(nproc) install \ + && cd /tmp/cli_intensities-master \ + && make -j$(nproc) install \ + && cd /tmp/mediatools-master \ + && make -j$(nproc) install + +COPY native/philomena /tmp/philomena +COPY docker/mediaproc/safe-rsvg-convert /usr/bin/safe-rsvg-convert + +RUN cd /tmp/philomena \ + && cargo build --release -p mediaproc_server \ + && cp target/release/mediaproc_server /usr/bin/mediaproc_server + +# Set up unprivileged user account +RUN useradd -ms /bin/bash mediaproc +USER mediaproc +WORKDIR /home/mediaproc +ENV RUST_LOG=trace +CMD ["/usr/bin/mediaproc_server", "0.0.0.0:1500"] diff --git a/docker/app/safe-rsvg-convert b/docker/mediaproc/safe-rsvg-convert similarity index 100% rename from docker/app/safe-rsvg-convert rename to docker/mediaproc/safe-rsvg-convert diff --git a/lib/philomena/native.ex b/lib/philomena/native.ex index 14eeaa17..b4f2f244 100644 --- a/lib/philomena/native.ex +++ b/lib/philomena/native.ex @@ -12,6 +12,10 @@ defmodule Philomena.Native do @spec camo_image_url(String.t()) :: String.t() def camo_image_url(_uri), do: :erlang.nif_error(:nif_not_loaded) + @spec async_process_command(String.t(), String.t(), [String.t()]) :: :ok + def async_process_command(_server_addr, _program, _arguments), + do: :erlang.nif_error(:nif_not_loaded) + @spec zip_open_writer(Path.t()) :: {:ok, reference()} | {:error, atom()} def zip_open_writer(_path), do: :erlang.nif_error(:nif_not_loaded) diff --git a/lib/philomena_media/analyzers/gif.ex b/lib/philomena_media/analyzers/gif.ex index 982d7a31..2c1365d0 100644 --- a/lib/philomena_media/analyzers/gif.ex +++ b/lib/philomena_media/analyzers/gif.ex @@ -3,6 +3,7 @@ defmodule PhilomenaMedia.Analyzers.Gif do alias PhilomenaMedia.Analyzers.Analyzer alias PhilomenaMedia.Analyzers.Result + alias PhilomenaMedia.Remote @behaviour Analyzer @@ -20,7 +21,7 @@ defmodule PhilomenaMedia.Analyzers.Gif do end defp stats(file) do - case System.cmd("mediastat", [file]) do + case Remote.cmd("mediastat", [file]) do {output, 0} -> [_size, frames, width, height, num, den] = output diff --git a/lib/philomena_media/analyzers/jpeg.ex b/lib/philomena_media/analyzers/jpeg.ex index 60b29e04..cca1d881 100644 --- a/lib/philomena_media/analyzers/jpeg.ex +++ b/lib/philomena_media/analyzers/jpeg.ex @@ -3,6 +3,7 @@ defmodule PhilomenaMedia.Analyzers.Jpeg do alias PhilomenaMedia.Analyzers.Analyzer alias PhilomenaMedia.Analyzers.Result + alias PhilomenaMedia.Remote @behaviour Analyzer @@ -20,7 +21,7 @@ defmodule PhilomenaMedia.Analyzers.Jpeg do end defp stats(file) do - case System.cmd("mediastat", [file]) do + case Remote.cmd("mediastat", [file]) do {output, 0} -> [_size, _frames, width, height, num, den] = output diff --git a/lib/philomena_media/analyzers/png.ex b/lib/philomena_media/analyzers/png.ex index 83cb506f..a01d68ab 100644 --- a/lib/philomena_media/analyzers/png.ex +++ b/lib/philomena_media/analyzers/png.ex @@ -3,6 +3,7 @@ defmodule PhilomenaMedia.Analyzers.Png do alias PhilomenaMedia.Analyzers.Analyzer alias PhilomenaMedia.Analyzers.Result + alias PhilomenaMedia.Remote @behaviour Analyzer @@ -20,7 +21,7 @@ defmodule PhilomenaMedia.Analyzers.Png do end defp stats(file) do - case System.cmd("mediastat", [file]) do + case Remote.cmd("mediastat", [file]) do {output, 0} -> [_size, frames, width, height, num, den] = output diff --git a/lib/philomena_media/analyzers/svg.ex b/lib/philomena_media/analyzers/svg.ex index f83a55f0..0b55a568 100644 --- a/lib/philomena_media/analyzers/svg.ex +++ b/lib/philomena_media/analyzers/svg.ex @@ -3,6 +3,7 @@ defmodule PhilomenaMedia.Analyzers.Svg do alias PhilomenaMedia.Analyzers.Analyzer alias PhilomenaMedia.Analyzers.Result + alias PhilomenaMedia.Remote @behaviour Analyzer @@ -20,7 +21,7 @@ defmodule PhilomenaMedia.Analyzers.Svg do end defp stats(file) do - case System.cmd("svgstat", [file]) do + case Remote.cmd("svgstat", [file]) do {output, 0} -> [_size, _frames, width, height, _num, _den] = output diff --git a/lib/philomena_media/analyzers/webm.ex b/lib/philomena_media/analyzers/webm.ex index b215e01e..fb785923 100644 --- a/lib/philomena_media/analyzers/webm.ex +++ b/lib/philomena_media/analyzers/webm.ex @@ -3,6 +3,7 @@ defmodule PhilomenaMedia.Analyzers.Webm do alias PhilomenaMedia.Analyzers.Analyzer alias PhilomenaMedia.Analyzers.Result + alias PhilomenaMedia.Remote @behaviour Analyzer @@ -20,7 +21,7 @@ defmodule PhilomenaMedia.Analyzers.Webm do end defp stats(file) do - case System.cmd("mediastat", [file]) do + case Remote.cmd("mediastat", [file]) do {output, 0} -> [_size, frames, width, height, num, den] = output diff --git a/lib/philomena_media/gif_preview.ex b/lib/philomena_media/gif_preview.ex index ac40044b..beef1e78 100644 --- a/lib/philomena_media/gif_preview.ex +++ b/lib/philomena_media/gif_preview.ex @@ -3,6 +3,8 @@ defmodule PhilomenaMedia.GifPreview do GIF preview generation for video files. """ + alias PhilomenaMedia.Remote + @type duration :: float() @type dimensions :: {pos_integer(), pos_integer()} @@ -49,7 +51,7 @@ defmodule PhilomenaMedia.GifPreview do end) {_output, 0} = - System.cmd( + Remote.cmd( "ffmpeg", commands(decoder, video, gif, clamp(duration), dimensions, num_images, target_framerate) ) diff --git a/lib/philomena_media/intensities.ex b/lib/philomena_media/intensities.ex index ea095295..f2df677b 100644 --- a/lib/philomena_media/intensities.ex +++ b/lib/philomena_media/intensities.ex @@ -17,6 +17,8 @@ defmodule PhilomenaMedia.Intensities do of image dimensions, with poor precision and a poor-to-fair accuracy. """ + alias PhilomenaMedia.Remote + @type t :: %__MODULE__{ nw: float(), ne: float(), @@ -50,7 +52,7 @@ defmodule PhilomenaMedia.Intensities do """ @spec file(Path.t()) :: {:ok, t()} | :error def file(input) do - System.cmd("image-intensities", [input]) + Remote.cmd("image-intensities", [input]) |> case do {output, 0} -> [nw, ne, sw, se] = diff --git a/lib/philomena_media/mime.ex b/lib/philomena_media/mime.ex index 1b29aa75..3bd48305 100644 --- a/lib/philomena_media/mime.ex +++ b/lib/philomena_media/mime.ex @@ -27,7 +27,7 @@ defmodule PhilomenaMedia.Mime do """ @spec file(Path.t()) :: {:ok, t()} | {:unsupported_mime, t()} | :error def file(path) do - System.cmd("file", ["-b", "--mime-type", path]) + PhilomenaMedia.Remote.cmd("file", ["-b", "--mime-type", path]) |> case do {output, 0} -> true_mime(String.trim(output)) diff --git a/lib/philomena_media/processors/gif.ex b/lib/philomena_media/processors/gif.ex index 6e185f9f..49756719 100644 --- a/lib/philomena_media/processors/gif.ex +++ b/lib/philomena_media/processors/gif.ex @@ -3,6 +3,7 @@ defmodule PhilomenaMedia.Processors.Gif do alias PhilomenaMedia.Intensities alias PhilomenaMedia.Analyzers.Result + alias PhilomenaMedia.Remote alias PhilomenaMedia.Processors.Processor alias PhilomenaMedia.Processors @@ -46,7 +47,7 @@ defmodule PhilomenaMedia.Processors.Gif do defp optimize(file) do optimized = Briefly.create!(extname: ".gif") - {_output, 0} = System.cmd("gifsicle", ["--careful", "-O2", file, "-o", optimized]) + {_output, 0} = Remote.cmd("gifsicle", ["--careful", "-O2", file, "-o", optimized]) optimized end @@ -54,7 +55,7 @@ defmodule PhilomenaMedia.Processors.Gif do defp preview(duration, file) do preview = Briefly.create!(extname: ".png") - {_output, 0} = System.cmd("mediathumb", [file, to_string(duration / 2), preview]) + {_output, 0} = Remote.cmd("mediathumb", [file, to_string(duration / 2), preview]) preview end @@ -63,7 +64,7 @@ defmodule PhilomenaMedia.Processors.Gif do palette = Briefly.create!(extname: ".png") {_output, 0} = - System.cmd("ffmpeg", [ + Remote.cmd("ffmpeg", [ "-loglevel", "0", "-y", @@ -88,7 +89,7 @@ defmodule PhilomenaMedia.Processors.Gif do filter_graph = "[0:v]#{scale_filter}[x];[x][1:v]#{palette_filter}" {_output, 0} = - System.cmd("ffmpeg", [ + Remote.cmd("ffmpeg", [ "-loglevel", "0", "-y", @@ -109,7 +110,7 @@ defmodule PhilomenaMedia.Processors.Gif do mp4 = Briefly.create!(extname: ".mp4") {_output, 0} = - System.cmd("ffmpeg", [ + Remote.cmd("ffmpeg", [ "-loglevel", "0", "-y", @@ -127,7 +128,7 @@ defmodule PhilomenaMedia.Processors.Gif do ]) {_output, 0} = - System.cmd("ffmpeg", [ + Remote.cmd("ffmpeg", [ "-loglevel", "0", "-y", diff --git a/lib/philomena_media/processors/jpeg.ex b/lib/philomena_media/processors/jpeg.ex index 60444257..7cbec03d 100644 --- a/lib/philomena_media/processors/jpeg.ex +++ b/lib/philomena_media/processors/jpeg.ex @@ -3,6 +3,7 @@ defmodule PhilomenaMedia.Processors.Jpeg do alias PhilomenaMedia.Intensities alias PhilomenaMedia.Analyzers.Result + alias PhilomenaMedia.Remote alias PhilomenaMedia.Processors.Processor alias PhilomenaMedia.Processors @@ -42,7 +43,7 @@ defmodule PhilomenaMedia.Processors.Jpeg do defp requires_lossy_transformation?(file) do with {output, 0} <- - System.cmd("identify", ["-format", "%[orientation]\t%[profile:icc]", file]), + Remote.cmd("identify", ["-format", "%[orientation]\t%[profile:icc]", file]), [orientation, profile] <- String.split(output, "\t") do orientation not in ["Undefined", "TopLeft"] or profile != "" else @@ -60,7 +61,7 @@ defmodule PhilomenaMedia.Processors.Jpeg do true -> # Transcode: strip EXIF, embedded profile and reorient image {_output, 0} = - System.cmd("convert", [ + Remote.cmd("convert", [ file, "-profile", srgb_profile(), @@ -71,7 +72,7 @@ defmodule PhilomenaMedia.Processors.Jpeg do _ -> # Transmux only: Strip EXIF without touching orientation - validate_return(System.cmd("jpegtran", ["-copy", "none", "-outfile", stripped, file])) + validate_return(Remote.cmd("jpegtran", ["-copy", "none", "-outfile", stripped, file])) end stripped @@ -80,7 +81,7 @@ defmodule PhilomenaMedia.Processors.Jpeg do defp optimize(file) do optimized = Briefly.create!(extname: ".jpg") - validate_return(System.cmd("jpegtran", ["-optimize", "-outfile", optimized, file])) + validate_return(Remote.cmd("jpegtran", ["-optimize", "-outfile", optimized, file])) optimized end @@ -90,7 +91,7 @@ defmodule PhilomenaMedia.Processors.Jpeg do scale_filter = "scale=w=#{width}:h=#{height}:force_original_aspect_ratio=decrease" {_output, 0} = - System.cmd("ffmpeg", [ + Remote.cmd("ffmpeg", [ "-loglevel", "0", "-y", @@ -103,7 +104,7 @@ defmodule PhilomenaMedia.Processors.Jpeg do scaled ]) - {_output, 0} = System.cmd("jpegtran", ["-optimize", "-outfile", scaled, scaled]) + {_output, 0} = Remote.cmd("jpegtran", ["-optimize", "-outfile", scaled, scaled]) [{:copy, scaled, "#{thumb_name}.jpg"}] end diff --git a/lib/philomena_media/processors/png.ex b/lib/philomena_media/processors/png.ex index 0fc4c50d..27c71c14 100644 --- a/lib/philomena_media/processors/png.ex +++ b/lib/philomena_media/processors/png.ex @@ -3,6 +3,7 @@ defmodule PhilomenaMedia.Processors.Png do alias PhilomenaMedia.Intensities alias PhilomenaMedia.Analyzers.Result + alias PhilomenaMedia.Remote alias PhilomenaMedia.Processors.Processor alias PhilomenaMedia.Processors @@ -49,7 +50,7 @@ defmodule PhilomenaMedia.Processors.Png do optimized = Briefly.create!(extname: ".png") {_output, 0} = - System.cmd("optipng", ["-fix", "-i0", "-o2", "-quiet", "-clobber", file, "-out", optimized]) + Remote.cmd("optipng", ["-fix", "-i0", "-o2", "-quiet", "-clobber", file, "-out", optimized]) # Remove useless .bak file File.rm(optimized <> ".bak") @@ -66,7 +67,7 @@ defmodule PhilomenaMedia.Processors.Png do {_output, 0} = cond do animated? -> - System.cmd("ffmpeg", [ + Remote.cmd("ffmpeg", [ "-loglevel", "0", "-y", @@ -82,10 +83,10 @@ defmodule PhilomenaMedia.Processors.Png do ]) true -> - System.cmd("ffmpeg", ["-loglevel", "0", "-y", "-i", file, "-vf", scale_filter, scaled]) + Remote.cmd("ffmpeg", ["-loglevel", "0", "-y", "-i", file, "-vf", scale_filter, scaled]) end - System.cmd("optipng", ["-i0", "-o1", "-quiet", "-clobber", scaled]) + Remote.cmd("optipng", ["-i0", "-o1", "-quiet", "-clobber", scaled]) [{:copy, scaled, "#{thumb_name}.png"}] end diff --git a/lib/philomena_media/processors/svg.ex b/lib/philomena_media/processors/svg.ex index aaa3dd5c..0f9b6e6c 100644 --- a/lib/philomena_media/processors/svg.ex +++ b/lib/philomena_media/processors/svg.ex @@ -3,6 +3,7 @@ defmodule PhilomenaMedia.Processors.Svg do alias PhilomenaMedia.Intensities alias PhilomenaMedia.Analyzers.Result + alias PhilomenaMedia.Remote alias PhilomenaMedia.Processors.Processor alias PhilomenaMedia.Processors @@ -42,7 +43,7 @@ defmodule PhilomenaMedia.Processors.Svg do defp preview(file) do preview = Briefly.create!(extname: ".png") - {_output, 0} = System.cmd("safe-rsvg-convert", [file, preview]) + {_output, 0} = Remote.cmd("safe-rsvg-convert", [file, preview]) preview end @@ -52,9 +53,9 @@ defmodule PhilomenaMedia.Processors.Svg do scale_filter = "scale=w=#{width}:h=#{height}:force_original_aspect_ratio=decrease" {_output, 0} = - System.cmd("ffmpeg", ["-loglevel", "0", "-y", "-i", preview, "-vf", scale_filter, scaled]) + Remote.cmd("ffmpeg", ["-loglevel", "0", "-y", "-i", preview, "-vf", scale_filter, scaled]) - {_output, 0} = System.cmd("optipng", ["-i0", "-o1", "-quiet", "-clobber", scaled]) + {_output, 0} = Remote.cmd("optipng", ["-i0", "-o1", "-quiet", "-clobber", scaled]) [{:copy, scaled, "#{thumb_name}.png"}] end diff --git a/lib/philomena_media/processors/webm.ex b/lib/philomena_media/processors/webm.ex index 0fcc1129..852d5492 100644 --- a/lib/philomena_media/processors/webm.ex +++ b/lib/philomena_media/processors/webm.ex @@ -3,6 +3,7 @@ defmodule PhilomenaMedia.Processors.Webm do alias PhilomenaMedia.Intensities alias PhilomenaMedia.Analyzers.Result + alias PhilomenaMedia.Remote alias PhilomenaMedia.GifPreview alias PhilomenaMedia.Processors.Processor alias PhilomenaMedia.Processors @@ -56,7 +57,7 @@ defmodule PhilomenaMedia.Processors.Webm do defp preview(duration, file) do preview = Briefly.create!(extname: ".png") - {_output, 0} = System.cmd("mediathumb", [file, to_string(duration / 2), preview]) + {_output, 0} = Remote.cmd("mediathumb", [file, to_string(duration / 2), preview]) preview end @@ -65,7 +66,7 @@ defmodule PhilomenaMedia.Processors.Webm do stripped = Briefly.create!(extname: ".webm") {_output, 0} = - System.cmd("ffmpeg", [ + Remote.cmd("ffmpeg", [ "-loglevel", "0", "-y", @@ -110,7 +111,7 @@ defmodule PhilomenaMedia.Processors.Webm do mp4 = Briefly.create!(extname: ".mp4") {_output, 0} = - System.cmd("ffmpeg", [ + Remote.cmd("ffmpeg", [ "-loglevel", "0", "-y", @@ -170,7 +171,7 @@ defmodule PhilomenaMedia.Processors.Webm do mp4 = Briefly.create!(extname: ".mp4") {_output, 0} = - System.cmd("ffmpeg", [ + Remote.cmd("ffmpeg", [ "-loglevel", "0", "-y", @@ -213,7 +214,7 @@ defmodule PhilomenaMedia.Processors.Webm do defp select_decoder(file) do {output, 0} = - System.cmd("ffprobe", [ + Remote.cmd("ffprobe", [ "-loglevel", "0", "-select_streams", diff --git a/lib/philomena_media/remote.ex b/lib/philomena_media/remote.ex new file mode 100644 index 00000000..518a8613 --- /dev/null +++ b/lib/philomena_media/remote.ex @@ -0,0 +1,18 @@ +defmodule PhilomenaMedia.Remote do + @doc """ + Out-of-process replacement for `System.cmd/2` that calls the requested + command elsewhere, translating file accesses, and returns the result. + """ + def cmd(command, args) do + :ok = Philomena.Native.async_process_command(mediaproc_addr(), command, args) + + receive do + {:command_reply, command_reply} -> + {command_reply.stdout, command_reply.status} + end + end + + defp mediaproc_addr do + Application.get_env(:philomena, :mediaproc_addr) + end +end diff --git a/mix.lock b/mix.lock index 288b5a24..969af3bd 100644 --- a/mix.lock +++ b/mix.lock @@ -63,7 +63,7 @@ "redix": {:hex, :redix, "1.5.1", "a2386971e69bf23630fb3a215a831b5478d2ee7dc9ea7ac811ed89186ab5d7b7", [:mix], [{:castore, "~> 0.1.0 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:nimble_options, "~> 0.5.0 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "85224eb2b683c516b80d472eb89b76067d5866913bf0be59d646f550de71f5c4"}, "remote_ip": {:hex, :remote_ip, "1.2.0", "fb078e12a44414f4cef5a75963c33008fe169b806572ccd17257c208a7bc760f", [:mix], [{:combine, "~> 0.10", [hex: :combine, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "2ff91de19c48149ce19ed230a81d377186e4412552a597d6a5137373e5877cb7"}, "req": {:hex, :req, "0.5.8", "50d8d65279d6e343a5e46980ac2a70e97136182950833a1968b371e753f6a662", [:mix], [{:brotli, "~> 0.3.1", [hex: :brotli, repo: "hexpm", optional: true]}, {:ezstd, "~> 1.0", [hex: :ezstd, repo: "hexpm", optional: true]}, {:finch, "~> 0.17", [hex: :finch, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mime, "~> 2.0.6 or ~> 2.1", [hex: :mime, repo: "hexpm", optional: false]}, {:nimble_csv, "~> 1.0", [hex: :nimble_csv, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "d7fc5898a566477e174f26887821a3c5082b243885520ee4b45555f5d53f40ef"}, - "rustler": {:hex, :rustler, "0.35.0", "1e2e379e1150fab9982454973c74ac9899bd0377b3882166ee04127ea613b2d9", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:req, "~> 0.5", [hex: :req, repo: "hexpm", optional: false]}, {:toml, "~> 0.6", [hex: :toml, repo: "hexpm", optional: false]}], "hexpm", "a176bea1bb6711474f9dfad282066f2b7392e246459bf4e29dfff6d828779fdf"}, + "rustler": {:hex, :rustler, "0.35.1", "ec81961ef9ee833d721dafb4449cab29b16b969a3063a842bb9e3ea912f6b938", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:req, "~> 0.5", [hex: :req, repo: "hexpm", optional: false]}, {:toml, "~> 0.6", [hex: :toml, repo: "hexpm", optional: false]}], "hexpm", "3713b2e70e68ec2bfa8291dfd9cb811fe64a770f254cd9c331f8b34fa7989115"}, "scrivener": {:hex, :scrivener, "2.7.2", "1d913c965ec352650a7f864ad7fd8d80462f76a32f33d57d1e48bc5e9d40aba2", [:mix], [], "hexpm", "7866a0ec4d40274efbee1db8bead13a995ea4926ecd8203345af8f90d2b620d9"}, "scrivener_ecto": {:git, "https://github.com/krns/scrivener_ecto.git", "eaad1ddd86a9c8ffa422479417221265a0673777", [ref: "eaad1ddd86a9c8ffa422479417221265a0673777"]}, "secure_compare": {:hex, :secure_compare, "0.1.0", "01b3c93c8edb696e8a5b38397ed48e10958c8a5ec740606656445bcbec0aadb8", [:mix], [], "hexpm", "6391a49eb4a6182f0d7425842fc774bbed715e78b2bfb0c83b99c94e02c78b5c"}, diff --git a/native/philomena/Cargo.lock b/native/philomena/Cargo.lock index 72be95b4..9b7048c9 100644 --- a/native/philomena/Cargo.lock +++ b/native/philomena/Cargo.lock @@ -2,6 +2,15 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "addr2line" +version = "0.24.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfbe277e56a376000877090da837660b4427aad530e3028d44e0bffe4f89a1c1" +dependencies = [ + "gimli", +] + [[package]] name = "adler2" version = "2.0.0" @@ -17,6 +26,61 @@ dependencies = [ "memchr", ] +[[package]] +name = "anstream" +version = "0.6.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" + +[[package]] +name = "anstyle-parse" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" +dependencies = [ + "windows-sys 0.59.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2109dbce0e72be3ec00bed26e6a7479ca384ad226efdd66db8fa2e3a38c83125" +dependencies = [ + "anstyle", + "windows-sys 0.59.0", +] + +[[package]] +name = "anyhow" +version = "1.0.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34ac096ce696dc2fcabef30516bb13c0a68a11d30131d3df6f04711467681b04" + [[package]] name = "arbitrary" version = "1.4.1" @@ -26,6 +90,38 @@ dependencies = [ "derive_arbitrary", ] +[[package]] +name = "async-trait" +version = "0.1.85" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f934833b4b7233644e5848f235df3f57ed8c80f1528a26c3dfa13d2147fa056" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "autocfg" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + +[[package]] +name = "backtrace" +version = "0.3.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d82cb332cdfaed17ae235a638438ac4d4839913cc2af585c3c6746e8f8bee1a" +dependencies = [ + "addr2line", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", + "windows-targets", +] + [[package]] name = "base64" version = "0.21.7" @@ -33,10 +129,25 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" [[package]] -name = "bon" -version = "3.3.0" +name = "bincode" +version = "1.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f265cdb2e8501f1c952749e78babe8f1937be92c98120e5f78fc72d634682bad" +checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" +dependencies = [ + "serde", +] + +[[package]] +name = "bitflags" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" + +[[package]] +name = "bon" +version = "3.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe7acc34ff59877422326db7d6f2d845a582b16396b6b08194942bf34c6528ab" dependencies = [ "bon-macros", "rustversion", @@ -44,9 +155,9 @@ dependencies = [ [[package]] name = "bon-macros" -version = "3.3.0" +version = "3.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38aa5c627cd7706490e5b003d685f8b9d69bc343b1a00b9fdd01e75fdf6827cf" +checksum = "4159dd617a7fbc9be6a692fe69dc2954f8e6bb6bb5e4d7578467441390d77fd0" dependencies = [ "darling", "ident_case", @@ -63,6 +174,12 @@ version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + [[package]] name = "bytes" version = "1.9.0" @@ -71,19 +188,18 @@ checksum = "325918d6fe32f23b19878fe4b34794ae41fc19ddbe53b10571a4874d44ffd39b" [[package]] name = "caseless" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "808dab3318747be122cb31d36de18d4d1c81277a76f8332a02b81a3d73463d7f" +checksum = "8b6fd507454086c8edfd769ca6ada439193cdb209c7681712ef6275cccbfe5d8" dependencies = [ - "regex", "unicode-normalization", ] [[package]] name = "cc" -version = "1.2.3" +version = "1.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "27f657647bcff5394bf56c7317665bbf790a137a50eaaa5c6bfbb9e27a518f2d" +checksum = "a012a0df96dd6d06ba9a1b29d6402d1a5d77c6befd2566afdc26e10603dc93d7" dependencies = [ "shlex", ] @@ -94,6 +210,52 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "4.5.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9560b07a799281c7e0958b9296854d6fafd4c5f31444a7e5bb1ad6dde5ccf1bd" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "874e0dd3eb68bf99058751ac9712f622e61e6f393a94f7128fa26e3f02f5c7cd" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54b755194d6389280185988721fffba69495eed5ee9feeee9a599b53db80318c" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" + +[[package]] +name = "colorchoice" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" + [[package]] name = "comrak" version = "0.29.0" @@ -121,9 +283,9 @@ dependencies = [ [[package]] name = "crossbeam-utils" -version = "0.8.20" +version = "0.8.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" +checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" [[package]] name = "darling" @@ -188,18 +350,89 @@ dependencies = [ "syn", ] +[[package]] +name = "educe" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4bd92664bf78c4d3dba9b7cdafce6fa15b13ed3ed16175218196942e99168a8" +dependencies = [ + "enum-ordinalize", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "entities" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5320ae4c3782150d900b79807611a59a99fc9a1d61d686faafc24b93fc8d7ca" +[[package]] +name = "enum-ordinalize" +version = "4.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fea0dcfa4e54eeb516fe454635a95753ddd39acda650ce703031c6973e315dd5" +dependencies = [ + "enum-ordinalize-derive", +] + +[[package]] +name = "enum-ordinalize-derive" +version = "4.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d28318a75d4aead5c4db25382e8ef717932d0346600cacae6357eb5941bc5ff" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "env_filter" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "186e05a59d4c50738528153b83b0b0194d3a29507dfec16eccd4b342903397d0" +dependencies = [ + "log", + "regex", +] + +[[package]] +name = "env_logger" +version = "0.11.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcaee3d8e3cfc3fd92428d477bc97fc29ec8716d180c0d74c643bb26166660e0" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "humantime", + "log", +] + [[package]] name = "equivalent" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" +[[package]] +name = "errno" +version = "0.3.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d" +dependencies = [ + "libc", + "windows-sys 0.59.0", +] + +[[package]] +name = "fastrand" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" + [[package]] name = "flate2" version = "1.0.35" @@ -225,6 +458,112 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "futures" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" + +[[package]] +name = "futures-executor" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6" + +[[package]] +name = "futures-macro" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "futures-sink" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e575fab7d1e0dcb8d0c7bcf9a63ee213816ab51902e6d244a95819acacf1d4f7" + +[[package]] +name = "futures-task" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988" + +[[package]] +name = "futures-util" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "pin-utils", + "slab", +] + +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "gimli" +version = "0.31.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" + [[package]] name = "hashbrown" version = "0.15.2" @@ -248,6 +587,12 @@ dependencies = [ "itoa", ] +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "icu_collections" version = "1.5.0" @@ -405,9 +750,18 @@ dependencies = [ [[package]] name = "inventory" -version = "0.3.15" +version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f958d3d68f4167080a18141e10381e7634563984a537f2a49a30fd8e53ac5767" +checksum = "3b31349d02fe60f80bbbab1a9402364cad7460626d6030494b08ac4a2075bf81" +dependencies = [ + "rustversion", +] + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" [[package]] name = "itoa" @@ -446,10 +800,16 @@ dependencies = [ ] [[package]] -name = "libc" -version = "0.2.168" +name = "lazy_static" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5aaeb2981e0606ca11d79718f8bb01164f1d6ed75080182d3abf017e6d244b6d" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "libc" +version = "0.2.169" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" [[package]] name = "libloading" @@ -461,12 +821,28 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "linux-raw-sys" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab" + [[package]] name = "litemap" version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ee93343901ab17bd981295f2cf0026d4ad018c7c31ba84549a4ddbb47a45104" +[[package]] +name = "lock_api" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" +dependencies = [ + "autocfg", + "scopeguard", +] + [[package]] name = "lockfree-object-pool" version = "0.1.6" @@ -479,6 +855,39 @@ version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" +[[package]] +name = "mediaproc" +version = "0.1.0" +dependencies = [ + "once_cell", + "serde", + "tarpc", + "tokio", +] + +[[package]] +name = "mediaproc_client" +version = "0.1.0" +dependencies = [ + "clap", + "mediaproc", + "tokio", +] + +[[package]] +name = "mediaproc_server" +version = "0.1.0" +dependencies = [ + "clap", + "env_logger", + "futures", + "mediaproc", + "tarpc", + "tempfile", + "tokio", + "tracing", +] + [[package]] name = "memchr" version = "2.7.4" @@ -487,19 +896,99 @@ checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" [[package]] name = "miniz_oxide" -version = "0.8.0" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1" +checksum = "4ffbe83022cedc1d264172192511ae958937694cd57ce297164951b8b3568394" dependencies = [ "adler2", ] +[[package]] +name = "mio" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd" +dependencies = [ + "libc", + "wasi", + "windows-sys 0.52.0", +] + +[[package]] +name = "object" +version = "0.36.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62948e14d923ea95ea2c7c86c71013138b66525b86bdc08d2dcc262bdb497b87" +dependencies = [ + "memchr", +] + [[package]] name = "once_cell" version = "1.20.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" +[[package]] +name = "opentelemetry" +version = "0.26.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "570074cc999d1a58184080966e5bd3bf3a9a4af650c3b05047c2621e7405cd17" +dependencies = [ + "futures-core", + "futures-sink", + "js-sys", + "once_cell", + "pin-project-lite", + "thiserror 1.0.69", +] + +[[package]] +name = "opentelemetry-semantic-conventions" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cefe0543875379e47eb5f1e68ff83f45cc41366a92dfd0d073d513bf68e9a05" + +[[package]] +name = "opentelemetry_sdk" +version = "0.26.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2c627d9f4c9cdc1f21a29ee4bfbd6028fcb8bcf2a857b43f3abdf72c9c862f3" +dependencies = [ + "async-trait", + "futures-channel", + "futures-executor", + "futures-util", + "once_cell", + "opentelemetry", + "percent-encoding", + "rand", + "thiserror 1.0.69", +] + +[[package]] +name = "parking_lot" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets", +] + [[package]] name = "percent-encoding" version = "2.3.1" @@ -514,19 +1003,62 @@ dependencies = [ "comrak", "http", "jemallocator", + "mediaproc", "once_cell", "regex", "ring", "rustler", + "tokio", "url", "zip", ] [[package]] -name = "prettyplease" -version = "0.2.25" +name = "pin-project" +version = "1.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64d1ec885c64d0457d564db4ec299b2dae3f9c02808b8ad9c3a089c591b18033" +checksum = "1e2ec53ad785f4d35dac0adea7f7dc6f1bb277ad84a680c7afefeae05d1f5916" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d56a66c0c55993aa927429d0f8a0abfd74f084e4d9c192cffed01e418d83eefb" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "ppv-lite86" +version = "0.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "prettyplease" +version = "0.2.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "483f8c21f64f3ea09fe0f30f5d48c3e8eefe5dac9129f0075f76593b4c1da705" dependencies = [ "proc-macro2", "syn", @@ -543,13 +1075,52 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.37" +version = "1.0.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc" dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "redox_syscall" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03a862b389f93e68874fbf580b9de08dd02facb9a788ebadaf4a3fd33cf58834" +dependencies = [ + "bitflags", +] + [[package]] name = "regex" version = "1.11.1" @@ -601,10 +1172,29 @@ dependencies = [ ] [[package]] -name = "rustler" -version = "0.35.0" +name = "rustc-demangle" +version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b705f2c3643cc170d8888cb6bad589155d9c0248f3104ef7a04c2b7ffbaf13fc" +checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" + +[[package]] +name = "rustix" +version = "0.38.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a78891ee6bf2340288408954ac787aa063d8e8817e9f53abb37c695c6d834ef6" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.59.0", +] + +[[package]] +name = "rustler" +version = "0.35.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a9f6bb374bf0a1431cff92ee6a89e39b9978fa4dbccc4137605be4ed5118779" dependencies = [ "inventory", "libloading", @@ -614,9 +1204,9 @@ dependencies = [ [[package]] name = "rustler_codegen" -version = "0.35.0" +version = "0.35.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ad56caff00562948bd6ac33c18dbc579e5a1bbee2d7f2f54073307e57f6b57a" +checksum = "f7914359a19fff34b2f0e9d4b7d4b02ca5ee597e52804c7472802b25e1ec543e" dependencies = [ "heck", "inventory", @@ -627,42 +1217,93 @@ dependencies = [ [[package]] name = "rustversion" +version = "1.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7c45b9784283f1b2e7fb61b42047c2fd678ef0960d4f6f1eba131594cc369d4" + +[[package]] +name = "ryu" version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e819f2bc632f285be6d7cd36e25940d45b2391dd6d9b939e79de557f7014248" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "serde" -version = "1.0.215" +version = "1.0.217" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6513c1ad0b11a9376da888e3e0baa0077f1aed55c17f50e7b2397136129fb88f" +checksum = "02fc4265df13d6fa1d00ecff087228cc0a2b5f3c0e87e258d8b94a156e984c70" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.215" +version = "1.0.217" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0" +checksum = "5a9bf7cf98d04a2b28aead066b7496853d4779c9cc183c440dbac457641e19a0" dependencies = [ "proc-macro2", "quote", "syn", ] +[[package]] +name = "serde_json" +version = "1.0.135" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b0d7ba2887406110130a978386c4e1befb98c674b4fba677954e4db976630d9" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", +] + +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + [[package]] name = "shlex" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +[[package]] +name = "signal-hook-registry" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1" +dependencies = [ + "libc", +] + [[package]] name = "simd-adler32" version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d66dc143e6b11c1eddc06d5c423cfc97062865baf299914ab64caa38182078fe" +[[package]] +name = "slab" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" +dependencies = [ + "autocfg", +] + [[package]] name = "slug" version = "0.1.6" @@ -679,6 +1320,16 @@ version = "1.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" +[[package]] +name = "socket2" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c970269d99b64e60ec3bd6ad27270092a5394c4e309314b18ae3fe575695fbe8" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + [[package]] name = "spin" version = "0.5.2" @@ -691,6 +1342,12 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + [[package]] name = "strsim" version = "0.11.1" @@ -699,9 +1356,9 @@ checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] name = "syn" -version = "2.0.90" +version = "2.0.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31" +checksum = "46f71c0377baf4ef1cc3e3402ded576dccc315800fbc62dfc7fe04b009773b4a" dependencies = [ "proc-macro2", "quote", @@ -720,25 +1377,105 @@ dependencies = [ ] [[package]] -name = "thiserror" -version = "2.0.6" +name = "tarpc" +version = "0.35.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8fec2a1820ebd077e2b90c4df007bebf344cd394098a13c563957d0afc83ea47" +checksum = "14d1be17be018ebeec4c489449adb5ef227746775974c311ce79e09886ef83c7" dependencies = [ - "thiserror-impl", + "anyhow", + "fnv", + "futures", + "humantime", + "opentelemetry", + "opentelemetry-semantic-conventions", + "pin-project", + "rand", + "serde", + "static_assertions", + "tarpc-plugins", + "thiserror 1.0.69", + "tokio", + "tokio-serde", + "tokio-util", + "tracing", + "tracing-opentelemetry", ] [[package]] -name = "thiserror-impl" -version = "2.0.6" +name = "tarpc-plugins" +version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d65750cab40f4ff1929fb1ba509e9914eb756131cef4210da8d5d700d26f6312" +checksum = "e0e3d9676af494694e11a3e367a4bfa7f6d1d5566bd0fe9aceb4aa9281122ab8" dependencies = [ "proc-macro2", "quote", "syn", ] +[[package]] +name = "tempfile" +version = "3.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8a559c81686f576e8cd0290cd2a24a2a9ad80c98b3478856500fcbd7acd704" +dependencies = [ + "cfg-if", + "fastrand", + "getrandom", + "once_cell", + "rustix", + "windows-sys 0.59.0", +] + +[[package]] +name = "thiserror" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" +dependencies = [ + "thiserror-impl 1.0.69", +] + +[[package]] +name = "thiserror" +version = "2.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f072643fd0190df67a8bab670c20ef5d8737177d6ac6b2e9a236cb096206b2cc" +dependencies = [ + "thiserror-impl 2.0.9", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b50fa271071aae2e6ee85f842e2e28ba8cd2c5fb67f11fcb1fd70b276f9e7d4" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "thread_local" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" +dependencies = [ + "cfg-if", + "once_cell", +] + [[package]] name = "tinystr" version = "0.7.6" @@ -751,9 +1488,9 @@ dependencies = [ [[package]] name = "tinyvec" -version = "1.8.0" +version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "445e881f4f6d382d5f27c034e25eb92edd7c784ceab92a0937db7f2e9471b938" +checksum = "022db8904dfa342efe721985167e9fcd16c29b226db4397ed752a761cfce81e8" dependencies = [ "tinyvec_macros", ] @@ -764,6 +1501,125 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" +[[package]] +name = "tokio" +version = "1.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5cec9b21b0450273377fc97bd4c33a8acffc8c996c987a7c5b319a0083707551" +dependencies = [ + "backtrace", + "bytes", + "libc", + "mio", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", + "windows-sys 0.52.0", +] + +[[package]] +name = "tokio-macros" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tokio-serde" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "caf600e7036b17782571dd44fa0a5cea3c82f60db5137f774a325a76a0d6852b" +dependencies = [ + "bincode", + "bytes", + "educe", + "futures-core", + "futures-sink", + "pin-project", + "serde", + "serde_json", +] + +[[package]] +name = "tokio-util" +version = "0.7.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7fcaa8d55a2bdd6b83ace262b016eca0d79ee02818c5c1bcdf0305114081078" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "slab", + "tokio", +] + +[[package]] +name = "tracing" +version = "0.1.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" +dependencies = [ + "log", + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "395ae124c09f9e6918a2310af6038fba074bcf474ac352496d5910dd59a2226d" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-opentelemetry" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc58af5d3f6c5811462cabb3289aec0093f7338e367e5a33d28c0433b3c7360b" +dependencies = [ + "js-sys", + "once_cell", + "opentelemetry", + "opentelemetry_sdk", + "tracing", + "tracing-core", + "tracing-subscriber", + "web-time", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8189decb5ac0fa7bc8b96b7cb9b2701d60d48805aca84a238004d665fcc4008" +dependencies = [ + "sharded-slab", + "thread_local", + "tracing-core", +] + [[package]] name = "typed-arena" version = "2.0.2" @@ -820,6 +1676,24 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "wasm-bindgen" version = "0.2.99" @@ -884,6 +1758,16 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "web-time" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "winapi" version = "0.3.9" @@ -906,6 +1790,24 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-targets" version = "0.52.6" @@ -1006,6 +1908,27 @@ dependencies = [ "synstructure", ] +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "byteorder", + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "zerofrom" version = "0.1.5" @@ -1051,9 +1974,9 @@ dependencies = [ [[package]] name = "zip" -version = "2.2.1" +version = "2.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "99d52293fc86ea7cf13971b3bb81eb21683636e7ae24c729cdaf1b7c4157a352" +checksum = "ae9c1ea7b3a5e1f4b922ff856a129881167511563dc219869afe3787fc0c1a45" dependencies = [ "arbitrary", "crc32fast", @@ -1062,7 +1985,7 @@ dependencies = [ "flate2", "indexmap", "memchr", - "thiserror", + "thiserror 2.0.9", "zopfli", ] diff --git a/native/philomena/Cargo.toml b/native/philomena/Cargo.toml index 0e42330e..3a9fec3f 100644 --- a/native/philomena/Cargo.toml +++ b/native/philomena/Cargo.toml @@ -9,15 +9,25 @@ name = "philomena" path = "src/lib.rs" crate-type = ["dylib"] +[workspace] +members = [ + "mediaproc", + "mediaproc_client", + "mediaproc_server", +] +default-members = ["mediaproc"] + [dependencies] base64 = "0.21" comrak = { git = "https://github.com/philomena-dev/comrak", branch = "philomena-0.29.2", default-features = false } http = "0.2" jemallocator = { version = "0.5.0", features = ["disable_initial_exec_tls"] } +mediaproc = { path = "./mediaproc" } once_cell = "1.20" regex = "1" ring = "0.16" rustler = "0.35" +tokio = { version = "1.0", features = ["full"] } url = "2.5" zip = { version = "2.2.0", features = ["deflate"], default-features = false } diff --git a/native/philomena/mediaproc/Cargo.toml b/native/philomena/mediaproc/Cargo.toml new file mode 100644 index 00000000..82729464 --- /dev/null +++ b/native/philomena/mediaproc/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "mediaproc" +version = "0.1.0" +edition = "2021" + +[dependencies] +once_cell = "1.20" +serde = { version = "1.0", features = ["derive"] } +tarpc = { version = "0.35", features = ["full"] } +tokio = { version = "1.0", features = ["full"] } diff --git a/native/philomena/mediaproc/src/client.rs b/native/philomena/mediaproc/src/client.rs new file mode 100644 index 00000000..f76ef6d5 --- /dev/null +++ b/native/philomena/mediaproc/src/client.rs @@ -0,0 +1,146 @@ +use std::collections::{HashMap, HashSet}; +use std::ffi::OsString; +use std::path::Path; +use std::time::{Duration, Instant}; + +use crate::{CommandReply, ExecuteCommandError, FileMap, MediaProcessorClient}; +use once_cell::sync::Lazy; +use tarpc::context::Context; + +#[derive(Default)] +struct CallParameters { + /// Mapping from replaced name to original name. + replacements: HashMap, + /// List of post-processed arguments. + arguments: Vec, + /// Mapping of replaced name to file contents. + file_map: FileMap, +} + +/// List of file extensions which can be forwarded. +static FORWARDED_EXTS: Lazy> = Lazy::new(|| { + vec![ + "gif", "jpg", "jpeg", "png", "svg", "webm", "webp", "mp4", "icc", + ] + .into_iter() + .map(Into::into) + .collect() +}); + +fn forwarded_ext(path: &Path) -> Option<&str> { + match path.extension() { + Some(ext) if FORWARDED_EXTS.contains(ext) => ext.to_str(), + _ => None, + } +} + +fn create_replacements(arguments: impl Iterator) -> CallParameters { + use std::fs::read; + + // Maps original name to replaced name. + let mut processed = HashMap::::new(); + let mut counter: usize = 0; + + let mut output = CallParameters::default(); + + output.arguments = arguments + .map(|arg| { + let path = Path::new(&arg); + + // Avoid adding additional replacements if the same file is passed multiple times. + if let Some(replaced_name) = processed.get(&arg) { + return replaced_name.clone(); + } + + // Only try things that look like paths. + if !path.is_absolute() { + return arg; + } + + // Don't forward paths that don't exist or can't be read. + let Ok(contents) = read(path) else { + return arg; + }; + + // Only forward extension if extension is in allow list. + let replaced_name = match forwarded_ext(path) { + Some(ext) => format!("{}.{}", counter, ext), + None => format!("{}", counter), + }; + + counter = counter.saturating_add(1); + + processed.insert(arg.clone(), replaced_name.clone()); // original -> replaced + output.replacements.insert(replaced_name.clone(), arg); // replaced -> original + output.file_map.insert(replaced_name.clone(), contents); // replaced -> [contents] + + replaced_name + }) + .collect(); + + output +} + +fn update_replacements( + replacements: HashMap, + file_map: FileMap, +) -> Result<(), ExecuteCommandError> { + use std::fs::write; + + for (replaced_name, contents) in file_map { + let original_name = replacements + .get(&replaced_name) + .ok_or(ExecuteCommandError::InvalidFileMapName)?; + + write(original_name, contents).map_err(|_| ExecuteCommandError::LocalFilesystemError)?; + } + + Ok(()) +} + +fn context_with_1_hour_deadline() -> Context { + let mut context = Context::current(); + context.deadline = Instant::now() + Duration::from_secs(60 * 60); + context +} + +pub async fn execute_command( + client: &MediaProcessorClient, + program: String, + arguments: Vec, +) -> Result { + let call_params = create_replacements(arguments.into_iter()); + let (reply, file_map) = client + .execute_command( + context_with_1_hour_deadline(), + program, + call_params.arguments, + call_params.file_map, + ) + .await + .map_err(|_| ExecuteCommandError::UnknownError)??; + + update_replacements(call_params.replacements, file_map)?; + + Ok(reply) +} + +pub async fn connect_to_socket_server(server_addr: &str) -> Option { + let codec = tarpc::tokio_serde::formats::Bincode::default; + + for addr in tokio::net::lookup_host(server_addr).await.ok()? { + let mut transport = tarpc::serde_transport::tcp::connect(addr, codec); + transport.config_mut().max_frame_length(usize::MAX); + + let transport = match transport.await { + Ok(transport) => transport, + _ => continue, + }; + + return Some( + MediaProcessorClient::new(tarpc::client::Config::default(), transport).spawn(), + ); + } + + None +} diff --git a/native/philomena/mediaproc/src/lib.rs b/native/philomena/mediaproc/src/lib.rs new file mode 100644 index 00000000..5dedfdbf --- /dev/null +++ b/native/philomena/mediaproc/src/lib.rs @@ -0,0 +1,64 @@ +use once_cell::sync::Lazy; +use serde::{Deserialize, Serialize}; +use std::collections::{HashMap, HashSet}; + +pub mod client; + +#[tarpc::service] +pub trait MediaProcessor { + /// Executes a command on the media processor server. + async fn execute_command( + program: String, + arguments: Vec, + file_map: FileMap, + ) -> Result<(CommandReply, FileMap), ExecuteCommandError>; +} + +/// Errors which can occur during command execution. +#[derive(Debug, Deserialize, Serialize)] +pub enum ExecuteCommandError { + /// Requested program was not allowed to be executed. + UnpermittedProgram(String), + /// Failed to launch program. + ExecutionError, + /// File map name character was not allowed ('..', '/', '\\'). + InvalidFileMapName, + /// Generic filesystem error. + RemoteFilesystemError, + /// Generic filesystem error. + LocalFilesystemError, + /// Unknown error. + UnknownError, +} + +/// Enumeration of permitted program names. +pub static PERMITTED_PROGRAMS: Lazy> = Lazy::new(|| { + vec![ + "convert", + "ffprobe", + "ffmpeg", + "file", + "gifsicle", + "identify", + "image-intensities", + "jpegtran", + "mediastat", + "mediathumb", + "optipng", + "safe-rsvg-convert", + "svgstat", + ] + .into_iter() + .collect() +}); + +/// Mapping between file name and file contents. +pub type FileMap = HashMap>; + +/// Output reply after command execution has finished. +#[derive(Debug, Deserialize, Serialize)] +pub struct CommandReply { + pub status: u8, + pub stdout: Vec, + pub stderr: Vec, +} diff --git a/native/philomena/mediaproc_client/Cargo.toml b/native/philomena/mediaproc_client/Cargo.toml new file mode 100644 index 00000000..19c78297 --- /dev/null +++ b/native/philomena/mediaproc_client/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "mediaproc_client" +version = "0.1.0" +edition = "2021" + +[dependencies] +clap = { version = "4.5", features = ["derive"] } +mediaproc = { path = "../mediaproc" } +tokio = { version = "1.0", features = ["full"] } diff --git a/native/philomena/mediaproc_client/src/main.rs b/native/philomena/mediaproc_client/src/main.rs new file mode 100644 index 00000000..f9e70502 --- /dev/null +++ b/native/philomena/mediaproc_client/src/main.rs @@ -0,0 +1,62 @@ +use std::io::Write; +use std::process::ExitCode; + +use clap::{Parser, Subcommand}; +use mediaproc::client::{connect_to_socket_server, execute_command}; +use mediaproc::MediaProcessorClient; + +#[derive(Parser, Debug)] +#[command(version, about = "RPC Media Processor Client", long_about = None)] +struct Arguments { + /// Server address to connect to, like localhost:1500 + server_addr: String, + + /// Subcommand to execute. + #[command(subcommand)] + invocation_type: InvocationType, +} + +#[derive(Subcommand, Debug)] +enum InvocationType { + /// Execute a command with the given arguments on the remote server. + ExecuteCommand { + /// Program name to execute. + /// + /// One of convert, ffprobe, ffmpeg, file, gifsicle, identify, + /// image-intensities, jpegtran, mediastat, optipng, safe-rsvg-convert. + program: String, + /// Arguments to pass to program. + args: Vec, + }, +} + +#[tokio::main(flavor = "current_thread")] +async fn main() -> ExitCode { + let args = Arguments::parse(); + let client = connect_to_socket_server(&args.server_addr) + .await + .expect("failed to connect to server"); + + match args.invocation_type { + InvocationType::ExecuteCommand { program, args } => { + run_command_client(&client, program, args).await + } + } +} + +async fn run_command_client( + client: &MediaProcessorClient, + program: String, + args: Vec, +) -> ExitCode { + let reply = execute_command(client, program, args).await.unwrap(); + + write_then_drop(std::io::stderr(), reply.stderr); + write_then_drop(std::io::stdout(), reply.stdout); + + reply.status.into() +} + +fn write_then_drop(mut stream: impl Write, data: Vec) { + stream.write_all(&data).unwrap() +} diff --git a/native/philomena/mediaproc_server/Cargo.toml b/native/philomena/mediaproc_server/Cargo.toml new file mode 100644 index 00000000..834dcd81 --- /dev/null +++ b/native/philomena/mediaproc_server/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "mediaproc_server" +version = "0.1.0" +edition = "2021" + +[dependencies] +env_logger = "0.11" +clap = { version = "4.5", features = ["derive"] } +futures = "0.3" +mediaproc = { path = "../mediaproc" } +tarpc = { version = "0.35", features = ["full"] } +tempfile = "3" +tokio = { version = "1.0", features = ["full"] } +tracing = "0.1" diff --git a/native/philomena/mediaproc_server/src/command_server.rs b/native/philomena/mediaproc_server/src/command_server.rs new file mode 100644 index 00000000..0108ef2c --- /dev/null +++ b/native/philomena/mediaproc_server/src/command_server.rs @@ -0,0 +1,63 @@ +use std::collections::HashSet; +use std::os::unix::process::ExitStatusExt; + +use mediaproc::{CommandReply, ExecuteCommandError, FileMap, PERMITTED_PROGRAMS}; +use tokio::process::Command; + +fn validate_name(name: &str) -> Result<(), ExecuteCommandError> { + if name == "." || name.contains("..") || name.contains('/') || name.contains('\\') { + return Err(ExecuteCommandError::InvalidFileMapName); + } + + Ok(()) +} + +pub async fn execute_command( + program: String, + arguments: Vec, + file_map: FileMap, +) -> Result<(CommandReply, FileMap), ExecuteCommandError> { + use std::fs::{read, write}; + + // Check program name. + if !PERMITTED_PROGRAMS.contains(&program.as_ref()) { + return Err(ExecuteCommandError::UnpermittedProgram(program)); + } + + // Create a new temporary directory which we will work in. + let dir = tempfile::tempdir().map_err(|_| ExecuteCommandError::RemoteFilesystemError)?; + + // Verify and write out all files. + let mut files = HashSet::::new(); + for (name, contents) in file_map { + validate_name(&name)?; + files.insert(name.clone()); + + let path = dir.path().join(name); + write(path, contents).map_err(|_| ExecuteCommandError::RemoteFilesystemError)?; + } + + // Run the command. + let output = Command::new(program) + .args(arguments) + .current_dir(dir.path()) + .output() + .await + .map_err(|_| ExecuteCommandError::ExecutionError)?; + + // Read back all files. + let mut file_map = FileMap::new(); + for name in files { + let path = dir.path().join(name.clone()); + let contents = read(path).map_err(|_| ExecuteCommandError::RemoteFilesystemError)?; + file_map.insert(name, contents); + } + + let reply = CommandReply { + status: output.status.into_raw() as u8, + stdout: output.stdout, + stderr: output.stderr, + }; + + Ok((reply, file_map)) +} diff --git a/native/philomena/mediaproc_server/src/main.rs b/native/philomena/mediaproc_server/src/main.rs new file mode 100644 index 00000000..05683c08 --- /dev/null +++ b/native/philomena/mediaproc_server/src/main.rs @@ -0,0 +1,69 @@ +use std::net::SocketAddr; + +use clap::Parser; +use futures::{future, Future, StreamExt}; +use mediaproc::{CommandReply, ExecuteCommandError, FileMap, MediaProcessor}; +use tarpc::context; +use tarpc::server::Channel; + +mod command_server; +mod signal; + +#[derive(Parser, Debug)] +#[command(version, about = "RPC Media Processor Server", long_about = None)] +struct Arguments { + /// Socket address to bind to, like 127.0.0.1:1500 + server_addr: SocketAddr, +} + +#[derive(Clone)] +struct MediaProcessorServer; + +impl MediaProcessor for MediaProcessorServer { + async fn execute_command( + self, + _: context::Context, + program: String, + arguments: Vec, + file_map: FileMap, + ) -> Result<(CommandReply, FileMap), ExecuteCommandError> { + command_server::execute_command(program, arguments, file_map).await + } +} + +fn main() { + env_logger::init(); + + let args = Arguments::parse(); + + serve(&args); +} + +async fn spawn(fut: impl Future + Send + 'static) { + tokio::spawn(fut); +} + +#[tokio::main] +async fn serve(args: &Arguments) { + signal::install_handlers(); + + let codec = tarpc::tokio_serde::formats::Bincode::default; + let mut listener = tarpc::serde_transport::tcp::listen(args.server_addr, codec) + .await + .unwrap(); + + listener.config_mut().max_frame_length(usize::MAX); + listener + // Ignore accept errors. + .filter_map(|r| future::ready(r.ok())) + .map(tarpc::server::BaseChannel::with_defaults) + .map(|channel| { + tokio::spawn( + channel + .execute(MediaProcessorServer.serve()) + .for_each(spawn), + ); + }) + .collect() + .await +} diff --git a/native/philomena/mediaproc_server/src/signal.rs b/native/philomena/mediaproc_server/src/signal.rs new file mode 100644 index 00000000..68135d36 --- /dev/null +++ b/native/philomena/mediaproc_server/src/signal.rs @@ -0,0 +1,15 @@ +use tokio::signal::unix::{signal, SignalKind}; + +pub fn install_handlers() { + let mut sigterm = signal(SignalKind::terminate()).unwrap(); + let mut sigint = signal(SignalKind::interrupt()).unwrap(); + + tokio::spawn(async move { + tokio::select! { + _ = sigterm.recv() => tracing::debug!("Received SIGTERM"), + _ = sigint.recv() => tracing::debug!("Received SIGINT"), + }; + + std::process::exit(1); + }); +} diff --git a/native/philomena/src/asyncnif.rs b/native/philomena/src/asyncnif.rs new file mode 100644 index 00000000..faf84584 --- /dev/null +++ b/native/philomena/src/asyncnif.rs @@ -0,0 +1,26 @@ +use once_cell::sync::Lazy; +use rustler::{Atom, Env, OwnedEnv, Term}; +use std::future::Future; +use std::marker::Send; +use tokio::runtime::Runtime; + +static RUNTIME: Lazy = Lazy::new(|| Runtime::new().unwrap()); + +pub fn call_async(caller_env: Env, fut: F, w: W) -> Atom +where + F: Future + Send + 'static, + W: for<'a> FnOnce(Env<'a>, T) -> Term<'a>, + W: Send + 'static, +{ + let pid = caller_env.pid(); + + RUNTIME.spawn(async move { + let output = fut.await; + let owned_env = OwnedEnv::new(); + owned_env.run(move |env| { + let _ = env.send(&pid, w(env, output)); + }); + }); + + rustler::types::atom::ok() +} diff --git a/native/philomena/src/lib.rs b/native/philomena/src/lib.rs index 6f7f72b2..f2f5893e 100644 --- a/native/philomena/src/lib.rs +++ b/native/philomena/src/lib.rs @@ -1,10 +1,12 @@ use jemallocator::Jemalloc; -use rustler::{Atom, Binary}; +use rustler::{Atom, Binary, Env}; use std::collections::HashMap; +mod asyncnif; mod camo; mod domains; mod markdown; +mod remote; #[cfg(test)] mod tests; mod zip; @@ -35,6 +37,19 @@ fn camo_image_url(input: &str) -> String { camo::image_url_careful(input) } +// Remote NIF wrappers. + +#[rustler::nif] +fn async_process_command( + env: Env, + server_addr: String, + program: String, + arguments: Vec, +) -> Atom { + let fut = remote::process_command(server_addr, program, arguments); + asyncnif::call_async(env, fut, remote::with_env) +} + // Zip NIF wrappers. #[rustler::nif] diff --git a/native/philomena/src/remote.rs b/native/philomena/src/remote.rs new file mode 100644 index 00000000..e0cd4447 --- /dev/null +++ b/native/philomena/src/remote.rs @@ -0,0 +1,66 @@ +use mediaproc::client::{connect_to_socket_server, execute_command}; +use mediaproc::CommandReply; +use rustler::{atoms, Encoder, Env, NifStruct, OwnedBinary, Term}; + +atoms! { + nil, + command_reply, +} + +#[derive(NifStruct)] +#[module = "Elixir.Philomena.Native.CommandReply"] +struct CommandReply_<'a> { + stdout: Term<'a>, + stderr: Term<'a>, + status: u8, +} + +fn binary_or_nil<'a>(env: Env<'a>, data: Vec) -> Term<'a> { + match OwnedBinary::new(data.len()) { + Some(mut binary) => { + binary.copy_from_slice(&data); + binary.release(env).to_term(env) + } + None => nil().to_term(env), + } +} + +pub async fn process_command( + server_addr: String, + program: String, + arguments: Vec, +) -> CommandReply { + let client = match connect_to_socket_server(&server_addr).await { + Some(client) => client, + None => { + return CommandReply { + stdout: vec![], + stderr: "failed to connect to server".into(), + status: 255, + } + } + }; + + match execute_command(&client, program, arguments).await { + Ok(reply) => reply, + Err(err) => CommandReply { + stdout: vec![], + stderr: format!("failed to execute command: {err:?}").into(), + status: 255, + }, + } +} + +/// Converts the response into a {:command_reply, %CommandReply{...}} message +/// which gets sent back to the caller. +pub fn with_env<'a>(env: Env<'a>, r: CommandReply) -> Term<'a> { + ( + command_reply(), + CommandReply_ { + stdout: binary_or_nil(env, r.stdout), + stderr: binary_or_nil(env, r.stderr), + status: r.status, + }, + ) + .encode(env) +} diff --git a/priv/repo/seeds_development.exs b/priv/repo/seeds_development.exs index 47bcd1e7..0d2c5645 100644 --- a/priv/repo/seeds_development.exs +++ b/priv/repo/seeds_development.exs @@ -46,7 +46,7 @@ request_attributes = [ IO.puts "---- Generating images" for image_def <- resources["remote_images"] do - file = Briefly.create!() + file = Briefly.create!(extname: ".png") now = DateTime.utc_now() |> DateTime.to_unix(:microsecond) IO.puts "Fetching #{image_def["url"]} ..."