mirror of
https://github.com/minoplhy/chhoto-url.git
synced 2024-11-22 17:26:45 +00:00
chg: Use json while sending form for new url
This commit is contained in:
parent
6659452c51
commit
99b5298cd8
2
actix/Cargo.lock
generated
2
actix/Cargo.lock
generated
@ -485,6 +485,8 @@ dependencies = [
|
|||||||
"rand",
|
"rand",
|
||||||
"regex",
|
"regex",
|
||||||
"rusqlite",
|
"rusqlite",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -32,3 +32,5 @@ rand = "0.8.5"
|
|||||||
actix-session = { version = "0.9.0", features = ["cookie-session"] }
|
actix-session = { version = "0.9.0", features = ["cookie-session"] }
|
||||||
env_logger = "0.11.1"
|
env_logger = "0.11.1"
|
||||||
nanoid = "0.4.0"
|
nanoid = "0.4.0"
|
||||||
|
serde_json = "1.0.115"
|
||||||
|
serde = { version = "1.0.197", features = [ "derive" ] }
|
||||||
|
@ -5,6 +5,14 @@ use nanoid::nanoid;
|
|||||||
use rand::seq::SliceRandom;
|
use rand::seq::SliceRandom;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use rusqlite::Connection;
|
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<String> {
|
pub fn get_longurl(shortlink: String, db: &Connection) -> Option<String> {
|
||||||
if validate_link(&shortlink) {
|
if validate_link(&shortlink) {
|
||||||
@ -25,8 +33,7 @@ pub fn getall(db: &Connection) -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_link(req: String, db: &Connection) -> (bool, String) {
|
pub fn add_link(req: String, db: &Connection) -> (bool, String) {
|
||||||
let chunks: Vec<&str> = req.split(';').collect();
|
let mut chunks: Url = from_str(&req).unwrap();
|
||||||
let longlink = String::from(chunks[0]);
|
|
||||||
|
|
||||||
let style = env::var("slug_style").unwrap_or(String::from("Pair"));
|
let style = env::var("slug_style").unwrap_or(String::from("Pair"));
|
||||||
let len_str = env::var("slug_length").unwrap_or(String::from("8"));
|
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;
|
len = 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut shortlink;
|
if chunks.shortlink.is_empty() {
|
||||||
if chunks.len() > 1 {
|
chunks.shortlink = gen_link(style, len);
|
||||||
shortlink = chunks[1].to_string().to_lowercase();
|
|
||||||
if shortlink.is_empty() {
|
|
||||||
shortlink = gen_link(style, len);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
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),
|
database::add_link(chunks.shortlink.clone(), chunks.longlink, db),
|
||||||
shortlink,
|
chunks.shortlink,
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
(false, String::from("shortUrl not valid or already in use"))
|
(false, String::from("shortUrl not valid or already in use"))
|
||||||
|
@ -168,14 +168,19 @@ const TD = (s, u) => {
|
|||||||
|
|
||||||
const submitForm = () => {
|
const submitForm = () => {
|
||||||
const form = document.forms.namedItem("new-url-form");
|
const form = document.forms.namedItem("new-url-form");
|
||||||
const longUrl = form.elements["longUrl"];
|
const data ={
|
||||||
const shortUrl = form.elements["shortUrl"];
|
"longlink": form.elements["longUrl"].value,
|
||||||
|
"shortlink": form.elements["shortUrl"].value,
|
||||||
|
};
|
||||||
|
|
||||||
const url = prepSubdir("/api/new");
|
const url = prepSubdir("/api/new");
|
||||||
|
|
||||||
fetch(url, {
|
fetch(url, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: `${longUrl.value};${shortUrl.value}`
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify(data),
|
||||||
})
|
})
|
||||||
.then(res => {
|
.then(res => {
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
|
Loading…
Reference in New Issue
Block a user