remove nanoid
This commit is contained in:
parent
0632ee97dc
commit
b925726977
4 changed files with 52 additions and 17 deletions
15
deps.nix
15
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";
|
||||
|
|
|
|||
50
lib/zoeyscomputer/id_generator.ex
Normal file
50
lib/zoeyscomputer/id_generator.ex
Normal file
|
|
@ -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
|
||||
|
|
@ -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"
|
||||
|
||||
|
|
|
|||
1
mix.exs
1
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"},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue