1
0
Fork 0
mirror of https://github.com/SinTan1729/chhoto-url synced 2024-12-26 07:38:36 -06: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"); let token = session.get::<String>("session-token");
if token.is_err() { token.is_ok() && check(token.unwrap())
false
} else if !check(token.unwrap()) {
false
} else {
true
}
} }
fn check(token: Option<String>) -> bool { fn check(token: Option<String>) -> bool {
if token.is_none() { if let Some(token_body) = token {
false let token_parts: Vec<&str> = token_body.split(';').collect();
} else {
let token_body = token.unwrap();
let token_parts: Vec<&str> = token_body.split(";").collect();
if token_parts.len() < 2 { if token_parts.len() < 2 {
false false
} else { } else {
@ -32,13 +23,10 @@ fn check(token: Option<String>) -> bool {
.duration_since(SystemTime::UNIX_EPOCH) .duration_since(SystemTime::UNIX_EPOCH)
.expect("Time went backwards!") .expect("Time went backwards!")
.as_secs(); .as_secs();
if token_text == "session-token" && time_now < token_time + 1209600 { token_text == "session-token" && time_now < token_time + 1209600 // There are 1209600 seconds in 14 days
// There are 1209600 seconds in 14 days
true
} else {
false
}
} }
} else {
false
} }
} }

View file

@ -6,7 +6,7 @@ pub fn find_url(shortlink: &str, db: &Connection) -> String {
.unwrap(); .unwrap();
let links = statement let links = statement
.query_map([shortlink], |row| Ok(row.get("long_url")?)) .query_map([shortlink], |row| row.get("long_url"))
.unwrap(); .unwrap();
let mut longlink = "".to_string(); let mut longlink = "".to_string();
@ -33,7 +33,7 @@ pub fn getall(db: &Connection) -> Vec<String> {
links links
} }
pub fn add_hit(shortlink: &str, db: &Connection) -> () { pub fn add_hit(shortlink: &str, db: &Connection) {
db.execute( db.execute(
"UPDATE urls SET hits = hits + 1 WHERE short_url = ?1", "UPDATE urls SET hits = hits + 1 WHERE short_url = ?1",
[shortlink], [shortlink],
@ -42,16 +42,14 @@ pub fn add_hit(shortlink: &str, db: &Connection) -> () {
} }
pub fn add_link(shortlink: String, longlink: String, db: &Connection) -> bool { 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)", "INSERT INTO urls (long_url, short_url, hits) VALUES (?1, ?2, ?3)",
(longlink, shortlink, 0), (longlink, shortlink, 0),
) { )
Ok(_) => true, .is_ok()
Err(_) => false,
}
} }
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]) db.execute("DELETE FROM urls WHERE short_url = ?1", [shortlink])
.unwrap(); .unwrap();
} }

View file

@ -1,5 +1,3 @@
use std::env;
use actix_files::{Files, NamedFile}; use actix_files::{Files, NamedFile};
use actix_session::{storage::CookieSessionStore, Session, SessionMiddleware}; use actix_session::{storage::CookieSessionStore, Session, SessionMiddleware};
use actix_web::{ use actix_web::{
@ -9,6 +7,7 @@ use actix_web::{
App, HttpResponse, HttpServer, Responder, App, HttpResponse, HttpServer, Responder,
}; };
use rusqlite::Connection; use rusqlite::Connection;
use std::env;
mod auth; mod auth;
mod database; mod database;
mod utils; 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 { 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); let longlink = utils::get_longurl(shortlink_str, &data.db);
if longlink == "".to_string() { if longlink == *"" {
Redirect::to("/err/404") Redirect::to("/err/404")
} else { } else {
database::add_hit(shortlink.as_str(), &data.db); 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; let mut shortlink;
if chunks.len() > 1 { if chunks.len() > 1 {
shortlink = chunks[1].to_string().to_lowercase(); shortlink = chunks[1].to_string().to_lowercase();
if shortlink == "".to_string() { if shortlink == *"" {
shortlink = random_name(); shortlink = random_name();
} }
} else { } else {
shortlink = random_name(); 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), database::add_link(shortlink.clone(), longlink, db),
shortlink, shortlink,