feat(bridge,ui): preserve file names and use system opener
- Add mattermost-download-dir custom (default ~/Downloads) - Pass file_name and download_dir in download_file request - Save files with original names in user-configured directory - Open downloaded files via xdg-open (system default application)
This commit is contained in:
@@ -326,3 +326,76 @@ pub async fn handle_send_post(
|
||||
Err(e) => protocol::Response::err(request.id, e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn handle_download_file(
|
||||
app_state: &AppState,
|
||||
data_dir: &Path,
|
||||
request: protocol::Request,
|
||||
) -> protocol::Response {
|
||||
tracing::debug!("Received download_file request: {:?}", request.params);
|
||||
let params = match request.params.as_object() {
|
||||
Some(p) => p,
|
||||
None => return protocol_err(request.id, "params must be an object"),
|
||||
};
|
||||
let server_id = get_server_id(params);
|
||||
let file_id = match params.get("file_id").and_then(|v| v.as_str()) {
|
||||
Some(id) => id,
|
||||
None => return protocol_err(request.id, "missing file_id"),
|
||||
};
|
||||
let file_name = params
|
||||
.get("file_name")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or_else(|| "download.bin");
|
||||
let download_dir = params
|
||||
.get("download_dir")
|
||||
.and_then(|v| v.as_str())
|
||||
.map(|s| s.to_string())
|
||||
.unwrap_or_else(|| data_dir.join("downloads").to_string_lossy().to_string());
|
||||
|
||||
let client = match get_client(app_state, &server_id) {
|
||||
Ok(c) => c,
|
||||
Err(e) => return protocol::Response::err(request.id, e),
|
||||
};
|
||||
let token = match get_token(app_state, &server_id) {
|
||||
Ok(t) => t,
|
||||
Err(e) => return protocol::Response::err(request.id, e),
|
||||
};
|
||||
|
||||
match client.download_file(&token, file_id).await {
|
||||
Ok(bytes) => {
|
||||
let dir = std::path::PathBuf::from(&download_dir);
|
||||
if let Err(e) = std::fs::create_dir_all(&dir) {
|
||||
return protocol::Response::err(
|
||||
request.id,
|
||||
mm_core::BridgeError::new(
|
||||
mm_core::ErrorKind::InternalError,
|
||||
format!("Failed to create download dir: {}", e),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
let file_path = dir.join(file_name);
|
||||
|
||||
if let Err(e) = std::fs::write(&file_path, &bytes) {
|
||||
return protocol::Response::err(
|
||||
request.id,
|
||||
mm_core::BridgeError::new(
|
||||
mm_core::ErrorKind::InternalError,
|
||||
format!("Failed to write file: {}", e),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
protocol::Response::ok(
|
||||
request.id,
|
||||
serde_json::json!({
|
||||
"server_id": server_id,
|
||||
"file_id": file_id,
|
||||
"local_path": file_path.to_string_lossy(),
|
||||
"size": bytes.len(),
|
||||
}),
|
||||
)
|
||||
}
|
||||
Err(e) => protocol::Response::err(request.id, e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,9 @@ impl App {
|
||||
"open_self_dm" => handlers::handle_open_self_dm(&self.state, request).await,
|
||||
"get_history" => handlers::handle_get_history(&self.state, request).await,
|
||||
"send_post" => handlers::handle_send_post(&self.state, request).await,
|
||||
"download_file" => {
|
||||
handlers::handle_download_file(&self.state, &self.data_dir, request).await
|
||||
}
|
||||
_ => protocol::Response::err(
|
||||
request.id,
|
||||
BridgeError::new(
|
||||
|
||||
Reference in New Issue
Block a user