[ENG-1540] Remove localStorage if no libraries are created (#1945)

* feat: clear localstorage on start if no libraries are found

* chore: add a check to see if the node is for desktop or server use

this will prevent desktop/app localstorage from being cleared if it's a server being hosted

* fix: add secondary localstorage directory for macos

for some reason the app/onboarding progress is kept in `Library/WebKit/Spacedrive` (at least in dev?) so it also needs clearing

* feat: delete cache directories also

* chore: iterate over paths

* feat: add support for windows `localStorage` by using `data_local_dir`

* docs/style: better comments and logs

* feat: remove both `AppData` local and roaming on Windows

* fix: mobile builds

* chore: tweak error message for windows
This commit is contained in:
jake
2024-01-29 12:32:48 +00:00
committed by GitHub
parent ffe86a0147
commit 3398b2cbcd
6 changed files with 55 additions and 2 deletions

View File

@@ -182,7 +182,7 @@ async fn main() -> tauri::Result<()> {
let (_guard, result) = match Node::init_logger(&data_dir) {
Ok(guard) => (
Some(guard),
Node::new(data_dir, sd_core::Env::new(CLIENT_ID)).await,
Node::new(data_dir, sd_core::Env::new(CLIENT_ID), true).await,
),
Err(err) => (None, Err(NodeError::Logger(err))),
};

View File

@@ -75,7 +75,7 @@ pub fn handle_core_msg(
let _guard = Node::init_logger(&data_dir);
// TODO: probably don't unwrap
let new_node = Node::new(data_dir, sd_core::Env::new(CLIENT_ID))
let new_node = Node::new(data_dir, sd_core::Env::new(CLIENT_ID), false)
.await
.unwrap();
node.replace(new_node.clone());

View File

@@ -47,6 +47,7 @@ async fn main() {
client_id: std::env::var("SD_CLIENT_ID")
.unwrap_or_else(|_| "04701823-a498-406e-aef9-22081c1dae34".to_string()),
},
false,
)
.await
{

View File

@@ -4,6 +4,7 @@ use crate::{
api::{CoreEvent, Router},
location::LocationManagerError,
object::media::thumbnail::actor::Thumbnailer,
util::clear_localstorage::clear_localstorage,
};
#[cfg(feature = "ai")]
@@ -82,6 +83,7 @@ impl Node {
pub async fn new(
data_dir: impl AsRef<Path>,
env: env::Env,
desktop: bool,
) -> Result<(Arc<Node>, Arc<Router>), NodeError> {
let data_dir = data_dir.as_ref();
@@ -108,6 +110,11 @@ impl Node {
let (locations, locations_actor) = location::Locations::new();
let (jobs, jobs_actor) = job::Jobs::new();
let libraries = library::Libraries::new(data_dir.join("libraries")).await?;
if desktop && libraries.get_all().await.is_empty() {
clear_localstorage().await;
}
let (p2p, p2p_actor) = p2p::P2PManager::new(config.clone(), libraries.clone()).await?;
let node = Arc::new(Node {
data_dir: data_dir.to_path_buf(),

View File

@@ -0,0 +1,44 @@
use directories::BaseDirs;
use tokio::fs;
use tracing::{info, warn};
#[cfg(target_os = "linux")]
const EXTRA_DIRS: [&str; 1] = [".cache/spacedrive"];
#[cfg(target_os = "macos")]
const EXTRA_DIRS: [&str; 2] = ["Library/WebKit/Spacedrive", "Library/Caches/Spacedrive"];
pub async fn clear_localstorage() {
if let Some(base_dir) = BaseDirs::new() {
let data_dir = base_dir.data_dir().join("com.spacedrive.desktop"); // maybe tie this into something static?
fs::remove_dir_all(&data_dir)
.await
.map_err(|_| warn!("Unable to delete the `localStorage` primary directory."))
.ok();
// Windows needs both AppData/Local and AppData/Roaming clearing as it stores data in both
#[cfg(target_os = "windows")]
fs::remove_dir_all(&base_dir.data_local_dir().join("com.spacedrive.desktop"))
.await
.map_err(|_| warn!("Unable to delete the `localStorage` directory in Local AppData."))
.ok();
info!("Deleted {}", data_dir.display());
let home_dir = base_dir.home_dir();
#[cfg(any(target_os = "linux", target_os = "macos"))]
for path in EXTRA_DIRS {
fs::remove_dir_all(home_dir.join(path))
.await
.map_err(|_| warn!("Unable to delete a `localStorage` cache: {path}"))
.ok();
info!("Deleted {path}");
}
info!("Successfully wiped `localStorage` and related caches.")
} else {
warn!("Unable to source `BaseDirs` in order to clear `localStorage`.")
}
}

View File

@@ -1,5 +1,6 @@
mod abort_on_drop;
mod batched_stream;
pub mod clear_localstorage;
#[cfg(debug_assertions)]
pub mod debug_initializer;
mod infallible_request;