mobilizon.chapril.org-mobil.../lib/web/upload/filter/anonymize_filename.ex

32 lines
952 B
Elixir
Raw Normal View History

# Portions of this file are derived from Pleroma:
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
# Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/pleroma/upload/filter/anonymize_filename.ex
2020-01-26 21:36:50 +01:00
defmodule Mobilizon.Web.Upload.Filter.AnonymizeFilename do
@moduledoc """
Replaces the original filename with a pre-defined text or randomly generated string.
2020-01-26 21:36:50 +01:00
Should be used after `Mobilizon.Web.Upload.Filter.Dedupe`.
"""
2019-09-08 00:05:54 +02:00
2020-01-26 21:36:50 +01:00
@behaviour Mobilizon.Web.Upload.Filter
2019-09-08 00:05:54 +02:00
alias Mobilizon.Config
def filter(upload) do
extension = List.last(String.split(upload.name, "."))
2019-09-08 00:05:54 +02:00
name = Config.get([__MODULE__, :text], random(extension))
2020-01-26 21:36:50 +01:00
{:ok, %Mobilizon.Web.Upload{upload | name: name}}
end
defp random(extension) do
string =
10
|> :crypto.strong_rand_bytes()
|> Base.url_encode64(padding: false)
string <> "." <> extension
end
end