+ defp create_name(uuid, ext, type) do
+ case type do
+ "application/octet-stream" ->
+ String.downcase(Enum.join([uuid, ext], "."))
+
+ "audio/mpeg" ->
+ String.downcase(Enum.join([uuid, "mp3"], "."))
+
+ _ ->
+ String.downcase(Enum.join([uuid, List.last(String.split(type, "/"))], "."))
+ end
+ end
+
+ defp get_uuid(file, should_dedupe) do
+ if should_dedupe do
+ Base.encode16(:crypto.hash(:sha256, File.read!(file.path)))
+ else
+ UUID.generate()
+ end
+ end
+
+ defp get_name(file, uuid, type, should_dedupe) do
+ if should_dedupe do
+ create_name(uuid, List.last(String.split(file.filename, ".")), type)
+ else
+ parts = String.split(file.filename, ".")
+
+ new_filename =
+ if length(parts) > 1 do
+ Enum.drop(parts, -1) |> Enum.join(".")
+ else
+ Enum.join(parts)
+ end
+
+ case type do
+ "application/octet-stream" -> file.filename
+ "audio/mpeg" -> new_filename <> ".mp3"
+ "image/jpeg" -> new_filename <> ".jpg"
+ _ -> Enum.join([new_filename, String.split(type, "/") |> List.last()], ".")
+ end
+ end