2020-05-20 11:26:43 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Helpers.MediaHelper do
|
|
|
|
@moduledoc """
|
|
|
|
Handles common media-related operations.
|
|
|
|
"""
|
|
|
|
|
2020-08-25 16:18:22 -06:00
|
|
|
def image_resize(url, %{max_width: max_width, max_height: max_height} = options) do
|
|
|
|
quality = options[:quality] || 85
|
2020-08-20 23:59:08 -06:00
|
|
|
|
2020-05-20 11:26:43 -06:00
|
|
|
cmd = ~s"""
|
2020-08-25 16:18:22 -06:00
|
|
|
convert - -resize '#{max_width}x#{max_height}>' -quality #{quality} -
|
2020-05-20 11:26:43 -06:00
|
|
|
"""
|
|
|
|
|
2020-08-18 09:23:27 -06:00
|
|
|
pid = Port.open({:spawn, cmd}, [:use_stdio, :in, :stream, :exit_status, :binary])
|
2020-08-25 16:18:22 -06:00
|
|
|
{:ok, env} = url |> Pleroma.Web.MediaProxy.url() |> Pleroma.HTTP.get()
|
|
|
|
image = env.body
|
|
|
|
Port.command(pid, image)
|
2020-08-21 11:02:57 -06:00
|
|
|
loop_recv(pid)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp loop_recv(pid) do
|
|
|
|
loop_recv(pid, <<>>)
|
|
|
|
end
|
2020-08-18 09:23:27 -06:00
|
|
|
|
2020-08-21 11:02:57 -06:00
|
|
|
defp loop_recv(pid, acc) do
|
2020-08-18 09:23:27 -06:00
|
|
|
receive do
|
|
|
|
{^pid, {:data, data}} ->
|
2020-08-21 11:02:57 -06:00
|
|
|
loop_recv(pid, acc <> data)
|
2020-08-18 09:23:27 -06:00
|
|
|
|
2020-08-21 11:02:57 -06:00
|
|
|
{^pid, {:exit_status, 0}} ->
|
|
|
|
{:ok, acc}
|
|
|
|
|
|
|
|
{^pid, {:exit_status, status}} ->
|
2020-08-18 09:23:27 -06:00
|
|
|
{:error, status}
|
2020-05-20 11:26:43 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|