1
0
Fork 0
mirror of https://github.com/SinTan1729/chhoto-url synced 2024-12-26 07:38:36 -06:00

new: Support uid slug

This commit is contained in:
Sayantan Santra 2024-03-31 00:38:42 -05:00
parent 9a520c122e
commit f9e642275a
Signed by: SinTan1729
GPG key ID: EB3E68BFBA25C85F
3 changed files with 37 additions and 8 deletions

10
actix/Cargo.lock generated
View file

@ -481,6 +481,7 @@ dependencies = [
"actix-session", "actix-session",
"actix-web", "actix-web",
"env_logger", "env_logger",
"nanoid",
"rand", "rand",
"regex", "regex",
"rusqlite", "rusqlite",
@ -973,6 +974,15 @@ dependencies = [
"windows-sys 0.48.0", "windows-sys 0.48.0",
] ]
[[package]]
name = "nanoid"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ffa00dec017b5b1a8b7cf5e2c008bfda1aa7e0697ac1508b491fdf2622fb4d8"
dependencies = [
"rand",
]
[[package]] [[package]]
name = "num-conv" name = "num-conv"
version = "0.1.0" version = "0.1.0"

View file

@ -31,3 +31,4 @@ regex = "1.10.3"
rand = "0.8.5" rand = "0.8.5"
actix-session = { version = "0.9.0", features = ["cookie-session"] } actix-session = { version = "0.9.0", features = ["cookie-session"] }
env_logger = "0.11.1" env_logger = "0.11.1"
nanoid = "0.4.0"

View file

@ -1,4 +1,7 @@
use std::env;
use crate::database; use crate::database;
use nanoid::nanoid;
use rand::seq::SliceRandom; use rand::seq::SliceRandom;
use regex::Regex; use regex::Regex;
use rusqlite::Connection; use rusqlite::Connection;
@ -25,14 +28,21 @@ pub fn add_link(req: String, db: &Connection) -> (bool, String) {
let chunks: Vec<&str> = req.split(';').collect(); let chunks: Vec<&str> = req.split(';').collect();
let longlink = String::from(chunks[0]); let longlink = String::from(chunks[0]);
let style = env::var("slug-style").unwrap_or(String::from("pair"));
let len_str = env::var("slug-length").unwrap_or(String::from("8"));
let mut len: usize = len_str.parse().unwrap_or(8);
if len < 4 {
len = 4;
}
let mut shortlink; let mut shortlink;
if chunks.len() > 1 { if chunks.len() > 1 {
shortlink = chunks[1].to_string().to_lowercase(); shortlink = chunks[1].to_string().to_lowercase();
if shortlink.is_empty() { if shortlink.is_empty() {
shortlink = random_name(); shortlink = gen_link(style, len);
} }
} else { } else {
shortlink = random_name(); shortlink = gen_link(style, len);
} }
if validate_link(shortlink.as_str()) && get_longurl(shortlink.clone(), db).is_none() { if validate_link(shortlink.as_str()) && get_longurl(shortlink.clone(), db).is_none() {
@ -53,7 +63,7 @@ pub fn delete_link(shortlink: String, db: &Connection) -> bool {
} }
} }
fn random_name() -> String { fn gen_link(style: String, len: usize) -> String {
#[rustfmt::skip] #[rustfmt::skip]
static ADJECTIVES: [&str; 108] = ["admiring", "adoring", "affectionate", "agitated", "amazing", "angry", "awesome", "beautiful", static ADJECTIVES: [&str; 108] = ["admiring", "adoring", "affectionate", "agitated", "amazing", "angry", "awesome", "beautiful",
"blissful", "bold", "boring", "brave", "busy", "charming", "clever", "compassionate", "competent", "condescending", "confident", "cool", "blissful", "bold", "boring", "brave", "busy", "charming", "clever", "compassionate", "competent", "condescending", "confident", "cool",
@ -85,9 +95,17 @@ fn random_name() -> String {
"taussig", "tesla", "tharp", "thompson", "torvalds", "tu", "turing", "varahamihira", "vaughan", "vaughn", "villani", "visvesvaraya", "volhard", "taussig", "tesla", "tharp", "thompson", "torvalds", "tu", "turing", "varahamihira", "vaughan", "vaughn", "villani", "visvesvaraya", "volhard",
"wescoff", "weierstrass", "wilbur", "wiles", "williams", "williamson", "wilson", "wing", "wozniak", "wright", "wu", "yalow", "yonath", "zhukovsky"]; "wescoff", "weierstrass", "wilbur", "wiles", "williams", "williamson", "wilson", "wing", "wozniak", "wright", "wu", "yalow", "yonath", "zhukovsky"];
format!( #[rustfmt::skip]
"{0}-{1}", static CHARS: [char; 36] = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
ADJECTIVES.choose(&mut rand::thread_rng()).unwrap(), 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
NAMES.choose(&mut rand::thread_rng()).unwrap()
) if style == "uid" {
nanoid!(len, &CHARS)
} else {
format!(
"{0}-{1}",
ADJECTIVES.choose(&mut rand::thread_rng()).unwrap(),
NAMES.choose(&mut rand::thread_rng()).unwrap()
)
}
} }