2023-11-17 15:38:56 +08:00
|
|
|
use std::fmt;
|
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
use base64::Engine;
|
|
|
|
use tracing::{error, info};
|
|
|
|
|
2024-01-11 14:42:03 +08:00
|
|
|
use flowy_server_pub::af_cloud_config::AFCloudConfiguration;
|
|
|
|
use flowy_server_pub::supabase_config::SupabaseConfiguration;
|
2023-12-29 13:02:27 +08:00
|
|
|
use flowy_user::services::entities::URL_SAFE_ENGINE;
|
2023-11-20 20:54:47 +08:00
|
|
|
use lib_infra::file_util::copy_dir_recursive;
|
2024-04-07 21:36:55 +08:00
|
|
|
use lib_infra::util::Platform;
|
2023-11-17 15:38:56 +08:00
|
|
|
|
|
|
|
use crate::integrate::log::create_log_filter;
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct AppFlowyCoreConfig {
|
|
|
|
/// Different `AppFlowyCoreConfig` instance should have different name
|
2024-01-30 09:33:34 +08:00
|
|
|
pub(crate) app_version: String,
|
2024-03-22 14:15:38 +07:00
|
|
|
pub(crate) name: String,
|
2023-11-20 20:54:47 +08:00
|
|
|
pub(crate) device_id: String,
|
2024-04-07 21:36:55 +08:00
|
|
|
pub platform: String,
|
2023-11-20 20:54:47 +08:00
|
|
|
/// Used to store the user data
|
2023-11-17 15:38:56 +08:00
|
|
|
pub storage_path: String,
|
2023-11-20 20:54:47 +08:00
|
|
|
/// Origin application path is the path of the application binary. By default, the
|
|
|
|
/// storage_path is the same as the origin_application_path. However, when the user
|
|
|
|
/// choose a custom path for the user data, the storage_path will be different from
|
|
|
|
/// the origin_application_path.
|
|
|
|
pub application_path: String,
|
2023-11-17 15:38:56 +08:00
|
|
|
pub(crate) log_filter: String,
|
|
|
|
cloud_config: Option<AFCloudConfiguration>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for AppFlowyCoreConfig {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
let mut debug = f.debug_struct("AppFlowy Configuration");
|
2024-01-30 09:33:34 +08:00
|
|
|
debug.field("app_version", &self.app_version);
|
2023-11-17 15:38:56 +08:00
|
|
|
debug.field("storage_path", &self.storage_path);
|
2023-11-20 20:54:47 +08:00
|
|
|
debug.field("application_path", &self.application_path);
|
2023-11-17 15:38:56 +08:00
|
|
|
if let Some(config) = &self.cloud_config {
|
|
|
|
debug.field("base_url", &config.base_url);
|
|
|
|
debug.field("ws_url", &config.ws_base_url);
|
2023-11-24 11:54:47 +08:00
|
|
|
debug.field("gotrue_url", &config.gotrue_url);
|
2023-11-17 15:38:56 +08:00
|
|
|
}
|
|
|
|
debug.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-03 11:41:29 +08:00
|
|
|
fn make_user_data_folder(root: &str, url: &str) -> String {
|
2023-11-17 15:38:56 +08:00
|
|
|
// Isolate the user data folder by using the base url of AppFlowy cloud. This is to avoid
|
|
|
|
// the user data folder being shared by different AppFlowy cloud.
|
2023-11-24 11:54:47 +08:00
|
|
|
let storage_path = if !url.is_empty() {
|
|
|
|
let server_base64 = URL_SAFE_ENGINE.encode(url);
|
|
|
|
format!("{}_{}", root, server_base64)
|
|
|
|
} else {
|
|
|
|
root.to_string()
|
|
|
|
};
|
2023-11-17 15:38:56 +08:00
|
|
|
|
|
|
|
// Copy the user data folder from the root path to the isolated path
|
|
|
|
// The root path without any suffix is the created by the local version AppFlowy
|
|
|
|
if !Path::new(&storage_path).exists() && Path::new(root).exists() {
|
|
|
|
info!("Copy dir from {} to {}", root, storage_path);
|
|
|
|
let src = Path::new(root);
|
2023-11-20 20:54:47 +08:00
|
|
|
match copy_dir_recursive(src, Path::new(&storage_path)) {
|
2023-11-17 15:38:56 +08:00
|
|
|
Ok(_) => storage_path,
|
|
|
|
Err(err) => {
|
|
|
|
// when the copy dir failed, use the root path as the storage path
|
|
|
|
error!("Copy dir failed: {}", err);
|
|
|
|
root.to_string()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
storage_path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AppFlowyCoreConfig {
|
2023-11-20 20:54:47 +08:00
|
|
|
pub fn new(
|
2024-01-30 09:33:34 +08:00
|
|
|
app_version: String,
|
2023-11-20 20:54:47 +08:00
|
|
|
custom_application_path: String,
|
|
|
|
application_path: String,
|
|
|
|
device_id: String,
|
2024-04-07 21:36:55 +08:00
|
|
|
platform: String,
|
2023-11-20 20:54:47 +08:00
|
|
|
name: String,
|
|
|
|
) -> Self {
|
2023-11-17 15:38:56 +08:00
|
|
|
let cloud_config = AFCloudConfiguration::from_env().ok();
|
|
|
|
let storage_path = match &cloud_config {
|
|
|
|
None => {
|
|
|
|
let supabase_config = SupabaseConfiguration::from_env().ok();
|
|
|
|
match &supabase_config {
|
2023-11-20 20:54:47 +08:00
|
|
|
None => custom_application_path,
|
2024-01-03 11:41:29 +08:00
|
|
|
Some(config) => make_user_data_folder(&custom_application_path, &config.url),
|
2023-11-17 15:38:56 +08:00
|
|
|
}
|
|
|
|
},
|
2024-01-03 11:41:29 +08:00
|
|
|
Some(config) => make_user_data_folder(&custom_application_path, &config.base_url),
|
2023-11-17 15:38:56 +08:00
|
|
|
};
|
2024-04-07 21:36:55 +08:00
|
|
|
let log_filter = create_log_filter("info".to_owned(), vec![], Platform::from(&platform));
|
2023-11-17 15:38:56 +08:00
|
|
|
|
|
|
|
AppFlowyCoreConfig {
|
2024-01-30 09:33:34 +08:00
|
|
|
app_version,
|
2023-11-17 15:38:56 +08:00
|
|
|
name,
|
|
|
|
storage_path,
|
2023-11-20 20:54:47 +08:00
|
|
|
application_path,
|
|
|
|
device_id,
|
2024-04-07 21:36:55 +08:00
|
|
|
platform,
|
|
|
|
log_filter,
|
2023-11-17 15:38:56 +08:00
|
|
|
cloud_config,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-22 14:15:38 +07:00
|
|
|
pub fn log_filter(mut self, level: &str, with_crates: Vec<String>) -> Self {
|
2024-04-07 21:36:55 +08:00
|
|
|
self.log_filter = create_log_filter(
|
|
|
|
level.to_owned(),
|
|
|
|
with_crates,
|
|
|
|
Platform::from(&self.platform),
|
|
|
|
);
|
2023-11-17 15:38:56 +08:00
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|