2022-07-13 21:50:10 +00:00
|
|
|
use bytesize::ByteSize;
|
2022-07-31 20:31:35 +00:00
|
|
|
use chrono::{Datelike, Local, TimeZone, Timelike};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::fmt;
|
|
|
|
use std::path::Path;
|
2022-10-29 11:11:55 +00:00
|
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
2022-05-02 15:53:10 +00:00
|
|
|
|
2022-11-01 13:15:13 +00:00
|
|
|
use crate::args::ARGS;
|
2022-06-03 16:24:34 +00:00
|
|
|
use crate::util::animalnumbers::to_animal_names;
|
2022-11-01 13:15:13 +00:00
|
|
|
use crate::util::hashids::to_hashids;
|
2022-06-03 16:24:34 +00:00
|
|
|
use crate::util::syntaxhighlighter::html_highlight;
|
2022-04-10 22:21:45 +00:00
|
|
|
|
2022-07-13 21:50:10 +00:00
|
|
|
#[derive(Serialize, Deserialize, PartialEq, Eq)]
|
|
|
|
pub struct PastaFile {
|
2022-07-31 20:31:35 +00:00
|
|
|
pub name: String,
|
|
|
|
pub size: ByteSize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PastaFile {
|
|
|
|
pub fn from_unsanitized(path: &str) -> Result<Self, &'static str> {
|
|
|
|
let path = Path::new(path);
|
|
|
|
let name = path.file_name().ok_or("Path did not contain a file name")?;
|
|
|
|
let name = name.to_string_lossy().replace(' ', "_");
|
|
|
|
Ok(Self {
|
|
|
|
name,
|
|
|
|
size: ByteSize::b(0),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn name(&self) -> &str {
|
|
|
|
&self.name
|
|
|
|
}
|
2022-07-13 21:50:10 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 15:53:10 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
2022-04-10 22:21:45 +00:00
|
|
|
pub struct Pasta {
|
2022-05-02 15:53:10 +00:00
|
|
|
pub id: u64,
|
|
|
|
pub content: String,
|
2022-07-13 21:50:10 +00:00
|
|
|
pub file: Option<PastaFile>,
|
2022-06-03 16:24:34 +00:00
|
|
|
pub extension: String,
|
|
|
|
pub private: bool,
|
|
|
|
pub editable: bool,
|
2022-05-02 15:53:10 +00:00
|
|
|
pub created: i64,
|
|
|
|
pub expiration: i64,
|
2022-10-29 11:11:55 +00:00
|
|
|
pub last_read: i64,
|
|
|
|
pub read_count: u64,
|
|
|
|
pub burn_after_reads: u64,
|
2022-05-02 15:53:10 +00:00
|
|
|
pub pasta_type: String,
|
2022-04-10 22:21:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Pasta {
|
2022-05-02 15:53:10 +00:00
|
|
|
pub fn id_as_animals(&self) -> String {
|
2022-11-01 13:15:13 +00:00
|
|
|
if ARGS.hash_ids {
|
|
|
|
to_hashids(self.id)
|
|
|
|
} else {
|
|
|
|
to_animal_names(self.id)
|
|
|
|
}
|
2022-05-02 15:53:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn created_as_string(&self) -> String {
|
2022-07-13 21:55:28 +00:00
|
|
|
let date = Local.timestamp(self.created, 0);
|
2022-05-02 15:53:10 +00:00
|
|
|
format!(
|
2022-07-13 21:54:48 +00:00
|
|
|
"{:02}-{:02} {:02}:{:02}",
|
2022-05-02 15:53:10 +00:00
|
|
|
date.month(),
|
|
|
|
date.day(),
|
|
|
|
date.hour(),
|
|
|
|
date.minute(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn expiration_as_string(&self) -> String {
|
2022-05-09 21:36:13 +00:00
|
|
|
if self.expiration == 0 {
|
|
|
|
String::from("Never")
|
|
|
|
} else {
|
2022-07-13 21:55:28 +00:00
|
|
|
let date = Local.timestamp(self.expiration, 0);
|
2022-05-09 21:36:13 +00:00
|
|
|
format!(
|
2022-07-13 21:54:48 +00:00
|
|
|
"{:02}-{:02} {:02}:{:02}",
|
2022-05-09 21:36:13 +00:00
|
|
|
date.month(),
|
|
|
|
date.day(),
|
|
|
|
date.hour(),
|
|
|
|
date.minute(),
|
|
|
|
)
|
|
|
|
}
|
2022-05-02 15:53:10 +00:00
|
|
|
}
|
2022-06-03 16:24:34 +00:00
|
|
|
|
2022-10-29 11:11:55 +00:00
|
|
|
pub fn last_read_time_ago_as_string(&self) -> String {
|
|
|
|
// get current unix time in seconds
|
|
|
|
let timenow: i64 = match SystemTime::now().duration_since(UNIX_EPOCH) {
|
|
|
|
Ok(n) => n.as_secs(),
|
|
|
|
Err(_) => {
|
|
|
|
log::error!("SystemTime before UNIX EPOCH!");
|
|
|
|
0
|
|
|
|
}
|
|
|
|
} as i64;
|
|
|
|
|
|
|
|
// get seconds since last read and convert it to days
|
|
|
|
let days = ((timenow - self.last_read) / 86400) as u16;
|
|
|
|
if days > 1 {
|
|
|
|
return format!("{} days ago", days);
|
|
|
|
};
|
|
|
|
|
|
|
|
// it's less than 1 day, let's do hours then
|
|
|
|
let hours = ((timenow - self.last_read) / 3600) as u16;
|
|
|
|
if hours > 1 {
|
|
|
|
return format!("{} hours ago", hours);
|
|
|
|
};
|
|
|
|
|
|
|
|
// it's less than 1 hour, let's do minutes then
|
|
|
|
let minutes = ((timenow - self.last_read) / 60) as u16;
|
|
|
|
if minutes > 1 {
|
|
|
|
return format!("{} minutes ago", minutes);
|
|
|
|
};
|
|
|
|
|
|
|
|
// it's less than 1 minute, let's do seconds then
|
|
|
|
let seconds = (timenow - self.last_read) as u16;
|
|
|
|
if seconds > 1 {
|
|
|
|
return format!("{} seconds ago", seconds);
|
|
|
|
};
|
|
|
|
|
|
|
|
// it's less than 1 second?????
|
2022-11-08 21:30:16 +00:00
|
|
|
String::from("just now")
|
2022-10-29 11:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn last_read_days_ago(&self) -> u16 {
|
|
|
|
// get current unix time in seconds
|
|
|
|
let timenow: i64 = match SystemTime::now().duration_since(UNIX_EPOCH) {
|
|
|
|
Ok(n) => n.as_secs(),
|
|
|
|
Err(_) => {
|
|
|
|
log::error!("SystemTime before UNIX EPOCH!");
|
|
|
|
0
|
|
|
|
}
|
|
|
|
} as i64;
|
|
|
|
|
|
|
|
// get seconds since last read and convert it to days
|
2022-11-08 21:30:16 +00:00
|
|
|
((timenow - self.last_read) / 86400) as u16
|
2022-10-29 11:11:55 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 16:24:34 +00:00
|
|
|
pub fn content_syntax_highlighted(&self) -> String {
|
|
|
|
html_highlight(&self.content, &self.extension)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn content_not_highlighted(&self) -> String {
|
|
|
|
html_highlight(&self.content, "txt")
|
|
|
|
}
|
2022-10-02 04:50:05 +00:00
|
|
|
|
|
|
|
pub fn content_escaped(&self) -> String {
|
2022-11-08 21:30:16 +00:00
|
|
|
self.content.replace('`', "\\`").replace('$', "\\$")
|
2022-10-02 04:50:05 +00:00
|
|
|
}
|
2022-04-10 22:21:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Pasta {
|
2022-05-02 15:53:10 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.content)
|
|
|
|
}
|
2022-04-10 22:21:45 +00:00
|
|
|
}
|