| 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 |
#![allow(clippy::needless_pass_by_value)] |
| 82 |
|
| 83 |
use goingson_core::ImportOptions; |
| 84 |
use makeover_layout::Tone; |
| 85 |
use quasi_declare::declare; |
| 86 |
use quasi_router::screen::{Accepted, Choice}; |
| 87 |
use quasi_router::{Action, Node, Response, RouteError, Router}; |
| 88 |
|
| 89 |
use crate::backup_scheduler::backup_dir; |
| 90 |
use crate::commands::export::{ |
| 91 |
BackupInfoResponse, RestoreOptions, export_events_ics_bytes, export_json_bytes, |
| 92 |
export_tasks_csv_bytes, list_backups_in, |
| 93 |
}; |
| 94 |
use crate::commands::import_external::DuplicateStrategy; |
| 95 |
use crate::state::{AppState, BackupRun, DESKTOP_USER_ID}; |
| 96 |
|
| 97 |
#[cfg(test)] |
| 98 |
mod tests; |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
const PREVIEW_ROWS: usize = 25; |
| 105 |
|
| 106 |
|
| 107 |
const PREVIEW: &str = "data-preview"; |
| 108 |
|
| 109 |
|
| 110 |
const BACKUPS: &str = "data-backups"; |
| 111 |
|
| 112 |
|
| 113 |
const AUTOMATIC: &str = "data-automatic"; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
fn picked(request: &quasi_router::Request) -> Result<String, RouteError> { |
| 122 |
let path = request.payload.get("file").unwrap_or_default().trim(); |
| 123 |
if path.is_empty() { |
| 124 |
return Err(RouteError::not_found("no file was picked")); |
| 125 |
} |
| 126 |
Ok(path.to_owned()) |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
#[derive(Clone, Copy, PartialEq, Eq)] |
| 131 |
enum Kind { |
| 132 |
|
| 133 |
Csv, |
| 134 |
|
| 135 |
Contacts, |
| 136 |
|
| 137 |
Calendar, |
| 138 |
} |
| 139 |
|
| 140 |
impl Kind { |
| 141 |
|
| 142 |
const EVERY: [Self; 3] = [Self::Csv, Self::Contacts, Self::Calendar]; |
| 143 |
|
| 144 |
|
| 145 |
fn of(slug: &str) -> Result<Self, RouteError> { |
| 146 |
match slug { |
| 147 |
"csv" => Ok(Self::Csv), |
| 148 |
"contacts" => Ok(Self::Contacts), |
| 149 |
"calendar" => Ok(Self::Calendar), |
| 150 |
_ => Err(RouteError::not_found("nothing imports that")), |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
const fn slug(self) -> &'static str { |
| 156 |
match self { |
| 157 |
Self::Csv => "csv", |
| 158 |
Self::Contacts => "contacts", |
| 159 |
Self::Calendar => "calendar", |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
const fn label(self) -> &'static str { |
| 165 |
match self { |
| 166 |
Self::Csv => "CSV or TSV file", |
| 167 |
Self::Contacts => "vCard file", |
| 168 |
Self::Calendar => "iCalendar file", |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
const fn accept(self) -> &'static str { |
| 179 |
match self { |
| 180 |
Self::Csv => "csv,tsv", |
| 181 |
Self::Contacts => "vcf,vcard", |
| 182 |
Self::Calendar => "ics,ical", |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
const fn hint(self) -> &'static str { |
| 189 |
match self { |
| 190 |
Self::Csv => { |
| 191 |
"Columns are matched by name: description, due, priority, project and tags for \ |
| 192 |
tasks; start and end for events; name and type for projects. The kind is read \ |
| 193 |
from the header." |
| 194 |
} |
| 195 |
Self::Contacts => "Cards already here are matched by email address.", |
| 196 |
Self::Calendar => "Events already here are matched by their UID.", |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
fn from(request: &quasi_router::Request) -> Result<Self, RouteError> { |
| 202 |
Self::of( |
| 203 |
request |
| 204 |
.captures |
| 205 |
.get("kind") |
| 206 |
.ok_or_else(|| RouteError::not_found("no kind"))?, |
| 207 |
) |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
declare! { |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
shape import_form(kind: Kind) -> Node; |
| 218 |
|
| 219 |
region "data-import-{kind.slug()}" as Group { |
| 220 |
act "Choose a {kind.label()}" |
| 221 |
to post "/data/import/{kind.slug()}/preview" |
| 222 |
with "accept" kind.accept() |
| 223 |
with "name" kind.label() |
| 224 |
by_host awaiting; |
| 225 |
|
| 226 |
text kind.hint(); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
declare! { |
| 231 |
|
| 232 |
shape import_region() -> Slot; |
| 233 |
|
| 234 |
region "data-import" as Pane { |
| 235 |
section "Import"; |
| 236 |
text "Nothing is created until the preview is confirmed."; |
| 237 |
|
| 238 |
for kind in Kind::EVERY { |
| 239 |
include import_form(kind); |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
fn short(value: &str) -> String { |
| 252 |
if value.chars().count() > 50 { |
| 253 |
let kept: String = value.chars().take(50).collect(); |
| 254 |
format!("{kept}...") |
| 255 |
} else { |
| 256 |
value.to_owned() |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
fn short_or_blank(value: Option<&String>) -> String { |
| 262 |
value.map_or_else(String::new, |value| short(value)) |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
struct Previewed<T> { |
| 272 |
|
| 273 |
kind: Kind, |
| 274 |
|
| 275 |
path: String, |
| 276 |
|
| 277 |
total: usize, |
| 278 |
|
| 279 |
shown: Vec<T>, |
| 280 |
} |
| 281 |
|
| 282 |
impl<T> Previewed<T> { |
| 283 |
|
| 284 |
fn counted(&self, singular: &str) -> String { |
| 285 |
if self.total == 1 { |
| 286 |
format!("1 {singular}") |
| 287 |
} else { |
| 288 |
format!("{} {singular}s", self.total) |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
fn commits(&self, singular: &str) -> String { |
| 294 |
format!("Import {}", self.counted(singular)) |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
fn clipped(&self) -> bool { |
| 299 |
self.total > PREVIEW_ROWS |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
fn clipping(&self) -> String { |
| 304 |
format!("Showing the first {PREVIEW_ROWS} of {}.", self.total) |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
declare! { |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
shape confirm_form(kind: Kind, path: &str, submit: &str, duplicates: usize) -> Node; |
| 321 |
|
| 322 |
form post "/data/import/{kind.slug()}" { |
| 323 |
submit submit; |
| 324 |
|
| 325 |
field Hidden "file" "File" { |
| 326 |
value path; |
| 327 |
} |
| 328 |
|
| 329 |
field Radio "duplicates" already_here(duplicates) when duplicates over 0 { |
| 330 |
option Choice::new( |
| 331 |
"merge", |
| 332 |
"Merge into the existing contact: fill blank fields, add new emails and \ |
| 333 |
phones, never overwrite" |
| 334 |
); |
| 335 |
option Choice::new("skip", "Skip them"); |
| 336 |
option Choice::new("importAsNew", "Import them as new contacts"); |
| 337 |
value "merge"; |
| 338 |
hint "One choice for the whole import."; |
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
fn already_here(duplicates: usize) -> String { |
| 345 |
if duplicates == 1 { |
| 346 |
"1 contact is already here".to_owned() |
| 347 |
} else { |
| 348 |
format!("{duplicates} contacts are already here") |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
declare! { |
| 353 |
|
| 354 |
|
| 355 |
shape no_preview() -> Node; |
| 356 |
|
| 357 |
region PREVIEW as Pane { |
| 358 |
empty "Pick a file above to see what importing it would do."; |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
struct CsvPreview { |
| 371 |
|
| 372 |
file: Previewed<()>, |
| 373 |
|
| 374 |
entity: goingson_core::ImportEntityType, |
| 375 |
|
| 376 |
tasks: Vec<goingson_core::ImportTaskData>, |
| 377 |
|
| 378 |
projects: Vec<goingson_core::ImportProjectData>, |
| 379 |
|
| 380 |
events: Vec<goingson_core::ImportEventData>, |
| 381 |
|
| 382 |
|
| 383 |
warnings: Vec<String>, |
| 384 |
} |
| 385 |
|
| 386 |
impl CsvPreview { |
| 387 |
|
| 388 |
const fn singular(&self) -> &'static str { |
| 389 |
match self.entity { |
| 390 |
goingson_core::ImportEntityType::Task => "task", |
| 391 |
goingson_core::ImportEntityType::Project => "project", |
| 392 |
goingson_core::ImportEntityType::Event => "event", |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
fn csv_parse(path: &str) -> Result<CsvPreview, RouteError> { |
| 403 |
use goingson_core::ImportItemData; |
| 404 |
|
| 405 |
let parsed = crate::commands::import::preview_csv_at(path, &ImportOptions::default()) |
| 406 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 407 |
|
| 408 |
let total = parsed.items.len(); |
| 409 |
let mut preview = CsvPreview { |
| 410 |
file: Previewed { |
| 411 |
kind: Kind::Csv, |
| 412 |
path: path.to_owned(), |
| 413 |
total, |
| 414 |
shown: Vec::new(), |
| 415 |
}, |
| 416 |
entity: parsed.entity_type, |
| 417 |
tasks: Vec::new(), |
| 418 |
projects: Vec::new(), |
| 419 |
events: Vec::new(), |
| 420 |
warnings: parsed.warnings, |
| 421 |
}; |
| 422 |
|
| 423 |
for item in parsed.items.into_iter().take(PREVIEW_ROWS) { |
| 424 |
match item.data { |
| 425 |
ImportItemData::Task(task) => preview.tasks.push(task), |
| 426 |
ImportItemData::Project(project) => preview.projects.push(project), |
| 427 |
ImportItemData::Event(event) => preview.events.push(event), |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
Ok(preview) |
| 432 |
} |
| 433 |
|
| 434 |
declare! { |
| 435 |
|
| 436 |
shape csv_task(task: &goingson_core::ImportTaskData) -> Row; |
| 437 |
|
| 438 |
cells { |
| 439 |
cell at "Description" short(&task.description); |
| 440 |
cell at "Project" short_or_blank(task.project_name.as_ref()); |
| 441 |
cell at "Priority" short_or_blank(task.priority.as_ref()); |
| 442 |
cell at "Due" short_or_blank(task.due.as_ref()); |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
declare! { |
| 447 |
|
| 448 |
shape csv_project(project: &goingson_core::ImportProjectData) -> Row; |
| 449 |
|
| 450 |
cells { |
| 451 |
cell at "Name" short(&project.name); |
| 452 |
cell at "Description" short_or_blank(project.description.as_ref()); |
| 453 |
cell at "Type" short_or_blank(project.project_type.as_ref()); |
| 454 |
cell at "Status" short_or_blank(project.status.as_ref()); |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
declare! { |
| 459 |
|
| 460 |
shape csv_event(event: &goingson_core::ImportEventData) -> Row; |
| 461 |
|
| 462 |
cells { |
| 463 |
cell at "Title" short(&event.title); |
| 464 |
cell at "Start" short(&event.start); |
| 465 |
cell at "End" short_or_blank(event.end.as_ref()); |
| 466 |
cell at "Location" short_or_blank(event.location.as_ref()); |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
declare! { |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
shape csv_preview(preview: &CsvPreview) -> Node; |
| 484 |
|
| 485 |
region PREVIEW as Pane { |
| 486 |
empty "No rows in that file." when preview.file.total is 0; |
| 487 |
|
| 488 |
section preview.file.counted(preview.singular()) when preview.file.total over 0; |
| 489 |
|
| 490 |
given preview.entity { |
| 491 |
goingson_core::ImportEntityType::Task -> table { |
| 492 |
column "Description"; |
| 493 |
column "Project"; |
| 494 |
column "Priority"; |
| 495 |
column "Due"; |
| 496 |
|
| 497 |
for task in preview.tasks.iter() { |
| 498 |
include csv_task(task); |
| 499 |
} |
| 500 |
} |
| 501 |
goingson_core::ImportEntityType::Project -> table { |
| 502 |
column "Name"; |
| 503 |
column "Description"; |
| 504 |
column "Type"; |
| 505 |
column "Status"; |
| 506 |
|
| 507 |
for project in preview.projects.iter() { |
| 508 |
include csv_project(project); |
| 509 |
} |
| 510 |
} |
| 511 |
otherwise -> table { |
| 512 |
column "Title"; |
| 513 |
column "Start"; |
| 514 |
column "End"; |
| 515 |
column "Location"; |
| 516 |
|
| 517 |
for event in preview.events.iter() { |
| 518 |
include csv_event(event); |
| 519 |
} |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
text preview.file.clipping() when preview.file.clipped(); |
| 524 |
|
| 525 |
include confirm_form( |
| 526 |
preview.file.kind, |
| 527 |
&preview.file.path, |
| 528 |
&preview.file.commits(preview.singular()), |
| 529 |
0 |
| 530 |
) when preview.file.total over 0; |
| 531 |
|
| 532 |
for warning in preview.warnings.iter() { |
| 533 |
banner Tone::Warning warning; |
| 534 |
} |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
struct ContactsPreview { |
| 540 |
|
| 541 |
file: Previewed<crate::commands::import_external::VCardPreview>, |
| 542 |
|
| 543 |
|
| 544 |
duplicates: usize, |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
fn contacts_parse(state: &AppState, path: &str) -> Result<ContactsPreview, RouteError> { |
| 549 |
let cards = crate::commands::import_external::preview_vcf_at(state, path) |
| 550 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 551 |
|
| 552 |
Ok(ContactsPreview { |
| 553 |
duplicates: cards |
| 554 |
.iter() |
| 555 |
.filter(|card| card.duplicate_of.is_some()) |
| 556 |
.count(), |
| 557 |
file: Previewed { |
| 558 |
kind: Kind::Contacts, |
| 559 |
path: path.to_owned(), |
| 560 |
total: cards.len(), |
| 561 |
shown: cards.into_iter().take(PREVIEW_ROWS).collect(), |
| 562 |
}, |
| 563 |
}) |
| 564 |
} |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
fn matching(card: &crate::commands::import_external::VCardPreview) -> String { |
| 571 |
card.duplicate_of |
| 572 |
.as_ref() |
| 573 |
.map_or_else(String::new, |existing| format!("Matches {existing}")) |
| 574 |
} |
| 575 |
|
| 576 |
declare! { |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
shape contact_row(card: &crate::commands::import_external::VCardPreview) -> Row; |
| 583 |
|
| 584 |
cells { |
| 585 |
cell at "Name" short(&card.display_name); |
| 586 |
cell at "Company" short_or_blank(card.company.as_ref()); |
| 587 |
cell at "Emails" "{card.email_count}"; |
| 588 |
cell at "Phones" "{card.phone_count}"; |
| 589 |
cell at "Status" matching(card); |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
declare! { |
| 594 |
|
| 595 |
shape contacts_preview(preview: &ContactsPreview) -> Node; |
| 596 |
|
| 597 |
region PREVIEW as Pane { |
| 598 |
empty "No contacts in that file." when preview.file.total is 0; |
| 599 |
|
| 600 |
section preview.file.counted("contact") when preview.file.total over 0; |
| 601 |
|
| 602 |
table { |
| 603 |
column "Name"; |
| 604 |
column "Company"; |
| 605 |
column "Emails"; |
| 606 |
column "Phones"; |
| 607 |
column "Status"; |
| 608 |
|
| 609 |
for card in preview.file.shown.iter() { |
| 610 |
include contact_row(card); |
| 611 |
} |
| 612 |
} |
| 613 |
|
| 614 |
text preview.file.clipping() when preview.file.clipped(); |
| 615 |
|
| 616 |
include confirm_form( |
| 617 |
preview.file.kind, |
| 618 |
&preview.file.path, |
| 619 |
&preview.file.commits("contact"), |
| 620 |
preview.duplicates |
| 621 |
) when preview.file.total over 0; |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
|
| 626 |
type CalendarPreview = Previewed<crate::commands::import_external::IcsPreview>; |
| 627 |
|
| 628 |
|
| 629 |
fn calendar_parse(path: &str) -> Result<CalendarPreview, RouteError> { |
| 630 |
let events = crate::commands::import_external::preview_ics_at(path) |
| 631 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 632 |
|
| 633 |
Ok(Previewed { |
| 634 |
kind: Kind::Calendar, |
| 635 |
path: path.to_owned(), |
| 636 |
total: events.len(), |
| 637 |
shown: events.into_iter().take(PREVIEW_ROWS).collect(), |
| 638 |
}) |
| 639 |
} |
| 640 |
|
| 641 |
declare! { |
| 642 |
|
| 643 |
shape calendar_row(event: &crate::commands::import_external::IcsPreview) -> Row; |
| 644 |
|
| 645 |
cells { |
| 646 |
cell at "Title" short(&event.title); |
| 647 |
cell at "Start" short(&event.start_time); |
| 648 |
cell at "Location" short_or_blank(event.location.as_ref()); |
| 649 |
cell at "Repeats" short(&event.recurrence); |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
declare! { |
| 654 |
|
| 655 |
shape calendar_preview(preview: &CalendarPreview) -> Node; |
| 656 |
|
| 657 |
region PREVIEW as Pane { |
| 658 |
empty "No events in that file." when preview.total is 0; |
| 659 |
|
| 660 |
section preview.counted("event") when preview.total over 0; |
| 661 |
|
| 662 |
table { |
| 663 |
column "Title"; |
| 664 |
column "Start"; |
| 665 |
column "Location"; |
| 666 |
column "Repeats"; |
| 667 |
|
| 668 |
for event in preview.shown.iter() { |
| 669 |
include calendar_row(event); |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
text preview.clipping() when preview.clipped(); |
| 674 |
|
| 675 |
include confirm_form( |
| 676 |
preview.kind, |
| 677 |
&preview.path, |
| 678 |
&preview.commits("event"), |
| 679 |
0 |
| 680 |
) when preview.total over 0; |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
|
| 685 |
fn preview(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 686 |
let kind = Kind::from(&request)?; |
| 687 |
let path = picked(&request)?; |
| 688 |
let node = match kind { |
| 689 |
Kind::Csv => csv_preview(&csv_parse(&path)?), |
| 690 |
Kind::Contacts => contacts_preview(&contacts_parse(state, &path)?), |
| 691 |
Kind::Calendar => calendar_preview(&calendar_parse(&path)?), |
| 692 |
}; |
| 693 |
Ok(Response::fragment(PREVIEW, node)) |
| 694 |
} |
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
fn import(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 703 |
let kind = Kind::from(&request)?; |
| 704 |
let path = picked(&request)?; |
| 705 |
|
| 706 |
let (message, tone) = match kind { |
| 707 |
Kind::Csv => { |
| 708 |
let done = crate::commands::import::execute_csv_at( |
| 709 |
state, |
| 710 |
&path, |
| 711 |
&ImportOptions::default(), |
| 712 |
&[], |
| 713 |
) |
| 714 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 715 |
let tone = if done.failed_count > 0 { |
| 716 |
Tone::Warning |
| 717 |
} else { |
| 718 |
Tone::Success |
| 719 |
}; |
| 720 |
let message = if done.failed_count > 0 { |
| 721 |
format!( |
| 722 |
"Imported {}. {} failed.", |
| 723 |
done.imported_count, done.failed_count |
| 724 |
) |
| 725 |
} else { |
| 726 |
format!("Imported {}.", done.imported_count) |
| 727 |
}; |
| 728 |
(message, tone) |
| 729 |
} |
| 730 |
Kind::Contacts => { |
| 731 |
let strategy = strategy(&request); |
| 732 |
let done = crate::commands::import_external::import_vcf_at(state, &path, strategy) |
| 733 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 734 |
(result_message(&done), result_tone(&done)) |
| 735 |
} |
| 736 |
Kind::Calendar => { |
| 737 |
let done = crate::commands::import_external::import_ics_at(state, &path) |
| 738 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 739 |
(result_message(&done), result_tone(&done)) |
| 740 |
} |
| 741 |
}; |
| 742 |
|
| 743 |
Ok(Response::fragment(PREVIEW, no_preview()).toast(tone, message)) |
| 744 |
} |
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
fn strategy(request: &quasi_router::Request) -> DuplicateStrategy { |
| 754 |
match request.payload.get("duplicates") { |
| 755 |
Some("skip") => DuplicateStrategy::Skip, |
| 756 |
Some("importAsNew") => DuplicateStrategy::ImportAsNew, |
| 757 |
_ => DuplicateStrategy::Merge, |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
|
| 762 |
fn result_message(done: &crate::commands::import_external::ImportResult) -> String { |
| 763 |
let mut parts = Vec::new(); |
| 764 |
if done.imported > 0 { |
| 765 |
parts.push(format!("{} imported", done.imported)); |
| 766 |
} |
| 767 |
if done.merged > 0 { |
| 768 |
parts.push(format!("{} merged", done.merged)); |
| 769 |
} |
| 770 |
if done.skipped > 0 { |
| 771 |
parts.push(format!("{} already here", done.skipped)); |
| 772 |
} |
| 773 |
if !done.errors.is_empty() { |
| 774 |
parts.push(format!("{} failed", done.errors.len())); |
| 775 |
} |
| 776 |
if parts.is_empty() { |
| 777 |
return "Nothing to import.".to_owned(); |
| 778 |
} |
| 779 |
format!("{}.", parts.join(", ")) |
| 780 |
} |
| 781 |
|
| 782 |
|
| 783 |
fn result_tone(done: &crate::commands::import_external::ImportResult) -> Tone { |
| 784 |
if done.errors.is_empty() { |
| 785 |
Tone::Success |
| 786 |
} else { |
| 787 |
Tone::Warning |
| 788 |
} |
| 789 |
} |
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
fn safe_name(request: &quasi_router::Request) -> Result<String, RouteError> { |
| 799 |
let name = request |
| 800 |
.captures |
| 801 |
.get("name") |
| 802 |
.ok_or_else(|| RouteError::not_found("no backup"))?; |
| 803 |
let bad = name.contains('/') || name.contains('\\') || name.contains(".."); |
| 804 |
if bad || !name.ends_with(".json.gz") { |
| 805 |
return Err(RouteError::not_found("not a backup")); |
| 806 |
} |
| 807 |
Ok(name.to_owned()) |
| 808 |
} |
| 809 |
|
| 810 |
|
| 811 |
fn size(bytes: u64) -> String { |
| 812 |
const STEP: f64 = 1024.0; |
| 813 |
let units = ["bytes", "KB", "MB", "GB"]; |
| 814 |
if bytes == 0 { |
| 815 |
return "0 bytes".to_owned(); |
| 816 |
} |
| 817 |
let mut value = bytes as f64; |
| 818 |
let mut unit = 0; |
| 819 |
while value >= STEP && unit < units.len() - 1 { |
| 820 |
value /= STEP; |
| 821 |
unit += 1; |
| 822 |
} |
| 823 |
if unit == 0 { |
| 824 |
format!("{bytes} bytes") |
| 825 |
} else { |
| 826 |
format!("{value:.1} {}", units[unit]) |
| 827 |
} |
| 828 |
} |
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
fn taken_at(created_at: i64) -> String { |
| 837 |
chrono::DateTime::from_timestamp(created_at, 0).map_or_else( |
| 838 |
|| "date unknown".to_owned(), |
| 839 |
|at| at.format("%Y-%m-%d %H:%M UTC").to_string(), |
| 840 |
) |
| 841 |
} |
| 842 |
|
| 843 |
declare! { |
| 844 |
|
| 845 |
shape backup_row(backup: &BackupInfoResponse) -> Row; |
| 846 |
|
| 847 |
row &backup.file_name { |
| 848 |
meta "{taken_at(backup.created_at)} · {size(backup.size_bytes)}"; |
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
act "Restore" to post "/data/backups/{backup.file_name}/restore" { |
| 853 |
confirm "Restore from this backup? Anything already here with the same id is \ |
| 854 |
left alone."; |
| 855 |
} |
| 856 |
|
| 857 |
act "Delete" to post "/data/backups/{backup.file_name}/delete" { |
| 858 |
tone Danger; |
| 859 |
confirm "Delete this backup? That cannot be undone."; |
| 860 |
} |
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
struct Backups { |
| 867 |
|
| 868 |
found: Vec<BackupInfoResponse>, |
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
running: bool, |
| 875 |
} |
| 876 |
|
| 877 |
|
| 878 |
fn backups(state: &AppState) -> Result<Backups, RouteError> { |
| 879 |
Ok(Backups { |
| 880 |
found: list_backups_in(state).map_err(|error| RouteError::internal(error.to_string()))?, |
| 881 |
running: state.backup_running(), |
| 882 |
}) |
| 883 |
} |
| 884 |
|
| 885 |
declare! { |
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
shape backups_region(backups: &Backups) -> Slot; |
| 902 |
|
| 903 |
region BACKUPS as Pane { |
| 904 |
fed_by Action::get("/data/backups"); |
| 905 |
live; |
| 906 |
|
| 907 |
section "Backups"; |
| 908 |
act "Create Backup" to post "/data/backups/create"; |
| 909 |
|
| 910 |
underway "Creating backup…" when backups.running; |
| 911 |
|
| 912 |
include backup_list(backups); |
| 913 |
} |
| 914 |
} |
| 915 |
|
| 916 |
declare! { |
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
shape backup_list(backups: &Backups) -> Node; |
| 924 |
|
| 925 |
given backups.found.is_empty() { |
| 926 |
true -> empty "No backups yet. Automatic backups start once they are enabled below."; |
| 927 |
otherwise -> list { |
| 928 |
for backup in backups.found.iter() { |
| 929 |
include backup_row(backup); |
| 930 |
} |
| 931 |
} |
| 932 |
} |
| 933 |
} |
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
fn listing(state: &AppState, _request: quasi_router::Request) -> Result<Response, RouteError> { |
| 941 |
let finished = state.take_finished_backup(); |
| 942 |
let answer = Response::fragment(BACKUPS, Node::Region(backups_region(&backups(state)?))); |
| 943 |
Ok(match finished { |
| 944 |
Some((true, said)) => answer.toast(Tone::Success, said), |
| 945 |
Some((false, said)) => answer.toast(Tone::Danger, said), |
| 946 |
None => answer, |
| 947 |
}) |
| 948 |
} |
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
fn create(state: &AppState, _request: quasi_router::Request) -> Result<Response, RouteError> { |
| 964 |
if state.backup_running() { |
| 965 |
return Ok( |
| 966 |
Response::fragment(BACKUPS, Node::Region(backups_region(&backups(state)?))) |
| 967 |
.toast(Tone::Warning, "A backup is already being written."), |
| 968 |
); |
| 969 |
} |
| 970 |
|
| 971 |
let Some(offload) = state.offload.get() else { |
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
return Err(RouteError::internal( |
| 976 |
"this host cannot run a backup in the background", |
| 977 |
)); |
| 978 |
}; |
| 979 |
|
| 980 |
state.set_backup_run(BackupRun::Running); |
| 981 |
|
| 982 |
let handed_off = offload.run(|state| async move { |
| 983 |
let outcome = crate::backup_scheduler::create_backup_now(&state).await; |
| 984 |
state.set_backup_run(match outcome { |
| 985 |
Ok(done) => BackupRun::Finished { |
| 986 |
ok: true, |
| 987 |
said: format!( |
| 988 |
"Backup created: {}.", |
| 989 |
std::path::Path::new(&done.file_path) |
| 990 |
.file_name() |
| 991 |
.map_or_else( |
| 992 |
|| done.file_path.clone(), |
| 993 |
|name| name.to_string_lossy().into_owned() |
| 994 |
) |
| 995 |
), |
| 996 |
}, |
| 997 |
Err(error) => BackupRun::Finished { |
| 998 |
ok: false, |
| 999 |
said: format!("Backup failed: {error}"), |
| 1000 |
}, |
| 1001 |
}); |
| 1002 |
}); |
| 1003 |
|
| 1004 |
if !handed_off { |
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
state.set_backup_run(BackupRun::Idle); |
| 1009 |
return Err(RouteError::internal("the app is shutting down")); |
| 1010 |
} |
| 1011 |
|
| 1012 |
Ok(Response::started(BACKUPS, "Creating backup…")) |
| 1013 |
} |
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
fn frequency_choices() -> Vec<Choice> { |
| 1020 |
vec![ |
| 1021 |
Choice::new("15", "Every 15 minutes (recommended)"), |
| 1022 |
Choice::new("30", "Every 30 minutes"), |
| 1023 |
Choice::new("60", "Every hour"), |
| 1024 |
Choice::new("360", "Every 6 hours"), |
| 1025 |
Choice::new("1440", "Daily"), |
| 1026 |
] |
| 1027 |
} |
| 1028 |
|
| 1029 |
|
| 1030 |
fn retention_choices() -> Vec<Choice> { |
| 1031 |
vec![ |
| 1032 |
Choice::new("1", "Keep 1 backup (recommended)"), |
| 1033 |
Choice::new("3", "Keep 3 backups"), |
| 1034 |
Choice::new("7", "Keep 7 backups"), |
| 1035 |
Choice::new("14", "Keep 14 backups"), |
| 1036 |
Choice::new("0", "Keep all backups"), |
| 1037 |
] |
| 1038 |
} |
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
struct Automatic { |
| 1046 |
|
| 1047 |
enabled: bool, |
| 1048 |
|
| 1049 |
frequency: i32, |
| 1050 |
|
| 1051 |
retention: i32, |
| 1052 |
|
| 1053 |
last: String, |
| 1054 |
} |
| 1055 |
|
| 1056 |
|
| 1057 |
fn automatic(state: &AppState) -> Result<Automatic, RouteError> { |
| 1058 |
let settings = state |
| 1059 |
.backup_settings |
| 1060 |
.get(DESKTOP_USER_ID) |
| 1061 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 1062 |
|
| 1063 |
Ok(Automatic { |
| 1064 |
enabled: settings.as_ref().is_none_or(|s| s.auto_backup_enabled), |
| 1065 |
frequency: settings.as_ref().map_or(15, |s| s.backup_frequency_minutes), |
| 1066 |
retention: settings.as_ref().map_or(1, |s| s.max_backups_to_keep), |
| 1067 |
last: settings |
| 1068 |
.as_ref() |
| 1069 |
.and_then(|s| s.last_backup_at) |
| 1070 |
.map_or_else( |
| 1071 |
|| "No backups yet.".to_owned(), |
| 1072 |
|at| format!("Last backup {}.", at.format("%Y-%m-%d %H:%M UTC")), |
| 1073 |
), |
| 1074 |
}) |
| 1075 |
} |
| 1076 |
|
| 1077 |
declare! { |
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
shape automatic_region(automatic: &Automatic) -> Slot; |
| 1090 |
|
| 1091 |
region AUTOMATIC as Pane { |
| 1092 |
section "Automatic backups"; |
| 1093 |
|
| 1094 |
form post "/data/backups/automatic" { |
| 1095 |
submit "Save"; |
| 1096 |
|
| 1097 |
field Checkbox "enabled" "Take backups automatically" { |
| 1098 |
hint "Compressed snapshots on a schedule, kept in the app's own directory."; |
| 1099 |
|
| 1100 |
|
| 1101 |
value "on" when automatic.enabled; |
| 1102 |
} |
| 1103 |
|
| 1104 |
field Select "frequency" "How often" { |
| 1105 |
options frequency_choices(); |
| 1106 |
value "{automatic.frequency}"; |
| 1107 |
extended; |
| 1108 |
} |
| 1109 |
|
| 1110 |
field Select "retention" "How many to keep" { |
| 1111 |
options retention_choices(); |
| 1112 |
value "{automatic.retention}"; |
| 1113 |
hint "Older backups are deleted to save space. Three are always kept."; |
| 1114 |
extended; |
| 1115 |
} |
| 1116 |
} |
| 1117 |
|
| 1118 |
text &automatic.last; |
| 1119 |
} |
| 1120 |
} |
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
#[derive(Clone, Copy, PartialEq, Eq)] |
| 1129 |
enum Export { |
| 1130 |
|
| 1131 |
Json, |
| 1132 |
|
| 1133 |
Tasks, |
| 1134 |
|
| 1135 |
Calendar, |
| 1136 |
} |
| 1137 |
|
| 1138 |
impl Export { |
| 1139 |
|
| 1140 |
const EVERY: [Self; 3] = [Self::Json, Self::Tasks, Self::Calendar]; |
| 1141 |
|
| 1142 |
|
| 1143 |
fn of(slug: &str) -> Result<Self, RouteError> { |
| 1144 |
match slug { |
| 1145 |
"json" => Ok(Self::Json), |
| 1146 |
"tasks" => Ok(Self::Tasks), |
| 1147 |
"calendar" => Ok(Self::Calendar), |
| 1148 |
_ => Err(RouteError::not_found("nothing exports that")), |
| 1149 |
} |
| 1150 |
} |
| 1151 |
|
| 1152 |
|
| 1153 |
fn from(request: &quasi_router::Request) -> Result<Self, RouteError> { |
| 1154 |
Self::of( |
| 1155 |
request |
| 1156 |
.captures |
| 1157 |
.get("format") |
| 1158 |
.ok_or_else(|| RouteError::not_found("no format"))?, |
| 1159 |
) |
| 1160 |
} |
| 1161 |
|
| 1162 |
|
| 1163 |
const fn slug(self) -> &'static str { |
| 1164 |
match self { |
| 1165 |
Self::Json => "json", |
| 1166 |
Self::Tasks => "tasks", |
| 1167 |
Self::Calendar => "calendar", |
| 1168 |
} |
| 1169 |
} |
| 1170 |
|
| 1171 |
|
| 1172 |
const fn label(self) -> &'static str { |
| 1173 |
match self { |
| 1174 |
Self::Json => "Export All (JSON)", |
| 1175 |
Self::Tasks => "Export Tasks (CSV)", |
| 1176 |
Self::Calendar => "Export Calendar (ICS)", |
| 1177 |
} |
| 1178 |
} |
| 1179 |
|
| 1180 |
|
| 1181 |
const fn stem(self) -> &'static str { |
| 1182 |
match self { |
| 1183 |
Self::Json => "export", |
| 1184 |
Self::Tasks => "tasks", |
| 1185 |
Self::Calendar => "calendar", |
| 1186 |
} |
| 1187 |
} |
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
fn kind(self) -> Accepted { |
| 1195 |
Accepted::media_type(match self { |
| 1196 |
Self::Json => "application/json", |
| 1197 |
Self::Tasks => "text/csv", |
| 1198 |
Self::Calendar => "text/calendar", |
| 1199 |
}) |
| 1200 |
} |
| 1201 |
|
| 1202 |
|
| 1203 |
const fn suffix(self) -> &'static str { |
| 1204 |
match self { |
| 1205 |
Self::Json => ".json", |
| 1206 |
Self::Tasks => ".csv", |
| 1207 |
Self::Calendar => ".ics", |
| 1208 |
} |
| 1209 |
} |
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
fn file_name(self) -> String { |
| 1216 |
format!( |
| 1217 |
"goingson-{}-{}{}", |
| 1218 |
self.stem(), |
| 1219 |
chrono::Local::now().format("%Y-%m-%d"), |
| 1220 |
self.suffix() |
| 1221 |
) |
| 1222 |
} |
| 1223 |
|
| 1224 |
|
| 1225 |
fn said(self, count: usize) -> String { |
| 1226 |
match self { |
| 1227 |
Self::Json => format!("Exported {count} items to JSON"), |
| 1228 |
Self::Tasks => format!("Exported {count} tasks to CSV"), |
| 1229 |
Self::Calendar => format!("Exported {count} events to ICS"), |
| 1230 |
} |
| 1231 |
} |
| 1232 |
} |
| 1233 |
|
| 1234 |
declare! { |
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
shape export_region() -> Slot; |
| 1245 |
|
| 1246 |
region "data-export" as Pane { |
| 1247 |
section "Export"; |
| 1248 |
text "Nothing here is removed by exporting it. Where the file lands is up to this \ |
| 1249 |
machine."; |
| 1250 |
|
| 1251 |
for export in Export::EVERY { |
| 1252 |
act export.label() to post "/data/export/{export.slug()}"; |
| 1253 |
} |
| 1254 |
} |
| 1255 |
} |
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
fn export(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1263 |
let export = Export::from(&request)?; |
| 1264 |
let done = match export { |
| 1265 |
Export::Json => export_json_bytes(state), |
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
Export::Tasks => export_tasks_csv_bytes(state, None), |
| 1271 |
Export::Calendar => export_events_ics_bytes(state, true), |
| 1272 |
} |
| 1273 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 1274 |
|
| 1275 |
let said = export.said(done.item_count); |
| 1276 |
Ok(Response::file(export.file_name(), export.kind(), done.bytes).toast(Tone::Success, said)) |
| 1277 |
} |
| 1278 |
|
| 1279 |
declare! { |
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
shape screen(backups: &Backups, automatic: &Automatic) -> Screen; |
| 1285 |
|
| 1286 |
screen list_detail "Import & Export" false { |
| 1287 |
at_place super::shell::SETTINGS; |
| 1288 |
|
| 1289 |
region "data-band" as Band { |
| 1290 |
page "Import & Export"; |
| 1291 |
} |
| 1292 |
|
| 1293 |
include import_region(); |
| 1294 |
|
| 1295 |
region PREVIEW as Pane { |
| 1296 |
include no_preview(); |
| 1297 |
} |
| 1298 |
|
| 1299 |
include export_region(); |
| 1300 |
include backups_region(backups); |
| 1301 |
include automatic_region(automatic); |
| 1302 |
} |
| 1303 |
} |
| 1304 |
|
| 1305 |
|
| 1306 |
fn index(state: &AppState, _request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1307 |
Ok(screen(&backups(state)?, &automatic(state)?).into()) |
| 1308 |
} |
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
fn restore(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1316 |
let name = safe_name(&request)?; |
| 1317 |
let path = backup_dir(state).join(&name); |
| 1318 |
if !path.exists() { |
| 1319 |
return Err(RouteError::not_found("no such backup")); |
| 1320 |
} |
| 1321 |
|
| 1322 |
let done = crate::commands::export::restore_backup_from( |
| 1323 |
state, |
| 1324 |
&path.to_string_lossy(), |
| 1325 |
&RestoreOptions { replace_all: false }, |
| 1326 |
) |
| 1327 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 1328 |
|
| 1329 |
let total = done.projects_restored |
| 1330 |
+ done.tasks_restored |
| 1331 |
+ done.events_restored |
| 1332 |
+ done.emails_restored |
| 1333 |
+ done.contacts_restored; |
| 1334 |
|
| 1335 |
Ok(Response::fragment(BACKUPS, backup_list(&backups(state)?)) |
| 1336 |
.toast(Tone::Success, format!("Restored {total} from {name}."))) |
| 1337 |
} |
| 1338 |
|
| 1339 |
|
| 1340 |
fn delete(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1341 |
let name = safe_name(&request)?; |
| 1342 |
let path = backup_dir(state).join(&name); |
| 1343 |
|
| 1344 |
let removed = crate::commands::export::delete_backup_at(state, &path.to_string_lossy()) |
| 1345 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
Ok( |
| 1350 |
Response::fragment(BACKUPS, backup_list(&backups(state)?)).toast( |
| 1351 |
if removed { |
| 1352 |
Tone::Success |
| 1353 |
} else { |
| 1354 |
Tone::Warning |
| 1355 |
}, |
| 1356 |
if removed { |
| 1357 |
format!("Deleted {name}.") |
| 1358 |
} else { |
| 1359 |
format!("{name} was already gone.") |
| 1360 |
}, |
| 1361 |
), |
| 1362 |
) |
| 1363 |
} |
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
fn save_automatic( |
| 1373 |
state: &AppState, |
| 1374 |
request: quasi_router::Request, |
| 1375 |
) -> Result<Response, RouteError> { |
| 1376 |
let number = |name: &str, fallback: i32| { |
| 1377 |
request |
| 1378 |
.payload |
| 1379 |
.get(name) |
| 1380 |
.and_then(|value| value.parse::<i32>().ok()) |
| 1381 |
.unwrap_or(fallback) |
| 1382 |
}; |
| 1383 |
|
| 1384 |
crate::commands::export::save_backup_settings_for( |
| 1385 |
state, |
| 1386 |
&crate::commands::export::BackupSettingsInput { |
| 1387 |
|
| 1388 |
|
| 1389 |
auto_backup_enabled: request.payload.get("enabled").is_some(), |
| 1390 |
backup_frequency_minutes: number("frequency", 15), |
| 1391 |
max_backups_to_keep: number("retention", 1), |
| 1392 |
}, |
| 1393 |
) |
| 1394 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 1395 |
|
| 1396 |
Ok(Response::fragment( |
| 1397 |
AUTOMATIC, |
| 1398 |
Node::Region(automatic_region(&automatic(state)?)), |
| 1399 |
) |
| 1400 |
.toast(Tone::Success, "Backup settings saved.")) |
| 1401 |
} |
| 1402 |
|
| 1403 |
|
| 1404 |
#[must_use] |
| 1405 |
pub fn routes(router: Router<AppState>) -> Router<AppState> { |
| 1406 |
router |
| 1407 |
.get("/data", index) |
| 1408 |
.post("/data/import/{kind}/preview", preview) |
| 1409 |
.post("/data/import/{kind}", import) |
| 1410 |
.post("/data/export/{format}", export) |
| 1411 |
.get("/data/backups", listing) |
| 1412 |
.post("/data/backups/create", create) |
| 1413 |
.post("/data/backups/{name}/restore", restore) |
| 1414 |
.post("/data/backups/{name}/delete", delete) |
| 1415 |
.post("/data/backups/automatic", save_automatic) |
| 1416 |
} |
| 1417 |
|