File size: 17,997 Bytes
bbb1195 710977f bbb1195 032a337 bbb1195 710977f 9ed9e48 032a337 9ed9e48 710977f bbb1195 032a337 bbb1195 9ed9e48 bbb1195 9ed9e48 bbb1195 9ed9e48 bbb1195 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 | use std::fs;
use std::path::PathBuf;
use uuid::Uuid;
use crate::models::{Account, AccountIndex, AccountSummary, TokenData, QuotaData};
use crate::modules;
use once_cell::sync::Lazy;
use std::sync::Mutex;
/// Global account write lock to prevent concurrent index file corruption
static ACCOUNT_INDEX_LOCK: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(()));
const DATA_DIR_LOCAL: &str = ".antigravity_tools";
const DATA_DIR_CLOUD: &str = "/data";
const ACCOUNTS_INDEX: &str = "accounts.json";
const ACCOUNTS_DIR: &str = "accounts";
/// Get data directory path (cloud-compatible)
/// Priority: /data (HF persistent) > ./data (current dir) > ~/.antigravity_tools (fallback)
pub fn get_data_dir() -> Result<PathBuf, String> {
// Check for cloud environment (/data)
let cloud_path = PathBuf::from(DATA_DIR_CLOUD);
if cloud_path.exists() {
modules::logger::log_info(&format!("[get_data_dir] Using cloud path: {:?}", cloud_path));
return Ok(cloud_path);
}
// Check for ./data (current working directory, used in Docker)
let cwd_data = PathBuf::from("./data");
if cwd_data.exists() {
let canonical = cwd_data.canonicalize()
.map_err(|e| format!("Failed to canonicalize ./data: {}", e))?;
modules::logger::log_info(&format!("[get_data_dir] Using cwd data path: {:?}", canonical));
return Ok(canonical);
}
// Fallback to local development path
let home = dirs::home_dir().ok_or("Cannot get user home directory")?;
let data_dir = home.join(DATA_DIR_LOCAL);
// Ensure directory exists
if !data_dir.exists() {
fs::create_dir_all(&data_dir)
.map_err(|e| format!("Failed to create data directory: {}", e))?;
}
modules::logger::log_info(&format!("[get_data_dir] Using local path: {:?}", data_dir));
Ok(data_dir)
}
/// Get accounts directory path
pub fn get_accounts_dir() -> Result<PathBuf, String> {
let data_dir = get_data_dir()?;
let accounts_dir = data_dir.join(ACCOUNTS_DIR);
if !accounts_dir.exists() {
fs::create_dir_all(&accounts_dir)
.map_err(|e| format!("Failed to create accounts directory: {}", e))?;
}
Ok(accounts_dir)
}
/// Load account index
pub fn load_account_index() -> Result<AccountIndex, String> {
let data_dir = get_data_dir()?;
let index_path = data_dir.join(ACCOUNTS_INDEX);
if !index_path.exists() {
modules::logger::log_warn("Account index file does not exist");
return Ok(AccountIndex::new());
}
let content = fs::read_to_string(&index_path)
.map_err(|e| format!("Failed to read account index: {}", e))?;
let index: AccountIndex = serde_json::from_str(&content)
.map_err(|e| format!("Failed to parse account index: {}", e))?;
modules::logger::log_info(&format!("Loaded index with {} accounts", index.accounts.len()));
Ok(index)
}
/// Save account index (atomic write)
pub fn save_account_index(index: &AccountIndex) -> Result<(), String> {
let data_dir = get_data_dir()?;
let index_path = data_dir.join(ACCOUNTS_INDEX);
let temp_path = data_dir.join(format!("{}.tmp", ACCOUNTS_INDEX));
modules::logger::log_info(&format!("[save_account_index] Saving to: {:?}", index_path));
let content = serde_json::to_string_pretty(index)
.map_err(|e| format!("Failed to serialize account index: {}", e))?;
// Write to temp file
fs::write(&temp_path, &content)
.map_err(|e| format!("Failed to write temp index file: {}", e))?;
// Atomic rename
fs::rename(&temp_path, &index_path)
.map_err(|e| format!("Failed to replace index file: {}", e))?;
modules::logger::log_info(&format!("[save_account_index] Saved {} accounts successfully", index.accounts.len()));
Ok(())
}
/// Load account data
pub fn load_account(account_id: &str) -> Result<Account, String> {
let accounts_dir = get_accounts_dir()?;
let account_path = accounts_dir.join(format!("{}.json", account_id));
if !account_path.exists() {
return Err(format!("Account not found: {}", account_id));
}
let content = fs::read_to_string(&account_path)
.map_err(|e| format!("Failed to read account data: {}", e))?;
serde_json::from_str(&content)
.map_err(|e| format!("Failed to parse account data: {}", e))
}
/// Save account data
pub fn save_account(account: &Account) -> Result<(), String> {
let accounts_dir = get_accounts_dir()?;
let account_path = accounts_dir.join(format!("{}.json", account.id));
let content = serde_json::to_string_pretty(account)
.map_err(|e| format!("Failed to serialize account data: {}", e))?;
fs::write(&account_path, content)
.map_err(|e| format!("Failed to save account data: {}", e))
}
/// List all accounts
pub fn list_accounts() -> Result<Vec<Account>, String> {
modules::logger::log_info("Listing accounts...");
let mut index = load_account_index()?;
let mut accounts = Vec::new();
let mut invalid_ids = Vec::new();
for summary in &index.accounts {
match load_account(&summary.id) {
Ok(account) => accounts.push(account),
Err(e) => {
modules::logger::log_error(&format!("Failed to load account {}: {}", summary.id, e));
if e.contains("Account not found") || e.contains("Os { code: 2,") || e.contains("No such file") {
invalid_ids.push(summary.id.clone());
}
},
}
}
// Auto-fix index: remove invalid account IDs
if !invalid_ids.is_empty() {
modules::logger::log_warn(&format!("Found {} invalid account indexes, cleaning up...", invalid_ids.len()));
index.accounts.retain(|s| !invalid_ids.contains(&s.id));
if let Some(current_id) = &index.current_account_id {
if invalid_ids.contains(current_id) {
index.current_account_id = index.accounts.first().map(|s| s.id.clone());
}
}
if let Err(e) = save_account_index(&index) {
modules::logger::log_error(&format!("Failed to clean up index: {}", e));
} else {
modules::logger::log_info("Index cleanup completed");
}
}
Ok(accounts)
}
/// Add account
pub fn add_account(email: String, name: Option<String>, token: TokenData) -> Result<Account, String> {
let _lock = ACCOUNT_INDEX_LOCK.lock().map_err(|e| format!("Failed to get lock: {}", e))?;
let mut index = load_account_index()?;
// Check if already exists
if index.accounts.iter().any(|s| s.email == email) {
return Err(format!("Account already exists: {}", email));
}
// Create new account
let account_id = Uuid::new_v4().to_string();
let mut account = Account::new(account_id.clone(), email.clone(), token);
account.name = name.clone();
// Save account data
save_account(&account)?;
// Update index
index.accounts.push(AccountSummary {
id: account_id.clone(),
email: email.clone(),
name: name.clone(),
created_at: account.created_at,
last_used: account.last_used,
});
// If first account, set as current
if index.current_account_id.is_none() {
index.current_account_id = Some(account_id);
}
save_account_index(&index)?;
Ok(account)
}
/// Add or update account
pub fn upsert_account(email: String, name: Option<String>, token: TokenData) -> Result<Account, String> {
let _lock = ACCOUNT_INDEX_LOCK.lock().map_err(|e| format!("Failed to get lock: {}", e))?;
let mut index = load_account_index()?;
// Find account ID if exists
let existing_account_id = index.accounts.iter()
.find(|s| s.email == email)
.map(|s| s.id.clone());
if let Some(account_id) = existing_account_id {
// Update existing account
match load_account(&account_id) {
Ok(mut account) => {
account.token = token;
account.name = name.clone();
account.update_last_used();
save_account(&account)?;
// Sync update name in index
if let Some(idx_summary) = index.accounts.iter_mut().find(|s| s.id == account_id) {
idx_summary.name = name;
save_account_index(&index)?;
}
return Ok(account);
},
Err(e) => {
modules::logger::log_warn(&format!("Account {} file missing ({}), recreating...", account_id, e));
// Index exists but file missing, recreate
let mut account = Account::new(account_id.clone(), email.clone(), token);
account.name = name.clone();
save_account(&account)?;
if let Some(idx_summary) = index.accounts.iter_mut().find(|s| s.id == account_id) {
idx_summary.name = name;
save_account_index(&index)?;
}
return Ok(account);
}
}
}
// Not exists, add new
drop(_lock);
add_account(email, name, token)
}
/// Delete account
pub fn delete_account(account_id: &str) -> Result<(), String> {
let _lock = ACCOUNT_INDEX_LOCK.lock().map_err(|e| format!("Failed to get lock: {}", e))?;
let mut index = load_account_index()?;
// Remove from index
let original_len = index.accounts.len();
index.accounts.retain(|s| s.id != account_id);
if index.accounts.len() == original_len {
return Err(format!("Account ID not found: {}", account_id));
}
// If current account, clear it
if index.current_account_id.as_deref() == Some(account_id) {
index.current_account_id = index.accounts.first().map(|s| s.id.clone());
}
save_account_index(&index)?;
// Delete account file
let accounts_dir = get_accounts_dir()?;
let account_path = accounts_dir.join(format!("{}.json", account_id));
if account_path.exists() {
fs::remove_file(&account_path)
.map_err(|e| format!("Failed to delete account file: {}", e))?;
}
Ok(())
}
/// Batch delete accounts (atomic index operation)
pub fn delete_accounts(account_ids: &[String]) -> Result<(), String> {
let _lock = ACCOUNT_INDEX_LOCK.lock().map_err(|e| format!("Failed to get lock: {}", e))?;
let mut index = load_account_index()?;
let accounts_dir = get_accounts_dir()?;
for account_id in account_ids {
// Remove from index
index.accounts.retain(|s| &s.id != account_id);
// If current account, clear it
if index.current_account_id.as_deref() == Some(account_id) {
index.current_account_id = None;
}
// Delete account file
let account_path = accounts_dir.join(format!("{}.json", account_id));
if account_path.exists() {
let _ = fs::remove_file(&account_path);
}
}
// If current account is empty, try to select first as default
if index.current_account_id.is_none() {
index.current_account_id = index.accounts.first().map(|s| s.id.clone());
}
save_account_index(&index)
}
/// Get current account ID
pub fn get_current_account_id() -> Result<Option<String>, String> {
let index = load_account_index()?;
Ok(index.current_account_id)
}
/// Get current active account info
pub fn get_current_account() -> Result<Option<Account>, String> {
if let Some(id) = get_current_account_id()? {
Ok(Some(load_account(&id)?))
} else {
Ok(None)
}
}
/// Set current active account ID
pub fn set_current_account_id(account_id: &str) -> Result<(), String> {
let _lock = ACCOUNT_INDEX_LOCK.lock().map_err(|e| format!("Failed to get lock: {}", e))?;
let mut index = load_account_index()?;
index.current_account_id = Some(account_id.to_string());
save_account_index(&index)
}
/// Update account quota
pub fn update_account_quota(account_id: &str, quota: QuotaData) -> Result<(), String> {
let mut account = load_account(account_id)?;
account.update_quota(quota);
save_account(&account)
}
/// Export all account refresh_tokens
#[allow(dead_code)]
pub fn export_accounts() -> Result<Vec<(String, String)>, String> {
let accounts = list_accounts()?;
let mut exports = Vec::new();
for account in accounts {
exports.push((account.email, account.token.refresh_token));
}
Ok(exports)
}
/// Fetch quota with retry mechanism
pub async fn fetch_quota_with_retry(account: &mut Account) -> crate::error::AppResult<QuotaData> {
use crate::modules::oauth;
use crate::error::AppError;
use reqwest::StatusCode;
// 1. Time-based check - ensure token is valid
let token = oauth::ensure_fresh_token(&account.token).await.map_err(AppError::OAuth)?;
if token.access_token != account.token.access_token {
modules::logger::log_info(&format!("Token refreshed for: {}", account.email));
account.token = token.clone();
// Re-fetch user name if missing
let name = if account.name.is_none() || account.name.as_ref().map_or(false, |n| n.trim().is_empty()) {
match oauth::get_user_info(&token.access_token).await {
Ok(user_info) => user_info.get_display_name(),
Err(_) => None
}
} else {
account.name.clone()
};
account.name = name.clone();
upsert_account(account.email.clone(), name, token.clone()).map_err(AppError::Account)?;
}
// 0. Fill user name if missing
if account.name.is_none() || account.name.as_ref().map_or(false, |n| n.trim().is_empty()) {
modules::logger::log_info(&format!("Account {} missing name, fetching...", account.email));
match oauth::get_user_info(&account.token.access_token).await {
Ok(user_info) => {
let display_name = user_info.get_display_name();
modules::logger::log_info(&format!("Got user name: {:?}", display_name));
account.name = display_name.clone();
if let Err(e) = upsert_account(account.email.clone(), display_name, account.token.clone()) {
modules::logger::log_warn(&format!("Failed to save user name: {}", e));
}
},
Err(e) => {
modules::logger::log_warn(&format!("Failed to get user name: {}", e));
}
}
}
// 2. Try to query quota
let result: crate::error::AppResult<(QuotaData, Option<String>)> = modules::fetch_quota(&account.token.access_token, &account.email).await;
// Capture possible project_id update and save
if let Ok((ref _q, ref project_id)) = result {
if project_id.is_some() && *project_id != account.token.project_id {
modules::logger::log_info(&format!("Detected project_id update ({}), saving...", account.email));
account.token.project_id = project_id.clone();
if let Err(e) = upsert_account(account.email.clone(), account.name.clone(), account.token.clone()) {
modules::logger::log_warn(&format!("Failed to save project_id: {}", e));
}
}
}
// 3. Handle 401 error
if let Err(AppError::Network(ref e)) = result {
if let Some(status) = e.status() {
if status == StatusCode::UNAUTHORIZED {
modules::logger::log_warn(&format!("401 Unauthorized for {}, forcing refresh...", account.email));
// Force refresh
let token_res = oauth::refresh_access_token(&account.token.refresh_token)
.await
.map_err(AppError::OAuth)?;
let new_token = TokenData::new(
token_res.access_token.clone(),
account.token.refresh_token.clone(),
token_res.expires_in,
account.token.email.clone(),
account.token.project_id.clone(),
None,
);
// Re-fetch user name
let name = if account.name.is_none() || account.name.as_ref().map_or(false, |n| n.trim().is_empty()) {
match oauth::get_user_info(&token_res.access_token).await {
Ok(user_info) => user_info.get_display_name(),
Err(_) => None
}
} else {
account.name.clone()
};
account.token = new_token.clone();
account.name = name.clone();
upsert_account(account.email.clone(), name, new_token.clone()).map_err(AppError::Account)?;
// Retry query
let retry_result: crate::error::AppResult<(QuotaData, Option<String>)> = modules::fetch_quota(&new_token.access_token, &account.email).await;
// Also handle retry project_id save
if let Ok((ref _q, ref project_id)) = retry_result {
if project_id.is_some() && *project_id != account.token.project_id {
modules::logger::log_info(&format!("Detected retry project_id update ({}), saving...", account.email));
account.token.project_id = project_id.clone();
let _ = upsert_account(account.email.clone(), account.name.clone(), account.token.clone());
}
}
if let Err(AppError::Network(ref e)) = retry_result {
if let Some(s) = e.status() {
if s == StatusCode::FORBIDDEN {
let mut q = QuotaData::new();
q.is_forbidden = true;
return Ok(q);
}
}
}
return retry_result.map(|(q, _)| q);
}
}
}
result.map(|(q, _)| q)
}
|