Monorepo for wisp.place. A static site hosting service built on top of the AT Protocol.
wisp.place
1mod builder_types;
2mod place_wisp;
3mod cid;
4mod blob_map;
5mod metadata;
6mod download;
7mod pull;
8mod serve;
9mod subfs_utils;
10mod redirects;
11
12use clap::{Parser, Subcommand};
13use jacquard::CowStr;
14use jacquard::client::{Agent, FileAuthStore, AgentSessionExt, MemoryCredentialSession, AgentSession};
15use jacquard::oauth::client::OAuthClient;
16use jacquard::oauth::loopback::LoopbackConfig;
17use jacquard::prelude::IdentityResolver;
18use jacquard_common::types::string::{Datetime, Rkey, RecordKey};
19use jacquard_common::types::blob::MimeType;
20use miette::IntoDiagnostic;
21use std::path::{Path, PathBuf};
22use std::collections::HashMap;
23use flate2::Compression;
24use flate2::write::GzEncoder;
25use std::io::Write;
26use base64::Engine;
27use futures::stream::{self, StreamExt};
28
29use place_wisp::fs::*;
30use place_wisp::settings::*;
31
32#[derive(Parser, Debug)]
33#[command(author, version, about = "wisp.place CLI tool")]
34struct Args {
35 #[command(subcommand)]
36 command: Option<Commands>,
37
38 // Deploy arguments (when no subcommand is specified)
39 /// Handle (e.g., alice.bsky.social), DID, or PDS URL
40 #[arg(global = true, conflicts_with = "command")]
41 input: Option<CowStr<'static>>,
42
43 /// Path to the directory containing your static site
44 #[arg(short, long, global = true, conflicts_with = "command")]
45 path: Option<PathBuf>,
46
47 /// Site name (defaults to directory name)
48 #[arg(short, long, global = true, conflicts_with = "command")]
49 site: Option<String>,
50
51 /// Path to auth store file
52 #[arg(long, global = true, conflicts_with = "command")]
53 store: Option<String>,
54
55 /// App Password for authentication
56 #[arg(long, global = true, conflicts_with = "command")]
57 password: Option<CowStr<'static>>,
58
59 /// Enable directory listing mode for paths without index files
60 #[arg(long, global = true, conflicts_with = "command")]
61 directory: bool,
62
63 /// Enable SPA mode (serve index.html for all routes)
64 #[arg(long, global = true, conflicts_with = "command")]
65 spa: bool,
66}
67
68#[derive(Subcommand, Debug)]
69enum Commands {
70 /// Deploy a static site to wisp.place (default command)
71 Deploy {
72 /// Handle (e.g., alice.bsky.social), DID, or PDS URL
73 input: CowStr<'static>,
74
75 /// Path to the directory containing your static site
76 #[arg(short, long, default_value = ".")]
77 path: PathBuf,
78
79 /// Site name (defaults to directory name)
80 #[arg(short, long)]
81 site: Option<String>,
82
83 /// Path to auth store file (will be created if missing, only used with OAuth)
84 #[arg(long, default_value = "/tmp/wisp-oauth-session.json")]
85 store: String,
86
87 /// App Password for authentication (alternative to OAuth)
88 #[arg(long)]
89 password: Option<CowStr<'static>>,
90
91 /// Enable directory listing mode for paths without index files
92 #[arg(long)]
93 directory: bool,
94
95 /// Enable SPA mode (serve index.html for all routes)
96 #[arg(long)]
97 spa: bool,
98 },
99 /// Pull a site from the PDS to a local directory
100 Pull {
101 /// Handle (e.g., alice.bsky.social) or DID
102 input: CowStr<'static>,
103
104 /// Site name (record key)
105 #[arg(short, long)]
106 site: String,
107
108 /// Output directory for the downloaded site
109 #[arg(short, long, default_value = ".")]
110 output: PathBuf,
111 },
112 /// Serve a site locally with real-time firehose updates
113 Serve {
114 /// Handle (e.g., alice.bsky.social) or DID
115 input: CowStr<'static>,
116
117 /// Site name (record key)
118 #[arg(short, long)]
119 site: String,
120
121 /// Output directory for the site files
122 #[arg(short, long, default_value = ".")]
123 output: PathBuf,
124
125 /// Port to serve on
126 #[arg(short, long, default_value = "8080")]
127 port: u16,
128 },
129}
130
131#[tokio::main]
132async fn main() -> miette::Result<()> {
133 let args = Args::parse();
134
135 let result = match args.command {
136 Some(Commands::Deploy { input, path, site, store, password, directory, spa }) => {
137 // Dispatch to appropriate authentication method
138 if let Some(password) = password {
139 run_with_app_password(input, password, path, site, directory, spa).await
140 } else {
141 run_with_oauth(input, store, path, site, directory, spa).await
142 }
143 }
144 Some(Commands::Pull { input, site, output }) => {
145 pull::pull_site(input, CowStr::from(site), output).await
146 }
147 Some(Commands::Serve { input, site, output, port }) => {
148 serve::serve_site(input, CowStr::from(site), output, port).await
149 }
150 None => {
151 // Legacy mode: if input is provided, assume deploy command
152 if let Some(input) = args.input {
153 let path = args.path.unwrap_or_else(|| PathBuf::from("."));
154 let store = args.store.unwrap_or_else(|| "/tmp/wisp-oauth-session.json".to_string());
155
156 // Dispatch to appropriate authentication method
157 if let Some(password) = args.password {
158 run_with_app_password(input, password, path, args.site, args.directory, args.spa).await
159 } else {
160 run_with_oauth(input, store, path, args.site, args.directory, args.spa).await
161 }
162 } else {
163 // No command and no input, show help
164 use clap::CommandFactory;
165 Args::command().print_help().into_diagnostic()?;
166 Ok(())
167 }
168 }
169 };
170
171 // Force exit to avoid hanging on background tasks/connections
172 match result {
173 Ok(_) => std::process::exit(0),
174 Err(e) => {
175 eprintln!("{:?}", e);
176 std::process::exit(1)
177 }
178 }
179}
180
181/// Run deployment with app password authentication
182async fn run_with_app_password(
183 input: CowStr<'static>,
184 password: CowStr<'static>,
185 path: PathBuf,
186 site: Option<String>,
187 directory: bool,
188 spa: bool,
189) -> miette::Result<()> {
190 let (session, auth) =
191 MemoryCredentialSession::authenticated(input, password, None, None).await?;
192 println!("Signed in as {}", auth.handle);
193
194 let agent: Agent<_> = Agent::from(session);
195 deploy_site(&agent, path, site, directory, spa).await
196}
197
198/// Run deployment with OAuth authentication
199async fn run_with_oauth(
200 input: CowStr<'static>,
201 store: String,
202 path: PathBuf,
203 site: Option<String>,
204 directory: bool,
205 spa: bool,
206) -> miette::Result<()> {
207 use jacquard::oauth::scopes::Scope;
208 use jacquard::oauth::atproto::AtprotoClientMetadata;
209 use jacquard::oauth::session::ClientData;
210 use url::Url;
211
212 // Request the necessary scopes for wisp.place (including settings)
213 let scopes = Scope::parse_multiple("atproto repo:place.wisp.fs repo:place.wisp.subfs repo:place.wisp.settings blob:*/*")
214 .map_err(|e| miette::miette!("Failed to parse scopes: {:?}", e))?;
215
216 // Create redirect URIs that match the loopback server (port 4000, path /oauth/callback)
217 let redirect_uris = vec![
218 Url::parse("http://127.0.0.1:4000/oauth/callback").into_diagnostic()?,
219 Url::parse("http://[::1]:4000/oauth/callback").into_diagnostic()?,
220 ];
221
222 // Create client metadata with matching redirect URIs and scopes
223 let client_data = ClientData {
224 keyset: None,
225 config: AtprotoClientMetadata::new_localhost(
226 Some(redirect_uris),
227 Some(scopes),
228 ),
229 };
230
231 let oauth = OAuthClient::new(FileAuthStore::new(&store), client_data);
232
233 let session = oauth
234 .login_with_local_server(input, Default::default(), LoopbackConfig::default())
235 .await?;
236
237 let agent: Agent<_> = Agent::from(session);
238 deploy_site(&agent, path, site, directory, spa).await
239}
240
241/// Deploy the site using the provided agent
242async fn deploy_site(
243 agent: &Agent<impl jacquard::client::AgentSession + IdentityResolver>,
244 path: PathBuf,
245 site: Option<String>,
246 directory_listing: bool,
247 spa_mode: bool,
248) -> miette::Result<()> {
249 // Verify the path exists
250 if !path.exists() {
251 return Err(miette::miette!("Path does not exist: {}", path.display()));
252 }
253
254 // Get site name
255 let site_name = site.unwrap_or_else(|| {
256 path
257 .file_name()
258 .and_then(|n| n.to_str())
259 .unwrap_or("site")
260 .to_string()
261 });
262
263 println!("Deploying site '{}'...", site_name);
264
265 // Try to fetch existing manifest for incremental updates
266 let (existing_blob_map, old_subfs_uris): (HashMap<String, (jacquard_common::types::blob::BlobRef<'static>, String)>, Vec<(String, String)>) = {
267 use jacquard_common::types::string::AtUri;
268
269 // Get the DID for this session
270 let session_info = agent.session_info().await;
271 if let Some((did, _)) = session_info {
272 // Construct the AT URI for the record
273 let uri_string = format!("at://{}/place.wisp.fs/{}", did, site_name);
274 if let Ok(uri) = AtUri::new(&uri_string) {
275 match agent.get_record::<Fs>(&uri).await {
276 Ok(response) => {
277 match response.into_output() {
278 Ok(record_output) => {
279 let existing_manifest = record_output.value;
280 let mut blob_map = blob_map::extract_blob_map(&existing_manifest.root);
281 println!("Found existing manifest with {} files in main record", blob_map.len());
282
283 // Extract subfs URIs from main record
284 let subfs_uris = subfs_utils::extract_subfs_uris(&existing_manifest.root, String::new());
285
286 if !subfs_uris.is_empty() {
287 println!("Found {} subfs records, fetching for blob reuse...", subfs_uris.len());
288
289 // Merge blob maps from all subfs records
290 match subfs_utils::merge_subfs_blob_maps(agent, subfs_uris.clone(), &mut blob_map).await {
291 Ok(merged_count) => {
292 println!("Total blob map: {} files (main + {} from subfs)", blob_map.len(), merged_count);
293 }
294 Err(e) => {
295 eprintln!("⚠️ Failed to merge some subfs blob maps: {}", e);
296 }
297 }
298
299 (blob_map, subfs_uris)
300 } else {
301 (blob_map, Vec::new())
302 }
303 }
304 Err(_) => {
305 println!("No existing manifest found, uploading all files...");
306 (HashMap::new(), Vec::new())
307 }
308 }
309 }
310 Err(_) => {
311 // Record doesn't exist yet - this is a new site
312 println!("No existing manifest found, uploading all files...");
313 (HashMap::new(), Vec::new())
314 }
315 }
316 } else {
317 println!("No existing manifest found (invalid URI), uploading all files...");
318 (HashMap::new(), Vec::new())
319 }
320 } else {
321 println!("No existing manifest found (could not get DID), uploading all files...");
322 (HashMap::new(), Vec::new())
323 }
324 };
325
326 // Build directory tree
327 let (root_dir, total_files, reused_count) = build_directory(agent, &path, &existing_blob_map, String::new()).await?;
328 let uploaded_count = total_files - reused_count;
329
330 // Check if we need to split into subfs records
331 const MAX_MANIFEST_SIZE: usize = 140 * 1024; // 140KB (PDS limit is 150KB)
332 const FILE_COUNT_THRESHOLD: usize = 250; // Start splitting at this many files
333 const TARGET_FILE_COUNT: usize = 200; // Keep main manifest under this
334
335 let mut working_directory = root_dir;
336 let mut current_file_count = total_files;
337 let mut new_subfs_uris: Vec<(String, String)> = Vec::new();
338
339 // Estimate initial manifest size
340 let mut manifest_size = subfs_utils::estimate_directory_size(&working_directory);
341
342 if total_files >= FILE_COUNT_THRESHOLD || manifest_size > MAX_MANIFEST_SIZE {
343 println!("\n⚠️ Large site detected ({} files, {:.1}KB manifest), splitting into subfs records...",
344 total_files, manifest_size as f64 / 1024.0);
345
346 let mut attempts = 0;
347 const MAX_SPLIT_ATTEMPTS: usize = 50;
348
349 while (manifest_size > MAX_MANIFEST_SIZE || current_file_count > TARGET_FILE_COUNT) && attempts < MAX_SPLIT_ATTEMPTS {
350 attempts += 1;
351
352 // Find large directories to split
353 let directories = subfs_utils::find_large_directories(&working_directory, String::new());
354
355 if let Some(largest_dir) = directories.first() {
356 println!(" Split #{}: {} ({} files, {:.1}KB)",
357 attempts, largest_dir.path, largest_dir.file_count, largest_dir.size as f64 / 1024.0);
358
359 // Create a subfs record for this directory
360 use jacquard_common::types::string::Tid;
361 let subfs_tid = Tid::now_0();
362 let subfs_rkey = subfs_tid.to_string();
363
364 let subfs_manifest = crate::place_wisp::subfs::SubfsRecord::new()
365 .root(convert_fs_dir_to_subfs_dir(largest_dir.directory.clone()))
366 .file_count(Some(largest_dir.file_count as i64))
367 .created_at(Datetime::now())
368 .build();
369
370 // Upload subfs record
371 let subfs_output = agent.put_record(
372 RecordKey::from(Rkey::new(&subfs_rkey).into_diagnostic()?),
373 subfs_manifest
374 ).await.into_diagnostic()?;
375
376 let subfs_uri = subfs_output.uri.to_string();
377 println!(" ✅ Created subfs: {}", subfs_uri);
378
379 // Replace directory with subfs node (flat: false to preserve structure)
380 working_directory = subfs_utils::replace_directory_with_subfs(
381 working_directory,
382 &largest_dir.path,
383 &subfs_uri,
384 false // Preserve directory structure
385 )?;
386
387 new_subfs_uris.push((subfs_uri, largest_dir.path.clone()));
388 current_file_count -= largest_dir.file_count;
389
390 // Recalculate manifest size
391 manifest_size = subfs_utils::estimate_directory_size(&working_directory);
392 println!(" → Manifest now {:.1}KB with {} files ({} subfs total)",
393 manifest_size as f64 / 1024.0, current_file_count, new_subfs_uris.len());
394
395 if manifest_size <= MAX_MANIFEST_SIZE && current_file_count <= TARGET_FILE_COUNT {
396 println!("✅ Manifest now fits within limits");
397 break;
398 }
399 } else {
400 println!(" No more subdirectories to split - stopping");
401 break;
402 }
403 }
404
405 if attempts >= MAX_SPLIT_ATTEMPTS {
406 return Err(miette::miette!(
407 "Exceeded maximum split attempts ({}). Manifest still too large: {:.1}KB with {} files",
408 MAX_SPLIT_ATTEMPTS,
409 manifest_size as f64 / 1024.0,
410 current_file_count
411 ));
412 }
413
414 println!("✅ Split complete: {} subfs records, {} files in main manifest, {:.1}KB",
415 new_subfs_uris.len(), current_file_count, manifest_size as f64 / 1024.0);
416 } else {
417 println!("Manifest created ({} files, {:.1}KB) - no splitting needed",
418 total_files, manifest_size as f64 / 1024.0);
419 }
420
421 // Create the final Fs record
422 let fs_record = Fs::new()
423 .site(CowStr::from(site_name.clone()))
424 .root(working_directory)
425 .file_count(current_file_count as i64)
426 .created_at(Datetime::now())
427 .build();
428
429 // Use site name as the record key
430 let rkey = Rkey::new(&site_name).map_err(|e| miette::miette!("Invalid rkey: {}", e))?;
431 let output = agent.put_record(RecordKey::from(rkey), fs_record).await?;
432
433 // Extract DID from the AT URI (format: at://did:plc:xxx/collection/rkey)
434 let uri_str = output.uri.to_string();
435 let did = uri_str
436 .strip_prefix("at://")
437 .and_then(|s| s.split('/').next())
438 .ok_or_else(|| miette::miette!("Failed to parse DID from URI"))?;
439
440 println!("\n✓ Deployed site '{}': {}", site_name, output.uri);
441 println!(" Total files: {} ({} reused, {} uploaded)", total_files, reused_count, uploaded_count);
442 println!(" Available at: https://sites.wisp.place/{}/{}", did, site_name);
443
444 // Clean up old subfs records
445 if !old_subfs_uris.is_empty() {
446 println!("\nCleaning up {} old subfs records...", old_subfs_uris.len());
447
448 let mut deleted_count = 0;
449 let mut failed_count = 0;
450
451 for (uri, _path) in old_subfs_uris {
452 match subfs_utils::delete_subfs_record(agent, &uri).await {
453 Ok(_) => {
454 deleted_count += 1;
455 println!(" 🗑️ Deleted old subfs: {}", uri);
456 }
457 Err(e) => {
458 failed_count += 1;
459 eprintln!(" ⚠️ Failed to delete {}: {}", uri, e);
460 }
461 }
462 }
463
464 if failed_count > 0 {
465 eprintln!("⚠️ Cleanup completed with {} deleted, {} failed", deleted_count, failed_count);
466 } else {
467 println!("✅ Cleanup complete: {} old subfs records deleted", deleted_count);
468 }
469 }
470
471 // Upload settings if either flag is set
472 if directory_listing || spa_mode {
473 // Validate mutual exclusivity
474 if directory_listing && spa_mode {
475 return Err(miette::miette!("Cannot enable both --directory and --SPA modes"));
476 }
477
478 println!("\n⚙️ Uploading site settings...");
479
480 // Build settings record
481 let mut settings_builder = Settings::new();
482
483 if directory_listing {
484 settings_builder = settings_builder.directory_listing(Some(true));
485 println!(" • Directory listing: enabled");
486 }
487
488 if spa_mode {
489 settings_builder = settings_builder.spa_mode(Some(CowStr::from("index.html")));
490 println!(" • SPA mode: enabled (serving index.html for all routes)");
491 }
492
493 let settings_record = settings_builder.build();
494
495 // Upload settings record with same rkey as site
496 let rkey = Rkey::new(&site_name).map_err(|e| miette::miette!("Invalid rkey: {}", e))?;
497 match agent.put_record(RecordKey::from(rkey), settings_record).await {
498 Ok(settings_output) => {
499 println!("✅ Settings uploaded: {}", settings_output.uri);
500 }
501 Err(e) => {
502 eprintln!("⚠️ Failed to upload settings: {}", e);
503 eprintln!(" Site was deployed successfully, but settings may need to be configured manually.");
504 }
505 }
506 }
507
508 Ok(())
509}
510
511/// Recursively build a Directory from a filesystem path
512/// current_path is the path from the root of the site (e.g., "" for root, "config" for config dir)
513fn build_directory<'a>(
514 agent: &'a Agent<impl jacquard::client::AgentSession + IdentityResolver + 'a>,
515 dir_path: &'a Path,
516 existing_blobs: &'a HashMap<String, (jacquard_common::types::blob::BlobRef<'static>, String)>,
517 current_path: String,
518) -> std::pin::Pin<Box<dyn std::future::Future<Output = miette::Result<(Directory<'static>, usize, usize)>> + 'a>>
519{
520 Box::pin(async move {
521 // Collect all directory entries first
522 let dir_entries: Vec<_> = std::fs::read_dir(dir_path)
523 .into_diagnostic()?
524 .collect::<Result<Vec<_>, _>>()
525 .into_diagnostic()?;
526
527 // Separate files and directories
528 let mut file_tasks = Vec::new();
529 let mut dir_tasks = Vec::new();
530
531 for entry in dir_entries {
532 let path = entry.path();
533 let name = entry.file_name();
534 let name_str = name.to_str()
535 .ok_or_else(|| miette::miette!("Invalid filename: {:?}", name))?
536 .to_string();
537
538 // Skip unwanted files and directories
539
540 // .git directory (version control - thousands of files)
541 if name_str == ".git" {
542 continue;
543 }
544
545 // .DS_Store (macOS metadata - can leak info)
546 if name_str == ".DS_Store" {
547 continue;
548 }
549
550 // .wisp.metadata.json (wisp internal metadata - should not be uploaded)
551 if name_str == ".wisp.metadata.json" {
552 continue;
553 }
554
555 // .env files (environment variables with secrets)
556 if name_str.starts_with(".env") {
557 continue;
558 }
559
560 // node_modules (dependency folder - can be 100,000+ files)
561 if name_str == "node_modules" {
562 continue;
563 }
564
565 // OS metadata files
566 if name_str == "Thumbs.db" || name_str == "desktop.ini" || name_str.starts_with("._") {
567 continue;
568 }
569
570 // macOS system directories
571 if name_str == ".Spotlight-V100" || name_str == ".Trashes" || name_str == ".fseventsd" {
572 continue;
573 }
574
575 // Cache and temp directories
576 if name_str == ".cache" || name_str == ".temp" || name_str == ".tmp" {
577 continue;
578 }
579
580 // Python cache
581 if name_str == "__pycache__" || name_str.ends_with(".pyc") {
582 continue;
583 }
584
585 // Python virtual environments
586 if name_str == ".venv" || name_str == "venv" || name_str == "env" {
587 continue;
588 }
589
590 // Editor swap files
591 if name_str.ends_with(".swp") || name_str.ends_with(".swo") || name_str.ends_with("~") {
592 continue;
593 }
594
595 let metadata = entry.metadata().into_diagnostic()?;
596
597 if metadata.is_file() {
598 // Construct full path for this file (for blob map lookup)
599 let full_path = if current_path.is_empty() {
600 name_str.clone()
601 } else {
602 format!("{}/{}", current_path, name_str)
603 };
604 file_tasks.push((name_str, path, full_path));
605 } else if metadata.is_dir() {
606 dir_tasks.push((name_str, path));
607 }
608 }
609
610 // Process files concurrently with a limit of 5
611 let file_results: Vec<(Entry<'static>, bool)> = stream::iter(file_tasks)
612 .map(|(name, path, full_path)| async move {
613 let (file_node, reused) = process_file(agent, &path, &full_path, existing_blobs).await?;
614 let entry = Entry::new()
615 .name(CowStr::from(name))
616 .node(EntryNode::File(Box::new(file_node)))
617 .build();
618 Ok::<_, miette::Report>((entry, reused))
619 })
620 .buffer_unordered(5)
621 .collect::<Vec<_>>()
622 .await
623 .into_iter()
624 .collect::<miette::Result<Vec<_>>>()?;
625
626 let mut file_entries = Vec::new();
627 let mut reused_count = 0;
628 let mut total_files = 0;
629
630 for (entry, reused) in file_results {
631 file_entries.push(entry);
632 total_files += 1;
633 if reused {
634 reused_count += 1;
635 }
636 }
637
638 // Process directories recursively (sequentially to avoid too much nesting)
639 let mut dir_entries = Vec::new();
640 for (name, path) in dir_tasks {
641 // Construct full path for subdirectory
642 let subdir_path = if current_path.is_empty() {
643 name.clone()
644 } else {
645 format!("{}/{}", current_path, name)
646 };
647 let (subdir, sub_total, sub_reused) = build_directory(agent, &path, existing_blobs, subdir_path).await?;
648 dir_entries.push(Entry::new()
649 .name(CowStr::from(name))
650 .node(EntryNode::Directory(Box::new(subdir)))
651 .build());
652 total_files += sub_total;
653 reused_count += sub_reused;
654 }
655
656 // Combine file and directory entries
657 let mut entries = file_entries;
658 entries.extend(dir_entries);
659
660 let directory = Directory::new()
661 .r#type(CowStr::from("directory"))
662 .entries(entries)
663 .build();
664
665 Ok((directory, total_files, reused_count))
666 })
667}
668
669/// Process a single file: gzip -> base64 -> upload blob (or reuse existing)
670/// Returns (File, reused: bool)
671/// file_path_key is the full path from the site root (e.g., "config/file.json") for blob map lookup
672///
673/// Special handling: _redirects files are NOT compressed (uploaded as-is)
674async fn process_file(
675 agent: &Agent<impl jacquard::client::AgentSession + IdentityResolver>,
676 file_path: &Path,
677 file_path_key: &str,
678 existing_blobs: &HashMap<String, (jacquard_common::types::blob::BlobRef<'static>, String)>,
679) -> miette::Result<(File<'static>, bool)>
680{
681 // Read file
682 let file_data = std::fs::read(file_path).into_diagnostic()?;
683
684 // Detect original MIME type
685 let original_mime = mime_guess::from_path(file_path)
686 .first_or_octet_stream()
687 .to_string();
688
689 // Check if this is a _redirects file (don't compress it)
690 let is_redirects_file = file_path.file_name()
691 .and_then(|n| n.to_str())
692 .map(|n| n == "_redirects")
693 .unwrap_or(false);
694
695 let (upload_bytes, encoding, is_base64) = if is_redirects_file {
696 // Don't compress _redirects - upload as-is
697 (file_data.clone(), None, false)
698 } else {
699 // Gzip compress
700 let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
701 encoder.write_all(&file_data).into_diagnostic()?;
702 let gzipped = encoder.finish().into_diagnostic()?;
703
704 // Base64 encode the gzipped data
705 let base64_bytes = base64::prelude::BASE64_STANDARD.encode(&gzipped).into_bytes();
706 (base64_bytes, Some("gzip"), true)
707 };
708
709 // Compute CID for this file
710 let file_cid = cid::compute_cid(&upload_bytes);
711
712 // Check if we have an existing blob with the same CID
713 let existing_blob = existing_blobs.get(file_path_key);
714
715 if let Some((existing_blob_ref, existing_cid)) = existing_blob {
716 if existing_cid == &file_cid {
717 // CIDs match - reuse existing blob
718 println!(" ✓ Reusing blob for {} (CID: {})", file_path_key, file_cid);
719 let mut file_builder = File::new()
720 .r#type(CowStr::from("file"))
721 .blob(existing_blob_ref.clone())
722 .mime_type(CowStr::from(original_mime));
723
724 if let Some(enc) = encoding {
725 file_builder = file_builder.encoding(CowStr::from(enc));
726 }
727 if is_base64 {
728 file_builder = file_builder.base64(true);
729 }
730
731 return Ok((file_builder.build(), true));
732 }
733 }
734
735 // File is new or changed - upload it
736 let mime_type = if is_redirects_file {
737 MimeType::new_static("text/plain")
738 } else {
739 MimeType::new_static("application/octet-stream")
740 };
741
742 println!(" ↑ Uploading {} ({} bytes, CID: {})", file_path_key, upload_bytes.len(), file_cid);
743 let blob = agent.upload_blob(upload_bytes, mime_type).await?;
744
745 let mut file_builder = File::new()
746 .r#type(CowStr::from("file"))
747 .blob(blob)
748 .mime_type(CowStr::from(original_mime));
749
750 if let Some(enc) = encoding {
751 file_builder = file_builder.encoding(CowStr::from(enc));
752 }
753 if is_base64 {
754 file_builder = file_builder.base64(true);
755 }
756
757 Ok((file_builder.build(), false))
758}
759
760/// Convert fs::Directory to subfs::Directory
761/// They have the same structure, but different types
762fn convert_fs_dir_to_subfs_dir(fs_dir: place_wisp::fs::Directory<'static>) -> place_wisp::subfs::Directory<'static> {
763 use place_wisp::subfs::{Directory as SubfsDirectory, Entry as SubfsEntry, EntryNode as SubfsEntryNode, File as SubfsFile};
764
765 let subfs_entries: Vec<SubfsEntry> = fs_dir.entries.into_iter().map(|entry| {
766 let node = match entry.node {
767 place_wisp::fs::EntryNode::File(file) => {
768 SubfsEntryNode::File(Box::new(SubfsFile::new()
769 .r#type(file.r#type)
770 .blob(file.blob)
771 .encoding(file.encoding)
772 .mime_type(file.mime_type)
773 .base64(file.base64)
774 .build()))
775 }
776 place_wisp::fs::EntryNode::Directory(dir) => {
777 SubfsEntryNode::Directory(Box::new(convert_fs_dir_to_subfs_dir(*dir)))
778 }
779 place_wisp::fs::EntryNode::Subfs(subfs) => {
780 // Nested subfs in the directory we're converting
781 // Note: subfs::Subfs doesn't have the 'flat' field - that's only in fs::Subfs
782 SubfsEntryNode::Subfs(Box::new(place_wisp::subfs::Subfs::new()
783 .r#type(subfs.r#type)
784 .subject(subfs.subject)
785 .build()))
786 }
787 place_wisp::fs::EntryNode::Unknown(unknown) => {
788 SubfsEntryNode::Unknown(unknown)
789 }
790 };
791
792 SubfsEntry::new()
793 .name(entry.name)
794 .node(node)
795 .build()
796 }).collect();
797
798 SubfsDirectory::new()
799 .r#type(fs_dir.r#type)
800 .entries(subfs_entries)
801 .build()
802}
803