mirror of
https://github.com/meilisearch/MeiliSearch
synced 2025-07-03 20:07:09 +02:00
save the user-id in the config dir of the OS
This commit is contained in:
parent
f9b14ca149
commit
9be90011c6
3 changed files with 93 additions and 20 deletions
|
@ -1,11 +1,50 @@
|
|||
use actix_web::HttpRequest;
|
||||
use meilisearch_lib::index::SearchQuery;
|
||||
use serde_json::Value;
|
||||
use std::fmt::Display;
|
||||
use std::fs::read_to_string;
|
||||
|
||||
use crate::routes::indexes::documents::UpdateDocumentsQuery;
|
||||
use crate::Opt;
|
||||
use actix_web::HttpRequest;
|
||||
use meilisearch_lib::index::SearchQuery;
|
||||
use once_cell::sync::Lazy;
|
||||
use platform_dirs::AppDirs;
|
||||
use serde_json::Value;
|
||||
use std::fmt::Display;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// The MeiliSearch config dir:
|
||||
/// `~/.config/MeiliSearch` on *NIX or *BSD.
|
||||
/// `~/Library/ApplicationSupport` on macOS.
|
||||
/// `%APPDATA` (= `C:\Users%USERNAME%\AppData\Roaming`) on windows.
|
||||
static MEILISEARCH_CONFIG_PATH: Lazy<Option<PathBuf>> =
|
||||
Lazy::new(|| AppDirs::new(Some("MeiliSearch"), false).map(|appdir| appdir.config_dir));
|
||||
|
||||
fn config_user_id_path(opt: &Opt) -> Option<PathBuf> {
|
||||
opt.db_path
|
||||
.canonicalize()
|
||||
.ok()
|
||||
.map(|path| path.join("user-id").display().to_string().replace("/", "-"))
|
||||
.zip(MEILISEARCH_CONFIG_PATH.as_ref())
|
||||
.map(|(filename, config_path)| config_path.join(filename))
|
||||
}
|
||||
|
||||
/// Look for the user-id in the `data.ms` or in `~/.config/MeiliSearch/path-to-db-user-id`
|
||||
fn find_user_id(opt: &Opt) -> Option<String> {
|
||||
fs::read_to_string(opt.db_path.join("user-id"))
|
||||
.ok()
|
||||
.or_else(|| fs::read_to_string(&config_user_id_path(opt)?).ok())
|
||||
}
|
||||
|
||||
#[cfg(all(not(debug_assertions), feature = "analytics"))]
|
||||
/// Write the user-id in the `data.ms` and in `~/.config/MeiliSearch/path-to-db-user-id`. Ignore the errors.
|
||||
fn write_user_id(opt: &Opt, user_id: &str) {
|
||||
let _ = fs::write(opt.db_path.join("user-id"), user_id.as_bytes());
|
||||
if let Some((meilisearch_config_path, user_id_path)) = MEILISEARCH_CONFIG_PATH
|
||||
.as_ref()
|
||||
.zip(config_user_id_path(opt))
|
||||
{
|
||||
println!("{}", user_id_path.display());
|
||||
let _ = fs::create_dir_all(&meilisearch_config_path);
|
||||
let _ = fs::write(user_id_path, user_id.as_bytes());
|
||||
}
|
||||
}
|
||||
|
||||
// if we are in release mode and the feature analytics was enabled
|
||||
#[cfg(all(not(debug_assertions), feature = "analytics"))]
|
||||
|
@ -24,7 +63,6 @@ mod segment {
|
|||
use serde_json::{json, Value};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::fmt::Display;
|
||||
use std::fs;
|
||||
use std::time::{Duration, Instant};
|
||||
use sysinfo::{DiskExt, System, SystemExt};
|
||||
use tokio::sync::Mutex;
|
||||
|
@ -100,17 +138,12 @@ mod segment {
|
|||
}
|
||||
|
||||
pub async fn new(opt: &Opt, meilisearch: &MeiliSearch) -> &'static Self {
|
||||
// see if there is already a user-id in the `data.ms`
|
||||
let user_id = fs::read_to_string(opt.db_path.join("user-id"))
|
||||
.or_else(|_| fs::read_to_string("/tmp/meilisearch-user-id"));
|
||||
let first_time_run = user_id.is_err();
|
||||
// see if there is already a user-id in the `data.ms` or in `/tmp/path-to-db-user-id`
|
||||
let user_id = super::find_user_id(opt);
|
||||
let first_time_run = user_id.is_none();
|
||||
// if not, generate a new user-id and save it to the fs
|
||||
let user_id = user_id.unwrap_or_else(|_| Uuid::new_v4().to_string());
|
||||
let _ = fs::write(opt.db_path.join("user-id"), user_id.as_bytes());
|
||||
let _ = fs::write(
|
||||
opt.db_path.join("/tmp/meilisearch-user-id"),
|
||||
user_id.as_bytes(),
|
||||
);
|
||||
let user_id = user_id.unwrap_or_else(|| Uuid::new_v4().to_string());
|
||||
super::write_user_id(opt, &user_id);
|
||||
|
||||
let client = HttpClient::default();
|
||||
let user = User::UserId {
|
||||
|
@ -536,9 +569,7 @@ pub struct MockAnalytics {
|
|||
|
||||
impl MockAnalytics {
|
||||
pub fn new(opt: &Opt) -> &'static Self {
|
||||
let user = read_to_string(opt.db_path.join("user-id"))
|
||||
.or_else(|_| read_to_string("/tmp/meilisearch-user-id"))
|
||||
.unwrap_or_else(|_| "".to_string());
|
||||
let user = find_user_id(opt).unwrap_or(String::new());
|
||||
let analytics = Box::new(Self { user });
|
||||
Box::leak(analytics)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue