From 99b5298cd84d2c6d66ae274e270abfab6639440f Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Sun, 31 Mar 2024 15:17:54 -0500 Subject: [PATCH] chg: Use json while sending form for new url --- actix/Cargo.lock | 2 ++ actix/Cargo.toml | 2 ++ actix/src/utils.rs | 29 ++++++++++++++++------------- resources/static/script.js | 11 ++++++++--- 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/actix/Cargo.lock b/actix/Cargo.lock index fb9da0a..8fe58e6 100644 --- a/actix/Cargo.lock +++ b/actix/Cargo.lock @@ -485,6 +485,8 @@ dependencies = [ "rand", "regex", "rusqlite", + "serde", + "serde_json", ] [[package]] diff --git a/actix/Cargo.toml b/actix/Cargo.toml index 440ee82..763fb79 100644 --- a/actix/Cargo.toml +++ b/actix/Cargo.toml @@ -32,3 +32,5 @@ rand = "0.8.5" actix-session = { version = "0.9.0", features = ["cookie-session"] } env_logger = "0.11.1" nanoid = "0.4.0" +serde_json = "1.0.115" +serde = { version = "1.0.197", features = [ "derive" ] } diff --git a/actix/src/utils.rs b/actix/src/utils.rs index 1575596..c2d75aa 100644 --- a/actix/src/utils.rs +++ b/actix/src/utils.rs @@ -5,6 +5,14 @@ use nanoid::nanoid; use rand::seq::SliceRandom; use regex::Regex; use rusqlite::Connection; +use serde::Deserialize; +use serde_json::from_str; + +#[derive(Deserialize)] +struct Url { + shortlink: String, + longlink: String, +} pub fn get_longurl(shortlink: String, db: &Connection) -> Option { if validate_link(&shortlink) { @@ -25,8 +33,7 @@ pub fn getall(db: &Connection) -> String { } 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 chunks: Url = from_str(&req).unwrap(); let style = env::var("slug_style").unwrap_or(String::from("Pair")); let len_str = env::var("slug_length").unwrap_or(String::from("8")); @@ -35,20 +42,16 @@ pub fn add_link(req: String, db: &Connection) -> (bool, String) { len = 4; } - let mut shortlink; - if chunks.len() > 1 { - shortlink = chunks[1].to_string().to_lowercase(); - if shortlink.is_empty() { - shortlink = gen_link(style, len); - } - } else { - shortlink = gen_link(style, len); + if chunks.shortlink.is_empty() { + chunks.shortlink = gen_link(style, len); } - if validate_link(shortlink.as_str()) && get_longurl(shortlink.clone(), db).is_none() { + if validate_link(chunks.shortlink.as_str()) + && get_longurl(chunks.shortlink.clone(), db).is_none() + { ( - database::add_link(shortlink.clone(), longlink, db), - shortlink, + database::add_link(chunks.shortlink.clone(), chunks.longlink, db), + chunks.shortlink, ) } else { (false, String::from("shortUrl not valid or already in use")) diff --git a/resources/static/script.js b/resources/static/script.js index b7d19ca..3b939e0 100644 --- a/resources/static/script.js +++ b/resources/static/script.js @@ -168,14 +168,19 @@ const TD = (s, u) => { const submitForm = () => { const form = document.forms.namedItem("new-url-form"); - const longUrl = form.elements["longUrl"]; - const shortUrl = form.elements["shortUrl"]; + const data ={ + "longlink": form.elements["longUrl"].value, + "shortlink": form.elements["shortUrl"].value, + }; const url = prepSubdir("/api/new"); fetch(url, { method: "POST", - body: `${longUrl.value};${shortUrl.value}` + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(data), }) .then(res => { if (!res.ok) {