2023-04-03 03:26:23 +00:00
|
|
|
use crate::database;
|
2023-04-03 22:40:37 +00:00
|
|
|
use rand::seq::SliceRandom;
|
2023-04-03 03:26:23 +00:00
|
|
|
use regex::Regex;
|
2023-04-03 23:52:17 +00:00
|
|
|
use rusqlite::Connection;
|
2023-04-03 03:26:23 +00:00
|
|
|
|
2023-04-03 23:52:17 +00:00
|
|
|
pub fn get_longurl(shortlink: String, db: &Connection) -> String {
|
2023-04-03 03:26:23 +00:00
|
|
|
if validate_link(&shortlink) {
|
2023-04-03 23:52:17 +00:00
|
|
|
database::find_url(shortlink.as_str(), db)
|
2023-04-03 03:26:23 +00:00
|
|
|
} else {
|
2023-05-23 22:58:57 +00:00
|
|
|
String::new()
|
2023-04-03 03:26:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn validate_link(link: &str) -> bool {
|
2024-03-10 23:18:07 +00:00
|
|
|
let re = Regex::new("^[a-z0-9-_]+$").unwrap();
|
2023-04-03 03:26:23 +00:00
|
|
|
re.is_match(link)
|
|
|
|
}
|
2023-04-03 16:55:27 +00:00
|
|
|
|
2023-04-03 23:52:17 +00:00
|
|
|
pub fn getall(db: &Connection) -> String {
|
|
|
|
let links = database::getall(db);
|
2023-04-03 16:55:27 +00:00
|
|
|
links.join("\n")
|
|
|
|
}
|
2023-04-03 22:40:37 +00:00
|
|
|
|
2023-04-03 23:52:17 +00:00
|
|
|
pub fn add_link(req: String, db: &Connection) -> (bool, String) {
|
2023-04-03 22:40:37 +00:00
|
|
|
let chunks: Vec<&str> = req.split(';').collect();
|
2023-05-23 22:58:57 +00:00
|
|
|
let longlink = String::from(chunks[0]);
|
2023-04-03 22:40:37 +00:00
|
|
|
|
|
|
|
let mut shortlink;
|
|
|
|
if chunks.len() > 1 {
|
|
|
|
shortlink = chunks[1].to_string().to_lowercase();
|
2023-05-23 23:03:27 +00:00
|
|
|
if shortlink.is_empty() {
|
2023-04-03 22:40:37 +00:00
|
|
|
shortlink = random_name();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
shortlink = random_name();
|
|
|
|
}
|
|
|
|
|
2023-05-23 23:03:27 +00:00
|
|
|
if validate_link(shortlink.as_str()) && get_longurl(shortlink.clone(), db).is_empty() {
|
2023-04-03 23:52:17 +00:00
|
|
|
(
|
|
|
|
database::add_link(shortlink.clone(), longlink, db),
|
|
|
|
shortlink,
|
|
|
|
)
|
2023-04-03 22:40:37 +00:00
|
|
|
} else {
|
2023-05-23 22:58:57 +00:00
|
|
|
(false, String::from("shortUrl not valid or already in use"))
|
2023-04-03 22:40:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn random_name() -> String {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
static ADJECTIVES: [&str; 108] = ["admiring", "adoring", "affectionate", "agitated", "amazing", "angry", "awesome", "beautiful",
|
|
|
|
"blissful", "bold", "boring", "brave", "busy", "charming", "clever", "compassionate", "competent", "condescending", "confident", "cool",
|
|
|
|
"cranky", "crazy", "dazzling", "determined", "distracted", "dreamy", "eager", "ecstatic", "elastic", "elated", "elegant", "eloquent", "epic",
|
|
|
|
"exciting", "fervent", "festive", "flamboyant", "focused", "friendly", "frosty", "funny", "gallant", "gifted", "goofy", "gracious",
|
|
|
|
"great", "happy", "hardcore", "heuristic", "hopeful", "hungry", "infallible", "inspiring", "intelligent", "interesting", "jolly",
|
|
|
|
"jovial", "keen", "kind", "laughing", "loving", "lucid", "magical", "modest", "musing", "mystifying", "naughty", "nervous", "nice",
|
|
|
|
"nifty", "nostalgic", "objective", "optimistic", "peaceful", "pedantic", "pensive", "practical", "priceless", "quirky", "quizzical",
|
|
|
|
"recursing", "relaxed", "reverent", "romantic", "sad", "serene", "sharp", "silly", "sleepy", "stoic", "strange", "stupefied", "suspicious",
|
|
|
|
"sweet", "tender", "thirsty", "trusting", "unruffled", "upbeat", "vibrant", "vigilant", "vigorous", "wizardly", "wonderful", "xenodochial",
|
|
|
|
"youthful", "zealous", "zen"];
|
|
|
|
#[rustfmt::skip]
|
|
|
|
static NAMES: [&str; 241] = ["agnesi", "albattani", "allen", "almeida", "antonelli", "archimedes", "ardinghelli", "aryabhata", "austin",
|
|
|
|
"babbage", "banach", "banzai", "bardeen", "bartik", "bassi", "beaver", "bell", "benz", "bhabha", "bhaskara", "black", "blackburn", "blackwell",
|
|
|
|
"bohr", "booth", "borg", "bose", "bouman", "boyd", "brahmagupta", "brattain", "brown", "buck", "burnell", "cannon", "carson", "cartwright",
|
|
|
|
"carver", "cauchy", "cerf", "chandrasekhar", "chaplygin", "chatelet", "chatterjee", "chaum", "chebyshev", "clarke", "cohen", "colden", "cori",
|
|
|
|
"cray", "curie", "curran", "darwin", "davinci", "dewdney", "dhawan", "diffie", "dijkstra", "dirac", "driscoll", "dubinsky", "easley", "edison",
|
|
|
|
"einstein", "elbakyan", "elgamal", "elion", "ellis", "engelbart", "euclid", "euler", "faraday", "feistel", "fermat", "fermi", "feynman", "franklin",
|
|
|
|
"gagarin", "galileo", "galois", "ganguly", "gates", "gauss", "germain", "goldberg", "goldstine", "goldwasser", "golick", "goodall", "gould", "greider",
|
|
|
|
"grothendieck", "haibt", "hamilton", "hardy", "haslett", "hawking", "heisenberg", "hellman", "hermann", "herschel", "hertz", "heyrovsky", "hodgkin",
|
|
|
|
"hofstadter", "hoover", "hopper", "hugle", "hypatia", "ishizaka", "jackson", "jang", "jemison", "jennings", "jepsen", "johnson", "joliot", "jones",
|
|
|
|
"kalam", "kapitsa", "kare", "keldysh", "keller", "kepler", "khayyam", "khorana", "kilby", "kirch", "knuth", "kowalevski", "lalande", "lamarr",
|
|
|
|
"lamport", "leakey", "leavitt", "lederberg", "lehmann", "lewin", "lichterman", "liskov", "lovelace", "lumiere", "mahavira", "margulis", "matsumoto",
|
|
|
|
"maxwell", "mayer", "mccarthy", "mcclintock", "mclaren", "mclean", "mcnulty", "meitner", "mendel", "mendeleev", "meninsky", "merkle", "mestorf",
|
|
|
|
"mirzakhani", "montalcini", "moore", "morse", "moser", "murdock", "napier", "nash", "neumann", "newton", "nightingale", "nobel", "noether", "northcutt",
|
|
|
|
"noyce", "panini", "pare", "pascal", "pasteur", "payne", "perlman", "pike", "poincare", "poitras", "proskuriakova", "ptolemy", "raman", "ramanujan",
|
|
|
|
"rhodes", "ride", "riemann", "ritchie", "robinson", "roentgen", "rosalind", "rubin", "saha", "sammet", "sanderson", "satoshi", "shamir", "shannon",
|
|
|
|
"shaw", "shirley", "shockley", "shtern", "sinoussi", "snyder", "solomon", "spence", "stonebraker", "sutherland", "swanson", "swartz", "swirles",
|
|
|
|
"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"];
|
|
|
|
|
|
|
|
format!(
|
|
|
|
"{0}-{1}",
|
2023-09-19 23:11:29 +00:00
|
|
|
ADJECTIVES.choose(&mut rand::thread_rng()).unwrap(),
|
|
|
|
NAMES.choose(&mut rand::thread_rng()).unwrap()
|
2023-04-03 22:40:37 +00:00
|
|
|
)
|
|
|
|
}
|