2024-07-30 17:32:30 +08:00
|
|
|
use crate::local_ai::local_llm_resource::WatchDiskEvent;
|
|
|
|
use flowy_error::{FlowyError, FlowyResult};
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver};
|
2024-07-31 11:47:09 +08:00
|
|
|
use tracing::{error, trace};
|
2024-07-30 17:32:30 +08:00
|
|
|
|
2024-09-09 12:54:44 +08:00
|
|
|
#[cfg(any(target_os = "windows", target_os = "macos", target_os = "linux"))]
|
2024-11-15 11:12:27 +08:00
|
|
|
#[allow(dead_code)]
|
2024-07-30 17:32:30 +08:00
|
|
|
pub struct WatchContext {
|
|
|
|
watcher: notify::RecommendedWatcher,
|
|
|
|
pub path: PathBuf,
|
|
|
|
}
|
|
|
|
|
2024-09-09 12:54:44 +08:00
|
|
|
#[cfg(any(target_os = "windows", target_os = "macos", target_os = "linux"))]
|
2024-11-15 11:12:27 +08:00
|
|
|
#[allow(dead_code)]
|
2024-07-31 11:47:09 +08:00
|
|
|
pub fn watch_offline_app() -> FlowyResult<(WatchContext, UnboundedReceiver<WatchDiskEvent>)> {
|
2024-09-09 12:54:44 +08:00
|
|
|
use notify::{Event, Watcher};
|
|
|
|
|
2024-07-31 11:47:09 +08:00
|
|
|
let install_path = install_path().ok_or_else(|| {
|
|
|
|
FlowyError::internal().with_context("Unsupported platform for offline app watching")
|
|
|
|
})?;
|
2024-07-30 17:32:30 +08:00
|
|
|
let (tx, rx) = unbounded_channel();
|
2024-07-31 11:47:09 +08:00
|
|
|
let app_path = offline_app_path();
|
2024-07-30 17:32:30 +08:00
|
|
|
let mut watcher = notify::recommended_watcher(move |res: Result<Event, _>| match res {
|
2024-07-31 11:47:09 +08:00
|
|
|
Ok(event) => {
|
|
|
|
if event.paths.iter().any(|path| path == &app_path) {
|
|
|
|
trace!("watch event: {:?}", event);
|
|
|
|
match event.kind {
|
|
|
|
notify::EventKind::Create(_) => {
|
|
|
|
if let Err(err) = tx.send(WatchDiskEvent::Create) {
|
|
|
|
error!("watch send error: {:?}", err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
notify::EventKind::Remove(_) => {
|
|
|
|
if let Err(err) = tx.send(WatchDiskEvent::Remove) {
|
|
|
|
error!("watch send error: {:?}", err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
trace!("unhandle watch event: {:?}", event);
|
|
|
|
},
|
2024-07-30 17:32:30 +08:00
|
|
|
}
|
2024-07-31 11:47:09 +08:00
|
|
|
}
|
2024-07-30 17:32:30 +08:00
|
|
|
},
|
|
|
|
Err(e) => error!("watch error: {:?}", e),
|
|
|
|
})
|
|
|
|
.map_err(|err| FlowyError::internal().with_context(err))?;
|
|
|
|
watcher
|
2024-09-09 12:54:44 +08:00
|
|
|
.watch(&install_path, notify::RecursiveMode::NonRecursive)
|
2024-07-30 17:32:30 +08:00
|
|
|
.map_err(|err| FlowyError::internal().with_context(err))?;
|
|
|
|
|
2024-07-31 11:47:09 +08:00
|
|
|
Ok((
|
|
|
|
WatchContext {
|
|
|
|
watcher,
|
|
|
|
path: install_path,
|
|
|
|
},
|
|
|
|
rx,
|
|
|
|
))
|
2024-07-30 17:32:30 +08:00
|
|
|
}
|
2024-09-09 12:54:44 +08:00
|
|
|
|
|
|
|
#[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "linux")))]
|
|
|
|
pub(crate) fn install_path() -> Option<PathBuf> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(any(target_os = "windows", target_os = "macos", target_os = "linux"))]
|
|
|
|
pub(crate) fn install_path() -> Option<PathBuf> {
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
return None;
|
|
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
return Some(PathBuf::from("/usr/local/bin"));
|
|
|
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "linux")))]
|
|
|
|
pub(crate) fn offline_app_path() -> PathBuf {
|
|
|
|
PathBuf::new()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(any(target_os = "windows", target_os = "macos", target_os = "linux"))]
|
|
|
|
pub(crate) fn offline_app_path() -> PathBuf {
|
|
|
|
let offline_app = "appflowy_ai_plugin";
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
return PathBuf::from(format!("/usr/local/bin/{}", offline_app));
|
|
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
return PathBuf::from(format!("/usr/local/bin/{}", offline_app));
|
|
|
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
return PathBuf::from(format!("/usr/local/bin/{}", offline_app));
|
|
|
|
}
|