remove nanoid
This commit is contained in:
parent
0632ee97dc
commit
b925726977
4 changed files with 52 additions and 17 deletions
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"
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue