2018-12-23 21:04:54 +01:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-03 23:44:49 +01:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 21:04:54 +01:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-11-30 19:33:34 +01:00
|
|
|
defmodule Pleroma.Upload.Filter.AnonymizeFilename do
|
2018-12-07 14:07:11 +01:00
|
|
|
@moduledoc """
|
|
|
|
Replaces the original filename with a pre-defined text or randomly generated string.
|
|
|
|
|
|
|
|
Should be used after `Pleroma.Upload.Filter.Dedupe`.
|
|
|
|
"""
|
2018-11-30 19:33:34 +01:00
|
|
|
@behaviour Pleroma.Upload.Filter
|
|
|
|
|
2019-06-30 09:28:35 +02:00
|
|
|
alias Pleroma.Config
|
|
|
|
alias Pleroma.Upload
|
|
|
|
|
|
|
|
def filter(%Upload{name: name} = upload) do
|
|
|
|
extension = List.last(String.split(name, "."))
|
|
|
|
name = predefined_name(extension) || random(extension)
|
2020-09-05 00:40:59 +02:00
|
|
|
{:ok, :filtered, %Upload{upload | name: name}}
|
2019-06-30 09:28:35 +02:00
|
|
|
end
|
|
|
|
|
2020-09-05 00:40:59 +02:00
|
|
|
def filter(_), do: {:ok, :noop}
|
|
|
|
|
2019-06-30 09:28:35 +02:00
|
|
|
@spec predefined_name(String.t()) :: String.t() | nil
|
|
|
|
defp predefined_name(extension) do
|
|
|
|
with name when not is_nil(name) <- Config.get([__MODULE__, :text]),
|
|
|
|
do: String.replace(name, "{extension}", extension)
|
2018-12-07 14:07:11 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
defp random(extension) do
|
|
|
|
string =
|
|
|
|
10
|
|
|
|
|> :crypto.strong_rand_bytes()
|
|
|
|
|> Base.url_encode64(padding: false)
|
|
|
|
|
|
|
|
string <> "." <> extension
|
2018-11-30 19:33:34 +01:00
|
|
|
end
|
|
|
|
end
|