From e53098728a3315079cb22376851cc3fe314f79dd Mon Sep 17 00:00:00 2001 From: Ashwin Kumar Sivakumar Date: Sat, 11 Jul 2026 02:13:56 +0530 Subject: [PATCH] =?UTF-8?q?refactor(companies):=20remove=20local-disk=20fa?= =?UTF-8?q?llback=20from=20submit=5Fwith=5Fdocuments=20=E2=80=94=20B2=20on?= =?UTF-8?q?ly?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/companies/src/handlers/mod.rs | 65 ++++++++---------------------- 1 file changed, 16 insertions(+), 49 deletions(-) diff --git a/apps/companies/src/handlers/mod.rs b/apps/companies/src/handlers/mod.rs index 99e3830..f188431 100644 --- a/apps/companies/src/handlers/mod.rs +++ b/apps/companies/src/handlers/mod.rs @@ -748,9 +748,6 @@ async fn view_contact( } } -/// Local-disk fallback directory for company documents when B2 is down. -const COMPANY_DOCS_LOCAL_DIR: &str = "/var/lib/nxtgauge-uploads/company_documents"; - /// POST /api/companies/profile/submit-with-documents /// /// Accepts multipart/form-data with: @@ -759,9 +756,9 @@ const COMPANY_DOCS_LOCAL_DIR: &str = "/var/lib/nxtgauge-uploads/company_document /// /// In one request this handler: /// 1. Saves the profile into `company_profiles` (DRAFT upsert, same SQL as users service). -/// 2. Uploads each document, trying B2 first then falling back to local disk -/// (`/var/lib/nxtgauge-uploads/company_documents/{uuid}.{ext}`) and inserting -/// a `company_documents` row regardless of source. +/// 2. Uploads each document to B2 (`storage_backend` is always `"b2"`). On B2 failure +/// the file is recorded in `failure_details` and `documents_failed` is incremented, +/// then processing continues with the next file. No local-disk fallback. /// 3. Creates a `verifications` row (case_type = "PROFILE_VERIFICATION", priority = "MEDIUM"). /// 4. Updates `company_profiles.status` to 'PENDING'. /// 5. Returns `{ verification_id, status, documents_uploaded, documents_failed, storage_backend }`. @@ -779,7 +776,7 @@ async fn submit_with_documents( let mut documents_meta: Vec = Vec::new(); let mut documents_uploaded: usize = 0; let mut documents_failed: usize = 0; - let mut storage_backend: &str = "b2"; + let storage_backend: &str = "b2"; let mut failure_details: Vec = Vec::new(); while let Ok(Some(field)) = multipart.next_field().await { @@ -796,7 +793,7 @@ async fn submit_with_documents( } } } else if name == "documents" || name == "files" || name == "file" { - // ---- 2a. Upload to storage (B2 with local-disk fallback). ---- + // ---- 2a. Upload to B2. ---- let original_filename = field.file_name().unwrap_or("").to_string(); let content_type = field .content_type() @@ -846,60 +843,30 @@ async fn submit_with_documents( let data_len = data.len() as i64; - // Try B2 first. - let (url, source) = match state + // Upload to B2. On failure, record the file in failure_details and continue. + let url: String = match state .storage .upload("company_documents", &ext, data.clone(), &content_type) .await { - Ok(u) => (u, "b2"), + Ok(u) => u, Err(b2_err) => { tracing::warn!( - "B2 upload failed for company user {} ({}) — falling back to local disk: {}", + "B2 upload failed for company user {} ({}): {}", auth.user_id, original_filename, b2_err ); - // Fallback: write to local disk. - let dir = std::path::Path::new(COMPANY_DOCS_LOCAL_DIR); - if let Err(mkdir_err) = tokio::fs::create_dir_all(dir).await { - tracing::error!( - "Local-disk fallback unavailable: failed to create {}: {}", - COMPANY_DOCS_LOCAL_DIR, - mkdir_err - ); - documents_failed += 1; - failure_details.push(serde_json::json!({ - "filename": original_filename, - "error": format!("B2 failed and local fallback mkdir failed: {}", mkdir_err), - "b2_error": b2_err.to_string(), - })); - continue; - } - let file_id = Uuid::new_v4(); - let file_path = dir.join(format!("{}.{}", file_id, ext)); - if let Err(write_err) = tokio::fs::write(&file_path, &data).await { - tracing::error!( - "Local-disk fallback write failed for {}: {}", - file_path.display(), - write_err - ); - documents_failed += 1; - failure_details.push(serde_json::json!({ - "filename": original_filename, - "error": format!("B2 failed and local fallback write failed: {}", write_err), - "b2_error": b2_err.to_string(), - })); - continue; - } - (format!("file://{}", file_path.display()), "local") + documents_failed += 1; + failure_details.push(serde_json::json!({ + "filename": original_filename, + "error": format!("B2 upload failed: {}", b2_err), + })); + continue; } }; - // Track storage backend for the response: once any file goes local, the request is "local". - if source == "local" { - storage_backend = "local"; - } + // storage_backend is always "b2" (set at top of function). // Derive a stable document_type from the original filename (stem) so the admin UI can group. let document_type = if !original_filename.is_empty() {