cleanup of various clippy lints, bad practices and simplificaiton of some code
Signed-off-by: Jade <obsidianical@gmail.com>
This commit is contained in:
parent
fa67edc8c5
commit
528a7b6899
11 changed files with 34 additions and 45 deletions
|
@ -37,7 +37,7 @@ pub async fn create(
|
||||||
.finish());
|
.finish());
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut pastas = data.pastas.lock().unwrap();
|
let mut pastas = data.pastas.lock().await;
|
||||||
|
|
||||||
let timenow: i64 = match SystemTime::now().duration_since(UNIX_EPOCH) {
|
let timenow: i64 = match SystemTime::now().duration_since(UNIX_EPOCH) {
|
||||||
Ok(n) => n.as_secs(),
|
Ok(n) => n.as_secs(),
|
||||||
|
@ -65,18 +65,15 @@ pub async fn create(
|
||||||
while let Some(mut field) = payload.try_next().await? {
|
while let Some(mut field) = payload.try_next().await? {
|
||||||
match field.name() {
|
match field.name() {
|
||||||
"editable" => {
|
"editable" => {
|
||||||
// while let Some(_chunk) = field.try_next().await? {}
|
|
||||||
new_pasta.editable = true;
|
new_pasta.editable = true;
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
"private" => {
|
"private" => {
|
||||||
// while let Some(_chunk) = field.try_next().await? {}
|
|
||||||
new_pasta.private = true;
|
new_pasta.private = true;
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
"expiration" => {
|
"expiration" => {
|
||||||
while let Some(chunk) = field.try_next().await? {
|
while let Some(chunk) = field.try_next().await? {
|
||||||
new_pasta.expiration = match std::str::from_utf8(&chunk).unwrap() {
|
new_pasta.expiration = match std::str::from_utf8(&chunk).unwrap() {
|
||||||
|
// TODO: customizable times
|
||||||
"1min" => timenow + 60,
|
"1min" => timenow + 60,
|
||||||
"10min" => timenow + 60 * 10,
|
"10min" => timenow + 60 * 10,
|
||||||
"1hour" => timenow + 60 * 60,
|
"1hour" => timenow + 60 * 60,
|
||||||
|
@ -96,12 +93,12 @@ pub async fn create(
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
"burn_after" => {
|
"burn_after" => {
|
||||||
while let Some(chunk) = field.try_next().await? {
|
while let Some(chunk) = field.try_next().await? {
|
||||||
new_pasta.burn_after_reads = match std::str::from_utf8(&chunk).unwrap() {
|
new_pasta.burn_after_reads = match std::str::from_utf8(&chunk).unwrap() {
|
||||||
|
// TODO: also make customizable
|
||||||
|
// maybe options in config files, with defaults
|
||||||
// give an extra read because the user will be redirected to the pasta page automatically
|
// give an extra read because the user will be redirected to the pasta page automatically
|
||||||
"1" => 2,
|
"1" => 2,
|
||||||
"10" => 10,
|
"10" => 10,
|
||||||
|
@ -115,8 +112,6 @@ pub async fn create(
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
"content" => {
|
"content" => {
|
||||||
let mut content = String::from("");
|
let mut content = String::from("");
|
||||||
|
@ -132,13 +127,11 @@ pub async fn create(
|
||||||
String::from("text")
|
String::from("text")
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
"syntax-highlight" => {
|
"syntax-highlight" => {
|
||||||
while let Some(chunk) = field.try_next().await? {
|
while let Some(chunk) = field.try_next().await? {
|
||||||
new_pasta.extension = std::str::from_utf8(&chunk).unwrap().to_string();
|
new_pasta.extension = std::str::from_utf8(&chunk).unwrap().to_string();
|
||||||
}
|
}
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
"file" => {
|
"file" => {
|
||||||
if ARGS.no_file_upload {
|
if ARGS.no_file_upload {
|
||||||
|
|
|
@ -19,7 +19,7 @@ struct EditTemplate<'a> {
|
||||||
|
|
||||||
#[get("/edit/{id}")]
|
#[get("/edit/{id}")]
|
||||||
pub async fn get_edit(data: web::Data<AppState>, id: web::Path<String>) -> HttpResponse {
|
pub async fn get_edit(data: web::Data<AppState>, id: web::Path<String>) -> HttpResponse {
|
||||||
let mut pastas = data.pastas.lock().unwrap();
|
let mut pastas = data.pastas.lock().await;
|
||||||
|
|
||||||
let id = if ARGS.hash_ids {
|
let id = if ARGS.hash_ids {
|
||||||
hashid_to_u64(&id).unwrap_or(0)
|
hashid_to_u64(&id).unwrap_or(0)
|
||||||
|
@ -65,7 +65,7 @@ pub async fn post_edit(
|
||||||
to_u64(&id.into_inner()).unwrap_or(0)
|
to_u64(&id.into_inner()).unwrap_or(0)
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut pastas = data.pastas.lock().unwrap();
|
let mut pastas = data.pastas.lock().await;
|
||||||
|
|
||||||
remove_expired(&mut pastas);
|
remove_expired(&mut pastas);
|
||||||
|
|
||||||
|
|
|
@ -9,15 +9,15 @@ use askama::Template;
|
||||||
struct Info<'a> {
|
struct Info<'a> {
|
||||||
args: &'a Args,
|
args: &'a Args,
|
||||||
pastas: &'a Vec<Pasta>,
|
pastas: &'a Vec<Pasta>,
|
||||||
status: &'a String,
|
status: &'a str,
|
||||||
version_string: &'a String,
|
version_string: &'a str,
|
||||||
message: &'a String,
|
message: &'a str,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/info")]
|
#[get("/info")]
|
||||||
pub async fn info(data: web::Data<AppState>) -> HttpResponse {
|
pub async fn info(data: web::Data<AppState>) -> HttpResponse {
|
||||||
// get access to the pasta collection
|
// get access to the pasta collection
|
||||||
let pastas = data.pastas.lock().unwrap();
|
let pastas = data.pastas.lock().await;
|
||||||
|
|
||||||
// todo status report more sophisticated
|
// todo status report more sophisticated
|
||||||
let mut status = "OK";
|
let mut status = "OK";
|
||||||
|
@ -32,9 +32,9 @@ pub async fn info(data: web::Data<AppState>) -> HttpResponse {
|
||||||
Info {
|
Info {
|
||||||
args: &ARGS,
|
args: &ARGS,
|
||||||
pastas: &pastas,
|
pastas: &pastas,
|
||||||
status: &String::from(status),
|
status,
|
||||||
version_string: &String::from("1.2.0-20221107"),
|
version_string: env!("CARGO_PKG_VERSION"),
|
||||||
message: &String::from(message),
|
message
|
||||||
}
|
}
|
||||||
.render()
|
.render()
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
|
|
|
@ -21,7 +21,7 @@ struct PastaTemplate<'a> {
|
||||||
#[get("/pasta/{id}")]
|
#[get("/pasta/{id}")]
|
||||||
pub async fn getpasta(data: web::Data<AppState>, id: web::Path<String>) -> HttpResponse {
|
pub async fn getpasta(data: web::Data<AppState>, id: web::Path<String>) -> HttpResponse {
|
||||||
// get access to the pasta collection
|
// get access to the pasta collection
|
||||||
let mut pastas = data.pastas.lock().unwrap();
|
let mut pastas = data.pastas.lock().await;
|
||||||
|
|
||||||
let id = if ARGS.hash_ids {
|
let id = if ARGS.hash_ids {
|
||||||
hashid_to_u64(&id).unwrap_or(0)
|
hashid_to_u64(&id).unwrap_or(0)
|
||||||
|
@ -85,7 +85,7 @@ pub async fn getpasta(data: web::Data<AppState>, id: web::Path<String>) -> HttpR
|
||||||
#[get("/url/{id}")]
|
#[get("/url/{id}")]
|
||||||
pub async fn redirecturl(data: web::Data<AppState>, id: web::Path<String>) -> HttpResponse {
|
pub async fn redirecturl(data: web::Data<AppState>, id: web::Path<String>) -> HttpResponse {
|
||||||
// get access to the pasta collection
|
// get access to the pasta collection
|
||||||
let mut pastas = data.pastas.lock().unwrap();
|
let mut pastas = data.pastas.lock().await;
|
||||||
|
|
||||||
let id = if ARGS.hash_ids {
|
let id = if ARGS.hash_ids {
|
||||||
hashid_to_u64(&id).unwrap_or(0)
|
hashid_to_u64(&id).unwrap_or(0)
|
||||||
|
@ -152,7 +152,7 @@ pub async fn redirecturl(data: web::Data<AppState>, id: web::Path<String>) -> Ht
|
||||||
#[get("/raw/{id}")]
|
#[get("/raw/{id}")]
|
||||||
pub async fn getrawpasta(data: web::Data<AppState>, id: web::Path<String>) -> String {
|
pub async fn getrawpasta(data: web::Data<AppState>, id: web::Path<String>) -> String {
|
||||||
// get access to the pasta collection
|
// get access to the pasta collection
|
||||||
let mut pastas = data.pastas.lock().unwrap();
|
let mut pastas = data.pastas.lock().await;
|
||||||
|
|
||||||
let id = if ARGS.hash_ids {
|
let id = if ARGS.hash_ids {
|
||||||
hashid_to_u64(&id).unwrap_or(0)
|
hashid_to_u64(&id).unwrap_or(0)
|
||||||
|
|
|
@ -21,7 +21,7 @@ pub async fn list(data: web::Data<AppState>) -> HttpResponse {
|
||||||
.finish();
|
.finish();
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut pastas = data.pastas.lock().unwrap();
|
let mut pastas = data.pastas.lock().await;
|
||||||
|
|
||||||
remove_expired(&mut pastas);
|
remove_expired(&mut pastas);
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,7 @@ struct QRTemplate<'a> {
|
||||||
#[get("/qr/{id}")]
|
#[get("/qr/{id}")]
|
||||||
pub async fn getqr(data: web::Data<AppState>, id: web::Path<String>) -> HttpResponse {
|
pub async fn getqr(data: web::Data<AppState>, id: web::Path<String>) -> HttpResponse {
|
||||||
// get access to the pasta collection
|
// get access to the pasta collection
|
||||||
let mut pastas = data.pastas.lock().unwrap();
|
let mut pastas = data.pastas.lock().await;
|
||||||
|
|
||||||
let u64_id = if ARGS.hash_ids {
|
let u64_id = if ARGS.hash_ids {
|
||||||
hashid_to_u64(&id).unwrap_or(0)
|
hashid_to_u64(&id).unwrap_or(0)
|
||||||
|
|
|
@ -18,7 +18,7 @@ pub async fn remove(data: web::Data<AppState>, id: web::Path<String>) -> HttpRes
|
||||||
.finish();
|
.finish();
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut pastas = data.pastas.lock().unwrap();
|
let mut pastas = data.pastas.lock().await;
|
||||||
|
|
||||||
let id = if ARGS.hash_ids {
|
let id = if ARGS.hash_ids {
|
||||||
hashid_to_u64(&id).unwrap_or(0)
|
hashid_to_u64(&id).unwrap_or(0)
|
||||||
|
|
12
src/main.rs
12
src/main.rs
|
@ -11,10 +11,10 @@ use actix_web::{middleware, web, App, HttpServer};
|
||||||
use actix_web_httpauth::middleware::HttpAuthentication;
|
use actix_web_httpauth::middleware::HttpAuthentication;
|
||||||
use chrono::Local;
|
use chrono::Local;
|
||||||
use env_logger::Builder;
|
use env_logger::Builder;
|
||||||
|
use futures::lock::Mutex;
|
||||||
use log::LevelFilter;
|
use log::LevelFilter;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::sync::Mutex;
|
|
||||||
|
|
||||||
pub mod args;
|
pub mod args;
|
||||||
pub mod pasta;
|
pub mod pasta;
|
||||||
|
@ -68,14 +68,8 @@ async fn main() -> std::io::Result<()> {
|
||||||
match fs::create_dir_all("./pasta_data/public") {
|
match fs::create_dir_all("./pasta_data/public") {
|
||||||
Ok(dir) => dir,
|
Ok(dir) => dir,
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
log::error!(
|
log::error!("Couldn't create data directory ./pasta_data/public/: {error:?}");
|
||||||
"Couldn't create data directory ./pasta_data/public/: {:?}",
|
panic!("Couldn't create data directory ./pasta_data/public/: {error:?}");
|
||||||
error
|
|
||||||
);
|
|
||||||
panic!(
|
|
||||||
"Couldn't create data directory ./pasta_data/public/: {:?}",
|
|
||||||
error
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
10
src/pasta.rs
10
src/pasta.rs
|
@ -45,6 +45,8 @@ pub struct Pasta {
|
||||||
pub last_read: i64,
|
pub last_read: i64,
|
||||||
pub read_count: u64,
|
pub read_count: u64,
|
||||||
pub burn_after_reads: u64,
|
pub burn_after_reads: u64,
|
||||||
|
// what types can there be?
|
||||||
|
// `url`, `text`,
|
||||||
pub pasta_type: String,
|
pub pasta_type: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,25 +98,25 @@ impl Pasta {
|
||||||
// get seconds since last read and convert it to days
|
// get seconds since last read and convert it to days
|
||||||
let days = ((timenow - self.last_read) / 86400) as u16;
|
let days = ((timenow - self.last_read) / 86400) as u16;
|
||||||
if days > 1 {
|
if days > 1 {
|
||||||
return format!("{} days ago", days);
|
return format!("{days} days ago");
|
||||||
};
|
};
|
||||||
|
|
||||||
// it's less than 1 day, let's do hours then
|
// it's less than 1 day, let's do hours then
|
||||||
let hours = ((timenow - self.last_read) / 3600) as u16;
|
let hours = ((timenow - self.last_read) / 3600) as u16;
|
||||||
if hours > 1 {
|
if hours > 1 {
|
||||||
return format!("{} hours ago", hours);
|
return format!("{hours} hours ago");
|
||||||
};
|
};
|
||||||
|
|
||||||
// it's less than 1 hour, let's do minutes then
|
// it's less than 1 hour, let's do minutes then
|
||||||
let minutes = ((timenow - self.last_read) / 60) as u16;
|
let minutes = ((timenow - self.last_read) / 60) as u16;
|
||||||
if minutes > 1 {
|
if minutes > 1 {
|
||||||
return format!("{} minutes ago", minutes);
|
return format!("{minutes} minutes ago");
|
||||||
};
|
};
|
||||||
|
|
||||||
// it's less than 1 minute, let's do seconds then
|
// it's less than 1 minute, let's do seconds then
|
||||||
let seconds = (timenow - self.last_read) as u16;
|
let seconds = (timenow - self.last_read) as u16;
|
||||||
if seconds > 1 {
|
if seconds > 1 {
|
||||||
return format!("{} seconds ago", seconds);
|
return format!("{seconds} seconds ago");
|
||||||
};
|
};
|
||||||
|
|
||||||
// it's less than 1 second?????
|
// it's less than 1 second?????
|
||||||
|
|
|
@ -14,11 +14,11 @@ pub fn save_to_file(pasta_data: &Vec<Pasta>) {
|
||||||
serde_json::to_writer(writer, &pasta_data).expect("Failed to create JSON writer");
|
serde_json::to_writer(writer, &pasta_data).expect("Failed to create JSON writer");
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
log::info!("Database file {} not found!", DATABASE_PATH);
|
log::info!("Database file {DATABASE_PATH} not found!");
|
||||||
file = File::create(DATABASE_PATH);
|
file = File::create(DATABASE_PATH);
|
||||||
match file {
|
match file {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
log::info!("Database file {} created.", DATABASE_PATH);
|
log::info!("Database file {DATABASE_PATH} created.");
|
||||||
save_to_file(pasta_data);
|
save_to_file(pasta_data);
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
@ -27,7 +27,7 @@ pub fn save_to_file(pasta_data: &Vec<Pasta>) {
|
||||||
&DATABASE_PATH,
|
&DATABASE_PATH,
|
||||||
&err
|
&err
|
||||||
);
|
);
|
||||||
panic!("Failed to create database file {}: {}!", DATABASE_PATH, err)
|
panic!("Failed to create database file {DATABASE_PATH}: {err}!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,10 +46,10 @@ pub fn load_from_file() -> io::Result<Vec<Pasta>> {
|
||||||
Ok(data)
|
Ok(data)
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
log::info!("Database file {} not found!", DATABASE_PATH);
|
log::info!("Database file {DATABASE_PATH} not found!");
|
||||||
save_to_file(&Vec::<Pasta>::new());
|
save_to_file(&Vec::<Pasta>::new());
|
||||||
|
|
||||||
log::info!("Database file {} created.", DATABASE_PATH);
|
log::info!("Database file {DATABASE_PATH} created.");
|
||||||
load_from_file()
|
load_from_file()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,7 @@ pub fn html_highlight(text: &str, extension: &str) -> String {
|
||||||
|
|
||||||
let mut highlighted_content2: String = String::from("");
|
let mut highlighted_content2: String = String::from("");
|
||||||
for line in highlighted_content.lines() {
|
for line in highlighted_content.lines() {
|
||||||
highlighted_content2 += &*format!("<code-line>{}</code-line>\n", line);
|
highlighted_content2 += &*format!("<code-line>{line}</code-line>\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rewrite colours to ones that are compatible with water.css and both light/dark modes
|
// Rewrite colours to ones that are compatible with water.css and both light/dark modes
|
||||||
|
|
Loading…
Reference in a new issue