mirror of
https://github.com/minoplhy/chhoto-url.git
synced 2024-11-25 10:16:46 +00:00
94 lines
5.5 KiB
Rust
94 lines
5.5 KiB
Rust
use crate::database;
|
|
use rand::seq::SliceRandom;
|
|
use regex::Regex;
|
|
use rusqlite::Connection;
|
|
|
|
pub fn get_longurl(shortlink: String, db: &Connection) -> String {
|
|
if validate_link(&shortlink) {
|
|
database::find_url(shortlink.as_str(), db)
|
|
} else {
|
|
String::new()
|
|
}
|
|
}
|
|
|
|
fn validate_link(link: &str) -> bool {
|
|
let re = Regex::new("^[a-z0-9-_]+$").unwrap();
|
|
re.is_match(link)
|
|
}
|
|
|
|
pub fn getall(db: &Connection) -> String {
|
|
let links = database::getall(db);
|
|
links.join("\n")
|
|
}
|
|
|
|
pub fn add_link(req: String, db: &Connection) -> (bool, String) {
|
|
let chunks: Vec<&str> = req.split(';').collect();
|
|
let longlink = String::from(chunks[0]);
|
|
|
|
let mut shortlink;
|
|
if chunks.len() > 1 {
|
|
shortlink = chunks[1].to_string().to_lowercase();
|
|
if shortlink.is_empty() {
|
|
shortlink = random_name();
|
|
}
|
|
} else {
|
|
shortlink = random_name();
|
|
}
|
|
|
|
if validate_link(shortlink.as_str()) && get_longurl(shortlink.clone(), db).is_empty() {
|
|
(
|
|
database::add_link(shortlink.clone(), longlink, db),
|
|
shortlink,
|
|
)
|
|
} else {
|
|
(false, String::from("shortUrl not valid or already in use"))
|
|
}
|
|
}
|
|
|
|
pub fn delete_link(shortlink: String, db: &Connection) -> bool {
|
|
if validate_link(shortlink.as_str()) {
|
|
database::delete_link(shortlink, db)
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
|
|
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}",
|
|
ADJECTIVES.choose(&mut rand::thread_rng()).unwrap(),
|
|
NAMES.choose(&mut rand::thread_rng()).unwrap()
|
|
)
|
|
}
|