From 1a2c63e0572850c72b36b9c23c579109658e6d6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B4=BA=E5=87=AF?= <2401470435@qq.com> Date: Mon, 13 Jan 2025 15:54:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=8C=87=E9=92=88=E9=87=8A?= =?UTF-8?q?=E6=94=BE=E5=A4=84=E7=90=86=EF=BC=8C=E4=BF=AE=E6=94=B9url?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/datamgr_route_example/src/main.rs | 2 +- src/datamgr_route/datamgr_api.rs | 9 +++ src/datamgr_route/route.rs | 93 ++++++++++++++-------- 3 files changed, 71 insertions(+), 33 deletions(-) diff --git a/examples/datamgr_route_example/src/main.rs b/examples/datamgr_route_example/src/main.rs index 97835ce..79303f3 100644 --- a/examples/datamgr_route_example/src/main.rs +++ b/examples/datamgr_route_example/src/main.rs @@ -26,7 +26,7 @@ async fn main() { let plugin_manager = Arc::new(PluginManager::new()); let data_plugin_manager = datamgr_api::NewPluginManager(); - let plugin_file_path = CString::new("/root/project/fleet-datamgr/plugins/release").unwrap(); + let plugin_file_path = CString::new("/home/trickster/Downloads/ripple-remastered/plugins/release").unwrap(); datamgr_api::LoadPluginsFromDirectory(data_plugin_manager, plugin_file_path.as_ptr()); route::init_plugin(plugin_manager.clone(), data_plugin_manager); diff --git a/src/datamgr_route/datamgr_api.rs b/src/datamgr_route/datamgr_api.rs index e1705b2..02fe984 100644 --- a/src/datamgr_route/datamgr_api.rs +++ b/src/datamgr_route/datamgr_api.rs @@ -47,6 +47,12 @@ unsafe extern "C" { fileSize: *mut ::std::os::raw::c_int, ) -> *const ::std::os::raw::c_char; } +unsafe extern "C" { + pub fn DeleteFile( + pluginManager: *mut ::std::os::raw::c_void, + fileName: *const ::std::os::raw::c_char, + ) -> ::std::os::raw::c_int; +} unsafe extern "C" { pub fn ListFiles( pluginManager: *mut ::std::os::raw::c_void, @@ -60,6 +66,9 @@ unsafe extern "C" { indexCount: *mut ::std::os::raw::c_int, ) -> *mut *const ::std::os::raw::c_char; } +unsafe extern "C" { + pub fn FreeData(data: *mut *const ::std::os::raw::c_char, indexCount: ::std::os::raw::c_int); +} unsafe extern "C" { pub fn ReceiveNormalData( pluginManager: *mut ::std::os::raw::c_void, diff --git a/src/datamgr_route/route.rs b/src/datamgr_route/route.rs index c6cc326..b41ef0e 100644 --- a/src/datamgr_route/route.rs +++ b/src/datamgr_route/route.rs @@ -3,7 +3,7 @@ use std::ffi::CString; use std::ffi::CStr; use std::os::raw::c_char; use std::sync::Arc; -use crate::cores::apiserver::{CustomRouteProvider, APIServerRoute, AppState}; +use crate::cores::apiserver::{CustomRouteProvider, APIServerRoute}; use crate::cores::plugin::PluginManager; use actix_web::{web, HttpRequest, HttpResponse, Route}; use actix_web::http::Method; @@ -99,7 +99,7 @@ impl DataApiRouteProvider { // * req-header: x-data-status: Status // * res: Ok = success // */ - fn upload_status_handler(request: HttpRequest, body: Bytes) -> HttpResponse { + fn upload_status_handler(request: HttpRequest, _body: Bytes) -> HttpResponse { let header_status = match request.headers().get("x-data-status") { Some(value) => match value.to_str() { @@ -132,7 +132,7 @@ impl DataApiRouteProvider { // * req-header: x-data-instruction: Instruction // * res: Ok = success // */ - fn upload_instruction_handler(request: HttpRequest, body: Bytes) -> HttpResponse { + fn upload_instruction_handler(request: HttpRequest, _body: Bytes) -> HttpResponse { let header_instruction = match request.headers().get("x-data-instruction") { Some(value) => match value.to_str() { @@ -152,7 +152,7 @@ impl DataApiRouteProvider { DATA_PLUGIN_MANAGER, instruction.as_ptr() ); - println!("ret: {:?}", ret); + // println!("ret: {:?}", ret); if ret == 1 { HttpResponse::Ok().body("Upload instruction success") } else { @@ -163,18 +163,13 @@ impl DataApiRouteProvider { // /** // * download file - // * req-header: x-data-file-name: File name + // * {filename}: File name in url // * res-body: File content // */ - fn download_file_handler(request: HttpRequest, body: Bytes) -> HttpResponse { + fn download_file_handler(request: HttpRequest, _body: Bytes) -> HttpResponse { - let header_file_name = match request.headers().get("x-data-file-name") { - Some(value) => match value.to_str() { - Ok(s) => s.to_string(), - Err(_) => return HttpResponse::BadRequest().body("Invalid x-data-file-name header"), - }, - None => return HttpResponse::BadRequest().body("Missing x-data-file-name header"), - }; + let file_name = request.match_info().get("filename").unwrap(); + let header_file_name = file_name.to_string(); let file_name = match CString::new(header_file_name) { Ok(cstr) => cstr, @@ -194,11 +189,37 @@ impl DataApiRouteProvider { } } + // delete file + // {filename}: File name in url + // res: Ok = success + fn delete_file_handler(request: HttpRequest, _body: Bytes) -> HttpResponse { + + let file_name = request.match_info().get("filename").unwrap(); + let header_file_name = file_name.to_string(); + + let file_name = match CString::new(header_file_name) { + Ok(cstr) => cstr, + Err(_) => return HttpResponse::BadRequest().body("Invalid file name"), + }; + + unsafe { + let ret = datamgr_api::DeleteFile( + DATA_PLUGIN_MANAGER, + file_name.as_ptr() + ); + if ret == 1 { + HttpResponse::Ok().body("Delete file success") + } else { + HttpResponse::InternalServerError().finish() + } + } + } + // /** // * list file // * res-body: File list // */ - fn list_file_handler(request: HttpRequest, body: Bytes) -> HttpResponse { + fn list_file_handler(_request: HttpRequest, _body: Bytes) -> HttpResponse { unsafe { let mut index_count: i32 = 0; let ret = datamgr_api::ListFiles(DATA_PLUGIN_MANAGER, &mut index_count); @@ -210,6 +231,10 @@ impl DataApiRouteProvider { let str_slice = std::str::from_utf8(bytes).unwrap(); vec.push(str_slice.to_string()); } + + // free + datamgr_api::FreeData(ret , index_count); + let json_response = match serde_json::to_string(&vec) { Ok(json) => json, Err(e) => { @@ -225,18 +250,13 @@ impl DataApiRouteProvider { // /** // * query file - // * req-header: x-data-file-name: File name + // * {filename}: File name in url // * res-body: File information // */ - fn query_file_handler(request: HttpRequest, body: Bytes) -> HttpResponse { + fn query_file_handler(request: HttpRequest, _body: Bytes) -> HttpResponse { - let header_file_name = match request.headers().get("x-data-file-name") { - Some(value) => match value.to_str() { - Ok(s) => s.to_string(), - Err(_) => return HttpResponse::BadRequest().body("Invalid x-data-file-name header"), - }, - None => return HttpResponse::BadRequest().body("Missing x-data-file-name header"), - }; + let file_name = request.match_info().get("filename").unwrap(); + let header_file_name = file_name.to_string(); let file_name = match CString::new(header_file_name) { Ok(cstr) => cstr, @@ -258,6 +278,10 @@ impl DataApiRouteProvider { let str_slice = std::str::from_utf8(bytes).unwrap(); vec.push(str_slice.to_string()); } + + // free + datamgr_api::FreeData(ret , index_count); + let json_response: Vec<_> = vec.iter().map(|s| { let parts: Vec<&str> = s.split('|').collect(); json!({ @@ -316,7 +340,7 @@ impl DataApiRouteProvider { } } - fn set_sync_and_recycle_strategy_handler(request: HttpRequest, body: Bytes) -> HttpResponse { + fn set_sync_and_recycle_strategy_handler(request: HttpRequest, _body: Bytes) -> HttpResponse { unsafe { println!("set_sync_and_recycle_strategy_handler"); let header_key = request @@ -427,7 +451,7 @@ impl DataApiRouteProvider { } } - fn read_data_handler(request: HttpRequest, body: Bytes) -> HttpResponse { + fn read_data_handler(request: HttpRequest, _body: Bytes) -> HttpResponse { unsafe { println!("read_data_handler"); let header_key = request @@ -493,7 +517,7 @@ impl DataApiRouteProvider { } } - fn get_terminal_info_handler(request: HttpRequest, body: Bytes) -> HttpResponse { + fn get_terminal_info_handler(request: HttpRequest, _body: Bytes) -> HttpResponse { unsafe { println!("get_terminal_info_handler"); let header_user_token = request @@ -532,32 +556,37 @@ impl CustomRouteProvider for DataApiRouteProvider { vec![ Box::new(SimpleRoute { method: Method::POST, - path: "/data/self/file".to_string(), + path: "/data/v1/files".to_string(), handler: Self::upload_file_handler, }), Box::new(SimpleRoute { method: Method::POST, - path: "/data/other/status".to_string(), + path: "/data/v1/status".to_string(), handler: Self::upload_status_handler, }), Box::new(SimpleRoute { method: Method::POST, - path: "/data/user/instruction".to_string(), + path: "/data/v1/instructions".to_string(), handler: Self::upload_instruction_handler, }), Box::new(SimpleRoute { method: Method::GET, - path: "/data/self/file".to_string(), + path: "/data/v1/files/{filename}".to_string(), handler: Self::download_file_handler, }), + Box::new(SimpleRoute { + method: Method::DELETE, + path: "/data/v1/files/{filename}".to_string(), + handler: Self::delete_file_handler, + }), Box::new(SimpleRoute { method: Method::GET, - path: "/data/self/files".to_string(), + path: "/data/v1/files".to_string(), handler: Self::list_file_handler, }), Box::new(SimpleRoute { method: Method::GET, - path: "/data/self/fileinfo".to_string(), + path: "/data/v1/fileinfos/{filename}".to_string(), handler: Self::query_file_handler, }), Box::new(SimpleRoute { -- Gitee