-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuser_workflow.rs
More file actions
198 lines (157 loc) · 6.5 KB
/
Copy pathuser_workflow.rs
File metadata and controls
198 lines (157 loc) · 6.5 KB
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
// SPDX-License-Identifier: Apache-2.0
//! User Workflow Example: Using storage for files
//!
//! This example demonstrates the user flow in the bucket-based model.
//! Users are responsible for:
//! - Creating drives on their assigned buckets
//! - Uploading and downloading files
//! - Managing folder structures
//!
//! Users do NOT need to manage:
//! - Buckets (admin creates these)
//! - Storage agreements (admin handles these)
//! - Provider failures (admin replaces failed providers)
//! - Challenges (automated by Layer 0)
use file_system_primitives::DriveId;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();
println!("==================================================");
println!(" USER WORKFLOW: Using Storage for Files");
println!("==================================================\n");
// NOTE: In production, this would be initialized with actual endpoints
// let fs_client = FileSystemClient::new(
// "ws://127.0.0.1:2222", // Parachain RPC
// "http://provider.example.com", // Storage provider HTTP
// ).await?;
println!("Client initialized\n");
// ============================================================
// Step 1: List Available Buckets
// ============================================================
println!("Step 1: Checking available storage...");
// Query which buckets the user can access
// Returns buckets where user has Reader+Writer permissions
// let buckets = fs_client.list_my_buckets().await?;
// Placeholder response
let buckets = vec![
BucketInfo {
id: 1,
capacity_gb: 100,
available_gb: 100,
admin: "Alice".to_string(),
},
];
for bucket in &buckets {
println!(" Bucket {}: {} GB available", bucket.id, bucket.available_gb);
println!(" Admin: {}", bucket.admin);
}
println!();
let bucket_id = buckets[0].id;
// ============================================================
// Step 2: Create Drive on Bucket
// ============================================================
println!("Step 2: Creating drive on bucket {}...", bucket_id);
// User creates drive on their assigned bucket
// System automatically validates:
// - Bucket exists
// - User has Reader+Writer permissions
// - Bucket not already used by another drive
// let drive_id = fs_client.create_drive(bucket_id, Some("My Documents")).await?;
let drive_id: DriveId = 1; // Placeholder
println!("✓ Drive {} created", drive_id);
println!();
// ============================================================
// Step 3: Upload Files
// ============================================================
println!("Step 3: Uploading files...");
// Upload a document
// let file1 = std::fs::read("report.pdf")?;
// fs_client.upload_file(drive_id, "/report.pdf", &file1, bucket_id).await?;
println!("✓ Uploaded report.pdf");
// Upload a presentation
// let file2 = std::fs::read("presentation.pptx")?;
// fs_client.upload_file(drive_id, "/presentation.pptx", &file2, bucket_id).await?;
println!("✓ Uploaded presentation.pptx");
// Upload to subfolder
// let file3 = std::fs::read("photo.jpg")?;
// fs_client.upload_file(drive_id, "/images/photo.jpg", &file3, bucket_id).await?;
println!("✓ Uploaded /images/photo.jpg");
println!();
// ============================================================
// Step 4: Create Folders
// ============================================================
println!("Step 4: Creating folders...");
// fs_client.create_directory(drive_id, "/documents", bucket_id).await?;
println!("✓ Created /documents");
// fs_client.create_directory(drive_id, "/images", bucket_id).await?;
println!("✓ Created /images");
println!();
// ============================================================
// Step 5: List Directory
// ============================================================
println!("Step 5: Listing files:");
// let entries = fs_client.list_directory(drive_id, "/").await?;
// Placeholder entries
let entries = vec![
("report.pdf", "FILE", 1_048_576),
("presentation.pptx", "FILE", 2_097_152),
("documents", "DIR", 0),
("images", "DIR", 0),
];
for (name, type_str, size) in entries {
if type_str == "DIR" {
println!(" [{}] {}/", type_str, name);
} else {
println!(" [{}] {} ({} bytes)", type_str, name, size);
}
}
println!();
// ============================================================
// Step 6: Download File
// ============================================================
println!("Step 6: Downloading file...");
// let data = fs_client.download_file(drive_id, "/report.pdf").await?;
// std::fs::write("./downloaded_report.pdf", data)?;
println!("✓ Downloaded report.pdf");
println!();
// ============================================================
// Step 7: Delete File
// ============================================================
println!("Step 7: Deleting old file...");
// fs_client.delete_file(drive_id, "/old_document.pdf", bucket_id).await?;
println!("✓ Deleted /old_document.pdf");
println!();
// ============================================================
// Done!
// ============================================================
println!("==================================================");
println!("✓ All operations complete!");
println!("==================================================");
println!();
println!("What the user did:");
println!(" ✓ Listed available buckets");
println!(" ✓ Created drive on assigned bucket");
println!(" ✓ Uploaded files");
println!(" ✓ Created folders");
println!(" ✓ Listed directory");
println!(" ✓ Downloaded file");
println!(" ✓ Deleted file");
println!();
println!("What the user did NOT do:");
println!(" ✗ Create buckets");
println!(" ✗ Manage storage agreements");
println!(" ✗ Handle challenges");
println!(" ✗ Replace failed providers");
println!();
println!("Infrastructure is completely transparent to the user!");
println!();
Ok(())
}
// Helper struct for demonstration
#[derive(Debug)]
struct BucketInfo {
id: u64,
capacity_gb: u64,
available_gb: u64,
admin: String,
}