mirror of
https://github.com/minoplhy/chhoto-url.git
synced 2024-11-22 09:16:46 +00:00
Fixes suggested by clippy
This commit is contained in:
parent
2c34598faf
commit
ff4801a476
@ -8,21 +8,12 @@ pub fn validate(session: Session) -> bool {
|
||||
}
|
||||
|
||||
let token = session.get::<String>("session-token");
|
||||
if token.is_err() {
|
||||
false
|
||||
} else if !check(token.unwrap()) {
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
token.is_ok() && check(token.unwrap())
|
||||
}
|
||||
|
||||
fn check(token: Option<String>) -> bool {
|
||||
if token.is_none() {
|
||||
false
|
||||
} else {
|
||||
let token_body = token.unwrap();
|
||||
let token_parts: Vec<&str> = token_body.split(";").collect();
|
||||
if let Some(token_body) = token {
|
||||
let token_parts: Vec<&str> = token_body.split(';').collect();
|
||||
if token_parts.len() < 2 {
|
||||
false
|
||||
} else {
|
||||
@ -32,13 +23,10 @@ fn check(token: Option<String>) -> bool {
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.expect("Time went backwards!")
|
||||
.as_secs();
|
||||
if token_text == "session-token" && time_now < token_time + 1209600 {
|
||||
// There are 1209600 seconds in 14 days
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
token_text == "session-token" && time_now < token_time + 1209600 // There are 1209600 seconds in 14 days
|
||||
}
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,7 @@ pub fn find_url(shortlink: &str, db: &Connection) -> String {
|
||||
.unwrap();
|
||||
|
||||
let links = statement
|
||||
.query_map([shortlink], |row| Ok(row.get("long_url")?))
|
||||
.query_map([shortlink], |row| row.get("long_url"))
|
||||
.unwrap();
|
||||
|
||||
let mut longlink = "".to_string();
|
||||
@ -33,7 +33,7 @@ pub fn getall(db: &Connection) -> Vec<String> {
|
||||
links
|
||||
}
|
||||
|
||||
pub fn add_hit(shortlink: &str, db: &Connection) -> () {
|
||||
pub fn add_hit(shortlink: &str, db: &Connection) {
|
||||
db.execute(
|
||||
"UPDATE urls SET hits = hits + 1 WHERE short_url = ?1",
|
||||
[shortlink],
|
||||
@ -42,16 +42,14 @@ pub fn add_hit(shortlink: &str, db: &Connection) -> () {
|
||||
}
|
||||
|
||||
pub fn add_link(shortlink: String, longlink: String, db: &Connection) -> bool {
|
||||
match db.execute(
|
||||
db.execute(
|
||||
"INSERT INTO urls (long_url, short_url, hits) VALUES (?1, ?2, ?3)",
|
||||
(longlink, shortlink, 0),
|
||||
) {
|
||||
Ok(_) => true,
|
||||
Err(_) => false,
|
||||
}
|
||||
)
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
pub fn delete_link(shortlink: String, db: &Connection) -> () {
|
||||
pub fn delete_link(shortlink: String, db: &Connection) {
|
||||
db.execute("DELETE FROM urls WHERE short_url = ?1", [shortlink])
|
||||
.unwrap();
|
||||
}
|
||||
|
@ -1,5 +1,3 @@
|
||||
use std::env;
|
||||
|
||||
use actix_files::{Files, NamedFile};
|
||||
use actix_session::{storage::CookieSessionStore, Session, SessionMiddleware};
|
||||
use actix_web::{
|
||||
@ -9,6 +7,7 @@ use actix_web::{
|
||||
App, HttpResponse, HttpServer, Responder,
|
||||
};
|
||||
use rusqlite::Connection;
|
||||
use std::env;
|
||||
mod auth;
|
||||
mod database;
|
||||
mod utils;
|
||||
@ -67,7 +66,7 @@ async fn error404() -> impl Responder {
|
||||
async fn link_handler(shortlink: web::Path<String>, data: web::Data<AppState>) -> impl Responder {
|
||||
let shortlink_str = shortlink.to_string();
|
||||
let longlink = utils::get_longurl(shortlink_str, &data.db);
|
||||
if longlink == "".to_string() {
|
||||
if longlink == *"" {
|
||||
Redirect::to("/err/404")
|
||||
} else {
|
||||
database::add_hit(shortlink.as_str(), &data.db);
|
||||
|
@ -28,14 +28,14 @@ pub fn add_link(req: String, db: &Connection) -> (bool, String) {
|
||||
let mut shortlink;
|
||||
if chunks.len() > 1 {
|
||||
shortlink = chunks[1].to_string().to_lowercase();
|
||||
if shortlink == "".to_string() {
|
||||
if shortlink == *"" {
|
||||
shortlink = random_name();
|
||||
}
|
||||
} else {
|
||||
shortlink = random_name();
|
||||
}
|
||||
|
||||
if validate_link(shortlink.as_str()) && get_longurl(shortlink.clone(), db) == "".to_string() {
|
||||
if validate_link(shortlink.as_str()) && get_longurl(shortlink.clone(), db) == *"" {
|
||||
(
|
||||
database::add_link(shortlink.clone(), longlink, db),
|
||||
shortlink,
|
||||
|
Loading…
Reference in New Issue
Block a user