forked from szabodanika/microbin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pasta.rs
254 lines (221 loc) · 7.46 KB
/
pasta.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
use bytesize::ByteSize;
use chrono::{Datelike, Local, TimeZone, Timelike};
use serde::{Deserialize, Serialize};
use std::fmt;
use std::path::Path;
use std::time::{SystemTime, UNIX_EPOCH};
use crate::args::ARGS;
use crate::util::animalnumbers::to_animal_names;
use crate::util::hashids::to_hashids;
use crate::util::syntaxhighlighter::html_highlight;
#[derive(Serialize, Deserialize, PartialEq, Debug, Eq)]
pub struct PastaFile {
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
}
pub fn is_image(&self) -> bool {
let lowercase_name = self.name.to_lowercase();
let extensions = [
".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp", ".ico", ".svg", ".tiff", ".tif",
".jfif", ".pjpeg", ".pjp", ".avif", ".jxl", ".heif",
];
extensions.iter().any(|&ext| lowercase_name.ends_with(ext))
}
pub fn is_video(&self) -> bool {
let lowercase_name = self.name.to_lowercase();
let extensions = [
".mp4", ".mov", ".wmv", ".webm", ".avi", ".flv", ".mkv", ".mts",
];
extensions.iter().any(|&ext| lowercase_name.ends_with(ext))
}
pub fn embeddable(&self) -> bool {
self.is_image() || self.is_video()
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Pasta {
pub id: u64,
pub content: String,
pub file: Option<PastaFile>,
pub custom_alias: Option<String>,
pub extension: String,
pub private: bool,
pub readonly: bool,
pub editable: bool,
pub encrypt_server: bool,
pub encrypt_client: bool,
pub encrypted_key: Option<String>,
pub created: i64,
pub expiration: i64,
pub last_read: i64,
pub read_count: u64,
pub burn_after_reads: u64,
pub pasta_type: String,
}
impl Pasta {
pub fn id_as_animals(&self) -> String {
self.custom_alias.clone().unwrap_or(if ARGS.hash_ids {
to_hashids(self.id)
} else {
to_animal_names(self.id)
})
}
pub fn has_file(&self) -> bool {
self.file.is_some()
}
pub fn total_size_as_string(&self) -> String {
let total_size_bytes = if self.has_file() {
self.file.as_ref().unwrap().size.as_u64() as usize + self.content.as_bytes().len()
} else {
self.content.as_bytes().len()
};
if total_size_bytes < 1024 {
format!("{} B", total_size_bytes)
} else if total_size_bytes < 1024 * 1024 {
format!("{} KB", total_size_bytes / 1024)
} else if total_size_bytes < 1024 * 1024 * 1024 {
format!("{} MB", total_size_bytes / (1024 * 1024))
} else {
format!("{} GB", total_size_bytes / (1024 * 1024 * 1024))
}
}
pub fn file_embeddable(&self) -> bool {
return self.has_file()
&& self.file.as_ref().unwrap().embeddable()
&& !(self.encrypt_server || self.encrypt_client);
}
pub fn created_as_string(&self) -> String {
let date = Local.timestamp(self.created, 0);
format!(
"{:02}-{:02} {:02}:{:02}",
date.month(),
date.day(),
date.hour(),
date.minute(),
)
}
pub fn expiration_as_string(&self) -> String {
if self.expiration == 0 {
String::from("Never")
} else {
let date = Local.timestamp(self.expiration, 0);
format!(
"{:02}-{:02} {:02}:{:02}",
date.month(),
date.day(),
date.hour(),
date.minute(),
)
}
}
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?????
String::from("just now")
}
pub fn short_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!("{} d 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!("{} h 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!("{} m 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!("{} s ago", seconds);
};
// it's less than 1 second?????
String::from("just now")
}
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
((timenow - self.last_read) / 86400) as u16
}
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")
}
pub fn content_escaped(&self) -> String {
html_escape::encode_text(
&self
.content
.replace('\\', "\\\\")
.replace('`', "\\`")
.replace('$', "\\$"),
)
.to_string()
}
}
impl fmt::Display for Pasta {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.content)
}
}