1
0
Fork 0
mirror of https://github.com/SinTan1729/chhoto-url synced 2024-10-16 13:27:03 -05:00

Fixes suggested by clippy

This commit is contained in:
Sayantan Santra 2023-04-26 14:40:54 -05:00
parent 2c34598faf
commit ff4801a476
4 changed files with 16 additions and 31 deletions

View file

@ -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,15 +23,12 @@ 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
token_text == "session-token" && time_now < token_time + 1209600 // There are 1209600 seconds in 14 days
}
} else {
false
}
}
}
}
pub fn gen_token() -> String {
let token_text = "session-token".to_string();

View file

@ -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();
}

View file

@ -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);

View file

@ -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,