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
use crate::models::{Datasets, V1UserProfile};
use crate::org::get_organization_names;
use crate::state::AppState;
use axum::{
extract::Extension, extract::Json, extract::State, http::StatusCode, response::IntoResponse,
};
#[axum::debug_handler]
pub async fn list_datasets(
State(state): State<AppState>,
Extension(user_profile): Extension<V1UserProfile>,
) -> impl IntoResponse {
// Collect all namespaces the user has access to
let mut namespaces = Vec::new();
// Add the user's handle if it exists
if let Some(ref handle) = user_profile.handle {
namespaces.push(handle.clone());
} else {
return Err((
StatusCode::BAD_REQUEST,
"User handle is required".to_string(),
));
}
// Add organization names
if let Some(ref organizations) = user_profile.organizations {
let org_names = get_organization_names(organizations);
namespaces.extend(org_names);
}
// Initialize a vector to hold the dataset names
let mut datasets = Vec::new();
// Iterate over all namespaces and collect datasets
for namespace in namespaces {
// Construct the path to the datasets directory for this namespace
let datasets_path = format!("/orign/{}/datasets", namespace);
// Read the directories in the datasets path
match tokio::fs::read_dir(&datasets_path).await {
Ok(mut dir) => {
while let Ok(Some(entry)) = dir.next_entry().await {
let file_type = match entry.file_type().await {
Ok(ft) => ft,
Err(err) => {
// Handle the error (e.g., skip this entry or return an error)
return Err((
StatusCode::INTERNAL_SERVER_ERROR,
format!("Failed to get file type: {}", err),
));
}
};
if file_type.is_dir() {
if let Some(name) = entry.file_name().to_str() {
datasets.push(name.to_string());
}
}
}
}
Err(_) => {
// If the directory doesn't exist, skip this namespace
continue;
}
}
}
// Return the list of datasets
Ok(Json(Datasets { datasets }))
}