1 defmodule Pleroma.Upload do
6 * `:type`: presets for activity type (defaults to Document) and size limits from app configuration
7 * `:description`: upload alternative text
8 * `:uploader`: override uploader
9 * `:filters`: override filters
10 * `:size_limit`: override size limit
11 * `:activity_type`: override activity type
13 The `%Pleroma.Upload{}` struct: all documented fields are meant to be overwritten in filters:
15 * `:id` - the upload id.
16 * `:name` - the upload file name.
17 * `:path` - the upload path: set at first to `id/name` but can be changed. Keep in mind that the path
18 is once created permanent and changing it (especially in uploaders) is probably a bad idea!
19 * `:tempfile` - path to the temporary file. Prefer in-place changes on the file rather than changing the
20 path as the temporary file is also tracked by `Plug.Upload{}` and automatically deleted once the request is over.
24 * `Pleroma.Uploaders.Uploader`
25 * `Pleroma.Upload.Filter`
32 Plug.Upload.t() | data_uri_string ::
33 String.t() | {:from_local, name :: String.t(), id :: String.t(), path :: String.t()}
36 {:type, :avatar | :banner | :background}
37 | {:description, String.t()}
38 | {:activity_type, String.t()}
39 | {:size_limit, nil | non_neg_integer()}
40 | {:uploader, module()}
41 | {:filters, [module()]}
43 @type t :: %__MODULE__{
47 content_type: String.t(),
50 defstruct [:id, :name, :tempfile, :content_type, :path]
52 @spec store(source, options :: [option()]) :: {:ok, Map.t()} | {:error, any()}
53 def store(upload, opts \\ []) do
56 with {:ok, upload} <- prepare_upload(upload, opts),
57 upload = %__MODULE__{upload | path: upload.path || "#{upload.id}/#{upload.name}"},
58 {:ok, upload} <- Pleroma.Upload.Filter.filter(opts.filters, upload),
59 {:ok, url_spec} <- Pleroma.Uploaders.Uploader.put_file(opts.uploader, upload) do
62 "type" => opts.activity_type,
66 "mediaType" => upload.content_type,
67 "href" => url_from_spec(url_spec)
70 "name" => Map.get(opts, :description) || upload.name
75 "#{__MODULE__} store (using #{inspect(opts.uploader)}) failed: #{inspect(error)}"
82 defp get_opts(opts) do
83 {size_limit, activity_type} =
84 case Keyword.get(opts, :type) do
86 {Pleroma.Config.get!([:instance, :banner_upload_limit]), "Image"}
89 {Pleroma.Config.get!([:instance, :avatar_upload_limit]), "Image"}
92 {Pleroma.Config.get!([:instance, :background_upload_limit]), "Image"}
95 {Pleroma.Config.get!([:instance, :upload_limit]), "Document"}
99 activity_type: Keyword.get(opts, :activity_type, activity_type),
100 size_limit: Keyword.get(opts, :size_limit, size_limit),
101 uploader: Keyword.get(opts, :uploader, Pleroma.Config.get([__MODULE__, :uploader])),
102 filters: Keyword.get(opts, :filters, Pleroma.Config.get([__MODULE__, :filters])),
103 description: Keyword.get(opts, :description)
106 # TODO: 1.0+ : remove old config compatibility
108 if Pleroma.Config.get([__MODULE__, :strip_exif]) == true &&
109 !Enum.member?(opts.filters, Pleroma.Upload.Filter.Mogrify) do
111 Pleroma: configuration `:instance, :strip_exif` is deprecated, please instead set:
113 :pleroma, Pleroma.Upload, [filters: [Pleroma.Upload.Filter.Mogrify]]
115 :pleroma, Pleroma.Upload.Filter.Mogrify, args: "strip"
118 Pleroma.Config.put([Pleroma.Upload.Filter.Mogrify], args: "strip")
119 Map.put(opts, :filters, opts.filters ++ [Pleroma.Upload.Filter.Mogrify])
125 if Pleroma.Config.get([:instance, :dedupe_media]) == true &&
126 !Enum.member?(opts.filters, Pleroma.Upload.Filter.Dedupe) do
128 Pleroma: configuration `:instance, :dedupe_media` is deprecated, please instead set:
130 :pleroma, Pleroma.Upload, [filters: [Pleroma.Upload.Filter.Dedupe]]
133 Map.put(opts, :filters, opts.filters ++ [Pleroma.Upload.Filter.Dedupe])
139 defp prepare_upload(%Plug.Upload{} = file, opts) do
140 with :ok <- check_file_size(file.path, opts.size_limit),
141 {:ok, content_type, name} <- Pleroma.MIME.file_mime_type(file.path, file.filename) do
147 content_type: content_type
152 defp prepare_upload(%{"img" => "data:image/" <> image_data}, opts) do
153 parsed = Regex.named_captures(~r/(?<filetype>jpeg|png|gif);base64,(?<data>.*)/, image_data)
154 data = Base.decode64!(parsed["data"], ignore: :whitespace)
155 hash = String.downcase(Base.encode16(:crypto.hash(:sha256, data)))
157 with :ok <- check_binary_size(data, opts.size_limit),
158 tmp_path <- tempfile_for_image(data),
159 {:ok, content_type, name} <-
160 Pleroma.MIME.bin_mime_type(data, hash <> "." <> parsed["filetype"]) do
166 content_type: content_type
171 # For Mix.Tasks.MigrateLocalUploads
172 defp prepare_upload(upload = %__MODULE__{tempfile: path}, _opts) do
173 with {:ok, content_type} <- Pleroma.MIME.file_mime_type(path) do
174 {:ok, %__MODULE__{upload | content_type: content_type}}
178 defp check_binary_size(binary, size_limit)
179 when is_integer(size_limit) and size_limit > 0 and byte_size(binary) >= size_limit do
180 {:error, :file_too_large}
183 defp check_binary_size(_, _), do: :ok
185 defp check_file_size(path, size_limit) when is_integer(size_limit) and size_limit > 0 do
186 with {:ok, %{size: size}} <- File.stat(path),
187 true <- size <= size_limit do
190 false -> {:error, :file_too_large}
195 defp check_file_size(_, _), do: :ok
197 # Creates a tempfile using the Plug.Upload Genserver which cleans them up
199 defp tempfile_for_image(data) do
200 {:ok, tmp_path} = Plug.Upload.random_file("profile_pics")
201 {:ok, tmp_file} = File.open(tmp_path, [:write, :raw, :binary])
202 IO.binwrite(tmp_file, data)
207 defp url_from_spec({:file, path}) do
208 [Pleroma.Web.base_url(), "media", path]
212 defp url_from_spec({:url, url}) do