chore: import MCTF services
- Added all seven issued service trees without omissions. - Preserved the supplied runtime configuration, binaries, and assets.
This commit is contained in:
commit
13f936bee9
1222 files changed
+21280
No files matched your search
Executable
+50
@@ -0,0 +1,50 @@
|
||||
use crate::{
|
||||
dto::{AuthRequest, AuthResponse, ChangePasswordRequest, MeResponse},
|
||||
error::ApiError,
|
||||
extractors::{AuthHeader, CurrentUser},
|
||||
services,
|
||||
};
|
||||
use axum::{extract::State, Json};
|
||||
use sqlx::PgPool;
|
||||
|
||||
pub async fn login(
|
||||
State(pool): State<PgPool>,
|
||||
Json(req): Json<AuthRequest>,
|
||||
) -> Result<Json<AuthResponse>, ApiError> {
|
||||
let response = services::auth::login(&pool, req).await?;
|
||||
|
||||
Ok(Json(response))
|
||||
}
|
||||
|
||||
pub async fn register(
|
||||
State(pool): State<PgPool>,
|
||||
Json(req): Json<AuthRequest>,
|
||||
) -> Result<Json<AuthResponse>, ApiError> {
|
||||
let response = services::auth::register(&pool, req).await?;
|
||||
|
||||
Ok(Json(response))
|
||||
}
|
||||
|
||||
pub async fn change_password(
|
||||
user: CurrentUser,
|
||||
Json(req): Json<ChangePasswordRequest>,
|
||||
) -> Result<(), ApiError> {
|
||||
services::auth::change_password(user.0, req).await
|
||||
}
|
||||
|
||||
pub async fn logout(State(pool): State<PgPool>, auth: AuthHeader) -> Result<(), ApiError> {
|
||||
let token = auth.token.ok_or(ApiError::Unauthorized)?;
|
||||
|
||||
services::auth::logout(&pool, token).await
|
||||
}
|
||||
|
||||
pub async fn me(
|
||||
State(pool): State<PgPool>,
|
||||
auth: AuthHeader,
|
||||
) -> Result<Json<MeResponse>, ApiError> {
|
||||
let token = auth.token.ok_or(ApiError::Unauthorized)?;
|
||||
|
||||
let me = services::auth::me(&pool, token).await?;
|
||||
|
||||
Ok(Json(me))
|
||||
}
|
||||
Executable
+192
@@ -0,0 +1,192 @@
|
||||
use crate::{dto::FileResponse, error::ApiError, extractors::CurrentUser, services};
|
||||
use axum::{
|
||||
body::Body,
|
||||
extract::{Multipart, Path, Query, State},
|
||||
http::header,
|
||||
response::Response,
|
||||
Json,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use sqlx::PgPool;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct ListFilesQuery {
|
||||
pub page: Option<u32>,
|
||||
}
|
||||
|
||||
struct UploadedFile {
|
||||
name: String,
|
||||
data: Vec<u8>,
|
||||
is_public: bool,
|
||||
}
|
||||
|
||||
async fn read_multipart(multipart: &mut Multipart) -> Result<UploadedFile, ApiError> {
|
||||
let mut file: Option<(String, Vec<u8>)> = None;
|
||||
let mut is_public = false;
|
||||
|
||||
while let Some(field) = multipart
|
||||
.next_field()
|
||||
.await
|
||||
.map_err(|_| ApiError::BadRequest("invalid multipart body".to_string()))?
|
||||
{
|
||||
match field.name() {
|
||||
Some("file") => {
|
||||
if file.is_none() {
|
||||
let name = field.file_name().map(str::to_string).unwrap_or_default();
|
||||
let data = field
|
||||
.bytes()
|
||||
.await
|
||||
.map_err(|_| ApiError::BadRequest("failed to read file".to_string()))?;
|
||||
file = Some((name, data.to_vec()));
|
||||
}
|
||||
}
|
||||
Some("public") => {
|
||||
let value = field
|
||||
.text()
|
||||
.await
|
||||
.map_err(|_| ApiError::BadRequest("failed to read field".to_string()))?;
|
||||
is_public = matches!(value.trim(), "true" | "1" | "yes" | "on");
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
let (name, data) =
|
||||
file.ok_or_else(|| ApiError::BadRequest("missing file field".to_string()))?;
|
||||
|
||||
Ok(UploadedFile {
|
||||
name,
|
||||
data,
|
||||
is_public,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn upload(
|
||||
State(pool): State<PgPool>,
|
||||
user: CurrentUser,
|
||||
Path(workspace_id): Path<Uuid>,
|
||||
mut multipart: Multipart,
|
||||
) -> Result<Json<FileResponse>, ApiError> {
|
||||
let uploaded = read_multipart(&mut multipart).await?;
|
||||
let user_id = user.0.id;
|
||||
println!(
|
||||
"[file-upload] started scope=workspace workspace_id={workspace_id} user_id={user_id} size={} public={}",
|
||||
uploaded.data.len(),
|
||||
uploaded.is_public
|
||||
);
|
||||
|
||||
let file = services::files::upload(
|
||||
&pool,
|
||||
user.0,
|
||||
workspace_id,
|
||||
uploaded.name,
|
||||
uploaded.data,
|
||||
uploaded.is_public,
|
||||
)
|
||||
.await
|
||||
.map_err(|err| {
|
||||
println!(
|
||||
"[file-upload] failed scope=workspace workspace_id={workspace_id} user_id={user_id} error={err:?}"
|
||||
);
|
||||
err
|
||||
})?;
|
||||
|
||||
println!(
|
||||
"[file-upload] completed scope=workspace workspace_id={workspace_id} file_id={}",
|
||||
file.id
|
||||
);
|
||||
|
||||
Ok(Json(file))
|
||||
}
|
||||
|
||||
pub async fn upload_personal(
|
||||
State(pool): State<PgPool>,
|
||||
user: CurrentUser,
|
||||
mut multipart: Multipart,
|
||||
) -> Result<Json<FileResponse>, ApiError> {
|
||||
let uploaded = read_multipart(&mut multipart).await?;
|
||||
let user_id = user.0.id;
|
||||
println!(
|
||||
"[file-upload] started scope=personal user_id={user_id} size={} public={}",
|
||||
uploaded.data.len(),
|
||||
uploaded.is_public
|
||||
);
|
||||
|
||||
let file = services::files::upload_personal(
|
||||
&pool,
|
||||
user.0,
|
||||
uploaded.name,
|
||||
uploaded.data,
|
||||
uploaded.is_public,
|
||||
)
|
||||
.await
|
||||
.map_err(|err| {
|
||||
println!("[file-upload] failed scope=personal user_id={user_id} error={err:?}");
|
||||
err
|
||||
})?;
|
||||
|
||||
println!("[file-upload] completed scope=personal file_id={}", file.id);
|
||||
|
||||
Ok(Json(file))
|
||||
}
|
||||
|
||||
pub async fn list(
|
||||
State(pool): State<PgPool>,
|
||||
user: CurrentUser,
|
||||
Path(workspace_id): Path<Uuid>,
|
||||
Query(query): Query<ListFilesQuery>,
|
||||
) -> Result<Json<Vec<FileResponse>>, ApiError> {
|
||||
let files = services::files::list(&pool, user.0, workspace_id, query.page).await?;
|
||||
|
||||
Ok(Json(files))
|
||||
}
|
||||
|
||||
pub async fn list_personal(
|
||||
State(pool): State<PgPool>,
|
||||
user: CurrentUser,
|
||||
Query(query): Query<ListFilesQuery>,
|
||||
) -> Result<Json<Vec<FileResponse>>, ApiError> {
|
||||
let files = services::files::list_personal(&pool, user.0, query.page).await?;
|
||||
|
||||
Ok(Json(files))
|
||||
}
|
||||
|
||||
pub async fn list_public(
|
||||
State(pool): State<PgPool>,
|
||||
Query(query): Query<ListFilesQuery>,
|
||||
) -> Result<Json<Vec<FileResponse>>, ApiError> {
|
||||
let files = services::files::list_public(&pool, query.page).await?;
|
||||
|
||||
Ok(Json(files))
|
||||
}
|
||||
|
||||
pub async fn download(
|
||||
State(pool): State<PgPool>,
|
||||
user: CurrentUser,
|
||||
Path(file_id): Path<Uuid>,
|
||||
) -> Result<Response, ApiError> {
|
||||
let (name, data) = services::files::download(&pool, user.0, file_id).await?;
|
||||
|
||||
let disposition = format!(
|
||||
"attachment; filename=\"{}\"",
|
||||
name.replace('"', "").replace('\r', "").replace('\n', "")
|
||||
);
|
||||
|
||||
let disposition_value = header::HeaderValue::from_str(&disposition)
|
||||
.unwrap_or(header::HeaderValue::from_static("attachment"));
|
||||
|
||||
Response::builder()
|
||||
.header(header::CONTENT_TYPE, "application/octet-stream")
|
||||
.header(header::CONTENT_DISPOSITION, disposition_value)
|
||||
.body(Body::from(data))
|
||||
.map_err(|_| ApiError::Async)
|
||||
}
|
||||
|
||||
pub async fn delete(
|
||||
State(pool): State<PgPool>,
|
||||
user: CurrentUser,
|
||||
Path(file_id): Path<Uuid>,
|
||||
) -> Result<(), ApiError> {
|
||||
services::files::delete(&pool, user.0, file_id).await
|
||||
}
|
||||
Executable
+3
@@ -0,0 +1,3 @@
|
||||
pub mod auth;
|
||||
pub mod files;
|
||||
pub mod workspace;
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
use crate::{
|
||||
dto::{AddUserRequest, MyWorkspaceResponse, NewAuthPolicy, NewWorkspace, WorkspaceResponse},
|
||||
error::ApiError,
|
||||
extractors::CurrentUser,
|
||||
services,
|
||||
};
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
Json,
|
||||
};
|
||||
use sqlx::PgPool;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub async fn get_my(
|
||||
State(pool): State<PgPool>,
|
||||
user: CurrentUser,
|
||||
) -> Result<Json<MyWorkspaceResponse>, ApiError> {
|
||||
let workspace = services::workspaces::get_my(&pool, user.0).await?;
|
||||
|
||||
Ok(Json(workspace))
|
||||
}
|
||||
|
||||
pub async fn create(
|
||||
State(pool): State<PgPool>,
|
||||
user: CurrentUser,
|
||||
Json(req): Json<NewWorkspace>,
|
||||
) -> Result<Json<WorkspaceResponse>, ApiError> {
|
||||
let workspace = services::workspaces::create(&pool, user.0, req).await?;
|
||||
|
||||
Ok(Json(workspace))
|
||||
}
|
||||
|
||||
pub async fn add_user(
|
||||
State(pool): State<PgPool>,
|
||||
user: CurrentUser,
|
||||
Path(workspace_id): Path<Uuid>,
|
||||
Json(req): Json<AddUserRequest>,
|
||||
) -> Result<(), ApiError> {
|
||||
services::workspaces::add_user(&pool, user.0, workspace_id, req).await
|
||||
}
|
||||
|
||||
pub async fn remove_user(
|
||||
State(pool): State<PgPool>,
|
||||
user: CurrentUser,
|
||||
Path((workspace_id, user_id)): Path<(Uuid, Uuid)>,
|
||||
) -> Result<(), ApiError> {
|
||||
services::workspaces::remove_user(&pool, user.0, workspace_id, user_id).await
|
||||
}
|
||||
|
||||
pub async fn add_policy(
|
||||
State(pool): State<PgPool>,
|
||||
user: CurrentUser,
|
||||
Path(workspace_id): Path<Uuid>,
|
||||
Json(req): Json<NewAuthPolicy>,
|
||||
) -> Result<(), ApiError> {
|
||||
services::auth_policies::create(&pool, user.0, workspace_id, req).await
|
||||
}
|
||||
Reference in new issue
Block a user