Add Backup and Restore functionality with improved UI, new translations, and enhanced error handling for better user experience
This commit is contained in:
parent
488f746683
commit
07d0bb6b4e
5
.changeset/shy-bulldogs-cheat.md
Normal file
5
.changeset/shy-bulldogs-cheat.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
'pastebar-app-ui': patch
|
||||
---
|
||||
|
||||
Added Backup and Restore database and images
|
@ -8,7 +8,7 @@ Restore Data: Restaurar Datos
|
||||
Restore from File...: Restaurar desde Archivo...
|
||||
Select backup file: Seleccionar archivo de copia de seguridad
|
||||
Available Backups: Copias de Seguridad Disponibles
|
||||
Total backup space {{size}}: 'Espacio total de copia s: {{size}}'
|
||||
Total backup space {{size}}: 'Espacio total de respaldo: {{size}}'
|
||||
No backups found: No se encontraron copias de seguridad
|
||||
Restore: Restaurar
|
||||
Delete: Eliminar
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import { invoke } from '@tauri-apps/api'
|
||||
import { open } from '@tauri-apps/api/dialog'
|
||||
import { type as getOsType } from '@tauri-apps/api/os'
|
||||
import { settingsStoreAtom, uiStoreAtom } from '~/store'
|
||||
import { useAtomValue } from 'jotai'
|
||||
import {
|
||||
@ -137,10 +138,16 @@ export default function BackupRestoreSettings() {
|
||||
const handleCreateBackup = async () => {
|
||||
setIsCreatingBackup(true)
|
||||
try {
|
||||
const backupPath = await invoke<string>('create_backup', {
|
||||
let backupPath = await invoke<string>('create_backup', {
|
||||
includeImages,
|
||||
})
|
||||
|
||||
// Normalize path for Windows display
|
||||
const osType = await getOsType()
|
||||
if (osType === 'Windows_NT' && backupPath.startsWith('\\\\?\\')) {
|
||||
backupPath = backupPath.substring(4)
|
||||
}
|
||||
|
||||
toast({
|
||||
id: 'backup-create-success',
|
||||
title: t('Backup created successfully', { ns: 'backuprestore' }),
|
||||
|
@ -1,13 +1,13 @@
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::io::Write;
|
||||
use chrono::{DateTime, Local};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use zip::{ZipWriter, ZipArchive};
|
||||
use zip::write::FileOptions;
|
||||
use std::fs;
|
||||
use std::io::Write;
|
||||
use std::io::{Read, Seek};
|
||||
use std::path::{Path, PathBuf};
|
||||
use zip::write::FileOptions;
|
||||
use zip::{ZipArchive, ZipWriter};
|
||||
|
||||
use crate::db::{get_data_dir, get_db_path, get_clip_images_dir, get_clipboard_images_dir};
|
||||
use crate::db::{get_clip_images_dir, get_clipboard_images_dir, get_data_dir, get_db_path};
|
||||
use crate::services::utils::debug_output;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
@ -26,7 +26,6 @@ pub struct BackupListResponse {
|
||||
pub total_size_formatted: String,
|
||||
}
|
||||
|
||||
|
||||
fn get_backup_filename() -> String {
|
||||
let now = Local::now();
|
||||
format!("pastebar-data-backup-{}.zip", now.format("%Y-%m-%d-%H-%M"))
|
||||
@ -120,8 +119,8 @@ pub async fn create_backup(include_images: bool) -> Result<String, String> {
|
||||
}
|
||||
|
||||
// Create zip file
|
||||
let file = fs::File::create(&backup_path)
|
||||
.map_err(|e| format!("Failed to create backup file: {}", e))?;
|
||||
let file =
|
||||
fs::File::create(&backup_path).map_err(|e| format!("Failed to create backup file: {}", e))?;
|
||||
let mut zip = ZipWriter::new(file);
|
||||
|
||||
let options = FileOptions::default()
|
||||
@ -129,20 +128,24 @@ pub async fn create_backup(include_images: bool) -> Result<String, String> {
|
||||
.unix_permissions(0o644);
|
||||
|
||||
// Add database file
|
||||
let mut db_file = fs::File::open(&db_path)
|
||||
.map_err(|e| format!("Failed to open database file: {}", e))?;
|
||||
let mut db_file =
|
||||
fs::File::open(&db_path).map_err(|e| format!("Failed to open database file: {}", e))?;
|
||||
let mut db_buffer = Vec::new();
|
||||
db_file.read_to_end(&mut db_buffer)
|
||||
db_file
|
||||
.read_to_end(&mut db_buffer)
|
||||
.map_err(|e| format!("Failed to read database file: {}", e))?;
|
||||
|
||||
// Get just the filename for the zip entry
|
||||
let db_filename = db_path.file_name()
|
||||
let db_filename = db_path
|
||||
.file_name()
|
||||
.and_then(|name| name.to_str())
|
||||
.unwrap_or("pastebar-db.data");
|
||||
|
||||
zip.start_file(db_filename, options)
|
||||
zip
|
||||
.start_file(db_filename, options)
|
||||
.map_err(|e| format!("Failed to start database file in zip: {}", e))?;
|
||||
zip.write_all(&db_buffer)
|
||||
zip
|
||||
.write_all(&db_buffer)
|
||||
.map_err(|e| format!("Failed to write database to zip: {}", e))?;
|
||||
|
||||
// Add image directories if requested
|
||||
@ -168,7 +171,8 @@ pub async fn create_backup(include_images: bool) -> Result<String, String> {
|
||||
}
|
||||
}
|
||||
|
||||
zip.finish()
|
||||
zip
|
||||
.finish()
|
||||
.map_err(|e| format!("Failed to finalize zip file: {}", e))?;
|
||||
|
||||
debug_output(|| {
|
||||
@ -202,8 +206,15 @@ pub async fn list_backups() -> Result<BackupListResponse, String> {
|
||||
{
|
||||
// Format: YYYY-MM-DD-HH-MM
|
||||
if let Ok(parsed_date) = DateTime::parse_from_str(
|
||||
&format!("{} +0000", date_part.replace('-', " ").replacen(' ', "-", 2).replacen(' ', "-", 1).replacen(' ', ":", 1)),
|
||||
"%Y-%m-%d-%H-%M %z"
|
||||
&format!(
|
||||
"{} +0000",
|
||||
date_part
|
||||
.replace('-', " ")
|
||||
.replacen(' ', "-", 2)
|
||||
.replacen(' ', "-", 1)
|
||||
.replacen(' ', ":", 1)
|
||||
),
|
||||
"%Y-%m-%d-%H-%M %z",
|
||||
) {
|
||||
parsed_date.format("%B %d, %Y at %I:%M %p").to_string()
|
||||
} else {
|
||||
@ -237,9 +248,11 @@ pub async fn list_backups() -> Result<BackupListResponse, String> {
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn restore_backup(backup_path: String, create_pre_restore_backup: bool) -> Result<String, String> {
|
||||
pub async fn restore_backup(
|
||||
backup_path: String,
|
||||
create_pre_restore_backup: bool,
|
||||
) -> Result<String, String> {
|
||||
debug_output(|| {
|
||||
println!("Restoring backup from: {}", backup_path);
|
||||
});
|
||||
@ -273,23 +286,24 @@ pub async fn restore_backup(backup_path: String, create_pre_restore_backup: bool
|
||||
}
|
||||
|
||||
// Open the backup zip file
|
||||
let file = fs::File::open(&backup_path)
|
||||
.map_err(|e| format!("Failed to open backup file: {}", e))?;
|
||||
let file =
|
||||
fs::File::open(&backup_path).map_err(|e| format!("Failed to open backup file: {}", e))?;
|
||||
|
||||
let mut archive = ZipArchive::new(file)
|
||||
.map_err(|e| format!("Failed to read backup file: {}", e))?;
|
||||
let mut archive =
|
||||
ZipArchive::new(file).map_err(|e| format!("Failed to read backup file: {}", e))?;
|
||||
|
||||
// Extract files
|
||||
for i in 0..archive.len() {
|
||||
let mut file = archive.by_index(i)
|
||||
let mut file = archive
|
||||
.by_index(i)
|
||||
.map_err(|e| format!("Failed to read file from backup: {}", e))?;
|
||||
|
||||
let outpath = data_dir.join(file.name());
|
||||
let sanitized_name = file.name().replace("..", "");
|
||||
let outpath = data_dir.join(sanitized_name);
|
||||
|
||||
if file.name().ends_with('/') {
|
||||
// Directory
|
||||
fs::create_dir_all(&outpath)
|
||||
.map_err(|e| format!("Failed to create directory: {}", e))?;
|
||||
fs::create_dir_all(&outpath).map_err(|e| format!("Failed to create directory: {}", e))?;
|
||||
} else {
|
||||
// File
|
||||
if let Some(parent) = outpath.parent() {
|
||||
@ -297,8 +311,8 @@ pub async fn restore_backup(backup_path: String, create_pre_restore_backup: bool
|
||||
.map_err(|e| format!("Failed to create parent directory: {}", e))?;
|
||||
}
|
||||
|
||||
let mut outfile = fs::File::create(&outpath)
|
||||
.map_err(|e| format!("Failed to create file: {}", e))?;
|
||||
let mut outfile =
|
||||
fs::File::create(&outpath).map_err(|e| format!("Failed to create file: {}", e))?;
|
||||
|
||||
std::io::copy(&mut file, &mut outfile)
|
||||
.map_err(|e| format!("Failed to extract file: {}", e))?;
|
||||
@ -330,8 +344,7 @@ pub async fn delete_backup(backup_path: String) -> Result<String, String> {
|
||||
return Err("Invalid file path".to_string());
|
||||
}
|
||||
|
||||
fs::remove_file(path)
|
||||
.map_err(|e| format!("Failed to delete backup file: {}", e))?;
|
||||
fs::remove_file(path).map_err(|e| format!("Failed to delete backup file: {}", e))?;
|
||||
|
||||
debug_output(|| {
|
||||
println!("Backup deleted successfully: {}", backup_path);
|
||||
|
Loading…
x
Reference in New Issue
Block a user