X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fupload.ex;h=a52b698bf65a21f98bcb3466ffa43a35e91dcae4;hb=e8bf060e6e55396e7a0dbb53dacbf470d8f56beb;hp=015c875938967e3fed944f7eb21b46a85274cb96;hpb=3d5d8c05c9de9a70a8d49576f125b9987f9d34e8;p=akkoma diff --git a/lib/pleroma/upload.ex b/lib/pleroma/upload.ex index 015c87593..a52b698bf 100644 --- a/lib/pleroma/upload.ex +++ b/lib/pleroma/upload.ex @@ -31,6 +31,7 @@ defmodule Pleroma.Upload do """ alias Ecto.UUID + alias Pleroma.Config require Logger @type source :: @@ -66,6 +67,7 @@ defmodule Pleroma.Upload do end @spec store(source, options :: [option()]) :: {:ok, Map.t()} | {:error, any()} + @doc "Store a file. If using a `Plug.Upload{}` as the source, be sure to use `Majic.Plug` to ensure its content_type and filename is correct." def store(upload, opts \\ []) do opts = get_opts(opts) @@ -139,14 +141,13 @@ defmodule Pleroma.Upload do end defp prepare_upload(%Plug.Upload{} = file, opts) do - with :ok <- check_file_size(file.path, opts.size_limit), - {:ok, content_type, name} <- Pleroma.MIME.file_mime_type(file.path, file.filename) do + with :ok <- check_file_size(file.path, opts.size_limit) do {:ok, %__MODULE__{ id: UUID.generate(), - name: name, + name: file.filename, tempfile: file.path, - content_type: content_type + content_type: file.content_type }} end end @@ -154,16 +155,17 @@ defmodule Pleroma.Upload do defp prepare_upload(%{img: "data:image/" <> image_data}, opts) do parsed = Regex.named_captures(~r/(?jpeg|png|gif);base64,(?.*)/, image_data) data = Base.decode64!(parsed["data"], ignore: :whitespace) - hash = String.downcase(Base.encode16(:crypto.hash(:sha256, data))) + hash = Base.encode16(:crypto.hash(:sha256, data), lower: true) with :ok <- check_binary_size(data, opts.size_limit), tmp_path <- tempfile_for_image(data), - {:ok, content_type, name} <- - Pleroma.MIME.bin_mime_type(data, hash <> "." <> parsed["filetype"]) do + {:ok, %{mime_type: content_type}} <- + Majic.perform({:bytes, data}, pool: Pleroma.MajicPool), + [ext | _] <- MIME.extensions(content_type) do {:ok, %__MODULE__{ id: UUID.generate(), - name: name, + name: hash <> "." <> ext, tempfile: tmp_path, content_type: content_type }} @@ -172,7 +174,7 @@ defmodule Pleroma.Upload do # For Mix.Tasks.MigrateLocalUploads defp prepare_upload(%__MODULE__{tempfile: path} = upload, _opts) do - with {:ok, content_type} <- Pleroma.MIME.file_mime_type(path) do + with {:ok, %{mime_type: content_type}} <- Majic.perform(path, pool: Pleroma.MajicPool) do {:ok, %__MODULE__{upload | content_type: content_type}} end end @@ -227,4 +229,53 @@ defmodule Pleroma.Upload do end defp url_from_spec(_upload, _base_url, {:url, url}), do: url + + def base_url do + uploader = Config.get([Pleroma.Upload, :uploader]) + upload_base_url = Config.get([Pleroma.Upload, :base_url]) + public_endpoint = Config.get([uploader, :public_endpoint]) + + case uploader do + Pleroma.Uploaders.Local -> + cond do + !is_nil(upload_base_url) -> + upload_base_url + + true -> + Pleroma.Web.base_url() <> "/media/" + end + + Pleroma.Uploaders.S3 -> + bucket = Config.get([Pleroma.Uploaders.S3, :bucket]) + + bucket_with_namespace = + cond do + truncated_namespace = Config.get([Pleroma.Uploaders.S3, :truncated_namespace]) -> + truncated_namespace + + namespace = Config.get([Pleroma.Uploaders.S3, :bucket_namespace]) -> + namespace <> ":" <> bucket + + true -> + bucket + end + + cond do + !is_nil(public_endpoint) -> + Path.join([public_endpoint, bucket_with_namespace]) + + true -> + Path.join([upload_base_url, bucket_with_namespace]) + end + + _ -> + cond do + !is_nil(public_endpoint) -> + public_endpoint + + true -> + upload_base_url + end + end + end end