2019-05-22 14:12:11 +02:00
|
|
|
# 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/dedupe.ex
|
|
|
|
|
2020-01-26 21:36:50 +01:00
|
|
|
defmodule Mobilizon.Web.Upload.Filter.Dedupe do
|
2019-05-22 14:12:11 +02:00
|
|
|
@moduledoc """
|
|
|
|
Names the file after its hash to avoid dedupes
|
|
|
|
"""
|
2020-01-26 21:36:50 +01:00
|
|
|
@behaviour Mobilizon.Web.Upload.Filter
|
|
|
|
alias Mobilizon.Web.Upload
|
2019-05-22 14:12:11 +02:00
|
|
|
|
2020-10-25 12:00:00 +01:00
|
|
|
def filter(%Upload{name: name, tempfile: tempfile} = upload) do
|
2019-09-22 13:41:24 +02:00
|
|
|
extension = name |> String.split(".") |> List.last()
|
2020-10-25 12:00:00 +01:00
|
|
|
shasum = :crypto.hash(:sha256, File.read!(tempfile)) |> Base.encode16(case: :lower)
|
2019-05-22 14:12:11 +02:00
|
|
|
filename = shasum <> "." <> extension
|
2019-09-22 13:41:24 +02:00
|
|
|
|
2020-10-25 12:00:00 +01:00
|
|
|
{:ok, :filtered, %Upload{upload | id: shasum, path: filename}}
|
2019-05-22 14:12:11 +02:00
|
|
|
end
|
2020-10-25 12:00:00 +01:00
|
|
|
|
|
|
|
def filter(_), do: {:ok, :noop}
|
2019-05-22 14:12:11 +02:00
|
|
|
end
|