首次提交
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
10
.idea/.gitignore
generated
vendored
Normal file
10
.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
# 默认忽略的文件
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# 已忽略包含查询文件的默认文件夹
|
||||
/queries/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
# 基于编辑器的 HTTP 客户端请求
|
||||
/httpRequests/
|
||||
11
.idea/DnfUtils.iml
generated
Normal file
11
.idea/DnfUtils.iml
generated
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="EMPTY_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/DnfUtils.iml" filepath="$PROJECT_DIR$/.idea/DnfUtils.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
1551
Cargo.lock
generated
Normal file
1551
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
19
Cargo.toml
Normal file
19
Cargo.toml
Normal file
@@ -0,0 +1,19 @@
|
||||
[package]
|
||||
name = "DnfUtils"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[lib]
|
||||
name = "utils"
|
||||
crate-type = ["staticlib"]
|
||||
|
||||
[dependencies]
|
||||
cxx = "1.0.192"
|
||||
futures-util = "0.3.31"
|
||||
indicatif = "0.18.3"
|
||||
libc = "0.2.180"
|
||||
reqwest = { version = "0.13.1", default-features = false, features = ["stream", "native-tls"] }
|
||||
tokio = { version = "1.49.0", features = ["rt-multi-thread", "full"] }
|
||||
|
||||
[build-dependencies]
|
||||
cxx-build = "1.0.192"
|
||||
6
build.rs
Normal file
6
build.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
fn main() {
|
||||
cxx_build::bridge("src/lib.rs")
|
||||
.std("c++20")
|
||||
.compile("dnf_utils");
|
||||
println!("cargo:rerun-if-changed=src/lib.rs");
|
||||
}
|
||||
27
src/lib.rs
Normal file
27
src/lib.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
use tokio::runtime::Runtime;
|
||||
|
||||
mod network;
|
||||
|
||||
#[cxx::bridge]
|
||||
mod ffi {
|
||||
extern "Rust" {
|
||||
fn download_file(url: &str, path: &str) -> Result<()>;
|
||||
|
||||
fn http_get(url: &str) -> Result<String>;
|
||||
}
|
||||
}
|
||||
|
||||
fn get_runtime() -> Runtime {
|
||||
Runtime::new().expect("创建Tokio运行时失败")
|
||||
}
|
||||
|
||||
fn download_file(url: &str, path: &str) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let rt = get_runtime();
|
||||
rt.block_on(network::download_file(url, path))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn http_get(url: &str) -> Result<String, Box<dyn std::error::Error>> {
|
||||
let rt = get_runtime();
|
||||
rt.block_on(network::http_get(url))
|
||||
}
|
||||
62
src/network/mod.rs
Normal file
62
src/network/mod.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
use futures_util::StreamExt;
|
||||
use indicatif::{ProgressBar, ProgressStyle};
|
||||
use reqwest::Client;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test() {
|
||||
download_file(
|
||||
"http://114.66.26.35:999/Driver/Key=2Nn77I0E3B9Q4Dy09I5St67Iw1/",
|
||||
"read.bin",
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
/// 从url下载文件
|
||||
pub async fn download_file(url: &str, path: &str) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let client = Client::new();
|
||||
let response = client.get(url).send().await?;
|
||||
|
||||
// 获取文件总大小(如果服务器提供)
|
||||
let total_size = response.content_length().unwrap_or(0);
|
||||
|
||||
// 创建进度条
|
||||
//let pb = ProgressBar::new(total_size);
|
||||
|
||||
let pb = ProgressBar::with_draw_target(
|
||||
Some(total_size),
|
||||
indicatif::ProgressDrawTarget::stdout_with_hz(20)
|
||||
);
|
||||
|
||||
pb.set_style(
|
||||
ProgressStyle::default_bar()
|
||||
.template("{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {bytes}/{total_bytes} ({eta})")?
|
||||
.progress_chars("#>-"),
|
||||
);
|
||||
|
||||
// 创建输出文件
|
||||
let mut file = File::create(path)?;
|
||||
let mut downloaded: u64 = 0;
|
||||
|
||||
// 流式下载
|
||||
let mut stream = response.bytes_stream();
|
||||
while let Some(chunk) = stream.next().await {
|
||||
let chunk = chunk?;
|
||||
file.write_all(&chunk)?;
|
||||
|
||||
downloaded += chunk.len() as u64;
|
||||
pb.set_position(downloaded);
|
||||
}
|
||||
|
||||
pb.finish_with_message("下载完成");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// http/https请求获取远程数据
|
||||
pub async fn http_get(url: &str) -> Result<String, Box<dyn std::error::Error>> {
|
||||
let client = Client::new();
|
||||
let response = client.get(url).send().await?;
|
||||
Ok(response.text().await?)
|
||||
}
|
||||
Reference in New Issue
Block a user