mirror of
https://github.com/minoplhy/chhoto-url.git
synced 2024-11-22 09:16:46 +00:00
chg: Use Option instead of returning empty String
This commit is contained in:
parent
5e4db14ea2
commit
5d8dd6fb63
@ -1,20 +1,13 @@
|
|||||||
use rusqlite::Connection;
|
use rusqlite::Connection;
|
||||||
|
|
||||||
pub fn find_url(shortlink: &str, db: &Connection) -> String {
|
pub fn find_url(shortlink: &str, db: &Connection) -> Option<String> {
|
||||||
let mut statement = db
|
let mut statement = db
|
||||||
.prepare_cached("SELECT long_url FROM urls WHERE short_url = ?1")
|
.prepare_cached("SELECT long_url FROM urls WHERE short_url = ?1")
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let links = statement
|
statement
|
||||||
.query_map([shortlink], |row| row.get("long_url"))
|
.query_row([shortlink], |row| row.get("long_url"))
|
||||||
.unwrap();
|
.ok()
|
||||||
|
|
||||||
let mut longlink = String::new();
|
|
||||||
for link in links {
|
|
||||||
longlink = link.unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
longlink
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getall(db: &Connection) -> Vec<String> {
|
pub fn getall(db: &Connection) -> Vec<String> {
|
||||||
|
@ -78,10 +78,7 @@ async fn error404() -> impl Responder {
|
|||||||
#[get("/{shortlink}")]
|
#[get("/{shortlink}")]
|
||||||
async fn link_handler(shortlink: web::Path<String>, data: web::Data<AppState>) -> impl Responder {
|
async fn link_handler(shortlink: web::Path<String>, data: web::Data<AppState>) -> impl Responder {
|
||||||
let shortlink_str = shortlink.to_string();
|
let shortlink_str = shortlink.to_string();
|
||||||
let longlink = utils::get_longurl(shortlink_str, &data.db);
|
if let Some(longlink) = utils::get_longurl(shortlink_str, &data.db) {
|
||||||
if longlink.is_empty() {
|
|
||||||
Redirect::to("/err/404")
|
|
||||||
} else {
|
|
||||||
let redirect_method = env::var("redirect_method").unwrap_or(String::from("PERMANENT"));
|
let redirect_method = env::var("redirect_method").unwrap_or(String::from("PERMANENT"));
|
||||||
database::add_hit(shortlink.as_str(), &data.db);
|
database::add_hit(shortlink.as_str(), &data.db);
|
||||||
if redirect_method == "TEMPORARY" {
|
if redirect_method == "TEMPORARY" {
|
||||||
@ -90,6 +87,8 @@ async fn link_handler(shortlink: web::Path<String>, data: web::Data<AppState>) -
|
|||||||
// Defaults to permanent redirection
|
// Defaults to permanent redirection
|
||||||
Redirect::to(longlink).permanent()
|
Redirect::to(longlink).permanent()
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
Redirect::to("/err/404")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,11 +3,11 @@ use rand::seq::SliceRandom;
|
|||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use rusqlite::Connection;
|
use rusqlite::Connection;
|
||||||
|
|
||||||
pub fn get_longurl(shortlink: String, db: &Connection) -> String {
|
pub fn get_longurl(shortlink: String, db: &Connection) -> Option<String> {
|
||||||
if validate_link(&shortlink) {
|
if validate_link(&shortlink) {
|
||||||
database::find_url(shortlink.as_str(), db)
|
database::find_url(shortlink.as_str(), db)
|
||||||
} else {
|
} else {
|
||||||
String::new()
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,7 +35,7 @@ pub fn add_link(req: String, db: &Connection) -> (bool, String) {
|
|||||||
shortlink = random_name();
|
shortlink = random_name();
|
||||||
}
|
}
|
||||||
|
|
||||||
if validate_link(shortlink.as_str()) && get_longurl(shortlink.clone(), db).is_empty() {
|
if validate_link(shortlink.as_str()) && get_longurl(shortlink.clone(), db).is_none() {
|
||||||
(
|
(
|
||||||
database::add_link(shortlink.clone(), longlink, db),
|
database::add_link(shortlink.clone(), longlink, db),
|
||||||
shortlink,
|
shortlink,
|
||||||
|
Loading…
Reference in New Issue
Block a user