feat: add uploader
This commit is contained in:
@@ -84,6 +84,16 @@ pub async fn delete_files(
|
||||
Ok(Json(json!({ "deleted": deleted })))
|
||||
}
|
||||
|
||||
pub async fn list_my_files(
|
||||
State(state): State<Arc<AppState>>,
|
||||
auth: AuthUser,
|
||||
) -> Result<Json<FilesMetaResponse>, AppError> {
|
||||
let files = storage::list_user_files(&state.config.data_dir, &auth.username)
|
||||
.await
|
||||
.map_err(AppError::Io)?;
|
||||
Ok(Json(FilesMetaResponse { files }))
|
||||
}
|
||||
|
||||
pub async fn upload_file(
|
||||
State(state): State<Arc<AppState>>,
|
||||
auth: AuthUser,
|
||||
|
||||
@@ -32,7 +32,7 @@ async fn main() {
|
||||
.route("/admin/files", get(handlers::list_all_files_meta))
|
||||
.route("/admin/files/get", post(handlers::download_files))
|
||||
.route("/admin/files", delete(handlers::delete_files))
|
||||
.route("/files", post(handlers::upload_file))
|
||||
.route("/files", get(handlers::list_my_files).post(handlers::upload_file))
|
||||
.with_state(state);
|
||||
|
||||
let addr = format!("0.0.0.0:{port}");
|
||||
|
||||
@@ -54,6 +54,27 @@ pub async fn hash_file(path: &Path) -> io::Result<String> {
|
||||
Ok(hex::encode(hasher.finalize()))
|
||||
}
|
||||
|
||||
pub async fn list_user_files(data_dir: &Path, username: &str) -> io::Result<Vec<FileInfo>> {
|
||||
let user_dir = files_root(data_dir).join(username);
|
||||
let mut result = Vec::new();
|
||||
|
||||
let mut files = match tokio::fs::read_dir(&user_dir).await {
|
||||
Ok(d) => d,
|
||||
Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(result),
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
|
||||
while let Some(entry) = files.next_entry().await? {
|
||||
if !entry.file_type().await?.is_file() {
|
||||
continue;
|
||||
}
|
||||
let filename = entry.file_name().to_string_lossy().to_string();
|
||||
let sha256 = hash_file(&user_dir.join(&filename)).await?;
|
||||
result.push(FileInfo { username: username.to_string(), filename, sha256 });
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub async fn read_file(data_dir: &Path, username: &str, filename: &str) -> io::Result<Vec<u8>> {
|
||||
tokio::fs::read(files_root(data_dir).join(username).join(filename)).await
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user