From b925726977da7cc50a377dd09543bc251b5e5698 Mon Sep 17 00:00:00 2001 From: zack Date: Tue, 22 Oct 2024 17:23:02 -0400 Subject: [PATCH] remove nanoid --- deps.nix | 15 ------ lib/zoeyscomputer/id_generator.ex | 50 +++++++++++++++++++ .../controllers/image_controller.ex | 3 +- mix.exs | 1 - 4 files changed, 52 insertions(+), 17 deletions(-) create mode 100644 lib/zoeyscomputer/id_generator.ex diff --git a/deps.nix b/deps.nix index 2490000..01da3de 100644 --- a/deps.nix +++ b/deps.nix @@ -527,21 +527,6 @@ let beamDeps = [ castore hpax ]; }; - nanoid = - let - version = "2.1.0"; - in - buildMix { - inherit version; - name = "nanoid"; - - src = fetchHex { - inherit version; - pkg = "nanoid"; - sha256 = "ebc7a342d02d213534a7f93a091d569b9fea7f26fcd3a638dc655060fc1f76ac"; - }; - }; - nimble_options = let version = "1.1.1"; diff --git a/lib/zoeyscomputer/id_generator.ex b/lib/zoeyscomputer/id_generator.ex new file mode 100644 index 0000000..ab21be0 --- /dev/null +++ b/lib/zoeyscomputer/id_generator.ex @@ -0,0 +1,50 @@ +defmodule Zoeyscomputer.IdGenerator do + @moduledoc """ + Generates URL-friendly unique IDs + """ + + @alphabet "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + @size 7 + + @doc """ + Generates a random ID with default length of 7 characters using URL-friendly characters. + + ## Examples + iex> IdGenerator.generate() + "f3k9m4z" + """ + def generate(size \\ @size) do + alphabet_length = String.length(@alphabet) + + 1..size + |> Enum.reduce([], fn _, acc -> + position = :rand.uniform(alphabet_length) - 1 + char = String.at(@alphabet, position) + [char | acc] + end) + |> Enum.join("") + end + + @doc """ + Generates a random ID and ensures it's unique by checking against a provided function. + + ## Examples + iex> IdGenerator.generate_unique(&MyApp.id_exists?/1) + "x7k9m4z" + """ + def generate_unique(exists_fn, attempts \\ 0) + + def generate_unique(exists_fn, attempts) when attempts < 5 do + id = generate() + + if exists_fn.(id) do + generate_unique(exists_fn, attempts + 1) + else + id + end + end + + def generate_unique(_exists_fn, _attempts) do + raise "Failed to generate unique ID after 5 attempts" + end +end diff --git a/lib/zoeyscomputer_web/controllers/image_controller.ex b/lib/zoeyscomputer_web/controllers/image_controller.ex index af38987..84b70b4 100644 --- a/lib/zoeyscomputer_web/controllers/image_controller.ex +++ b/lib/zoeyscomputer_web/controllers/image_controller.ex @@ -1,6 +1,7 @@ defmodule ZoeyscomputerWeb.ImageController do use ZoeyscomputerWeb, :controller + alias Zoeyscomputer.IdGenerator alias Zoeyscomputer.Images alias Zoeyscomputer.Images.Image @@ -57,7 +58,7 @@ defmodule ZoeyscomputerWeb.ImageController do defp handle_upload(upload) do extension = Path.extname(upload.filename) - id = Nanoid.generate(7) + id = IdGenerator.generate() key = "uploads/#{id}#{extension}" bucket = "imgs" diff --git a/mix.exs b/mix.exs index 338f77d..ed9016f 100644 --- a/mix.exs +++ b/mix.exs @@ -54,7 +54,6 @@ defmodule Zoeyscomputer.MixProject do compile: false, depth: 1}, {:swoosh, "~> 1.5"}, - {:nanoid, "~> 2.1.0"}, {:ex_aws, "~> 2.1"}, {:ex_aws_s3, "~> 2.0"}, {:hackney, "~> 1.9"},