logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

dedupe.ex (657B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Upload.Filter.Dedupe do
  5. @behaviour Pleroma.Upload.Filter
  6. alias Pleroma.Upload
  7. def filter(%Upload{name: name, tempfile: tempfile} = upload) do
  8. extension =
  9. name
  10. |> String.split(".")
  11. |> List.last()
  12. shasum =
  13. :crypto.hash(:sha256, File.read!(tempfile))
  14. |> Base.encode16(case: :lower)
  15. filename = shasum <> "." <> extension
  16. {:ok, :filtered, %Upload{upload | id: shasum, path: filename}}
  17. end
  18. def filter(_), do: {:ok, :noop}
  19. end