第一个版本!

This commit is contained in:
lkhsss
2025-06-14 17:28:34 +08:00
commit 9797530d68
38 changed files with 11117 additions and 0 deletions

7
src-tauri/.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
# Generated by Cargo
# will have compiled files and executables
/target/
# Generated by Tauri
# will have schema files for capabilities auto-completion
/gen/schemas

5519
src-tauri/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

28
src-tauri/Cargo.toml Normal file
View File

@ -0,0 +1,28 @@
[package]
name = "githubdownloader"
version = "0.1.0"
description = "A Tauri App"
authors = ["you"]
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
# The `_lib` suffix may seem redundant but it is necessary
# to make the lib name unique and wouldn't conflict with the bin name.
# This seems to be only an issue on Windows, see https://github.com/rust-lang/cargo/issues/8519
name = "githubdownloader_lib"
crate-type = ["staticlib", "cdylib", "rlib"]
[build-dependencies]
tauri-build = { version = "2", features = [] }
[dependencies]
tauri = { version = "2", features = [] }
tauri-plugin-opener = "2"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
thiserror = "2.0.12"
url = "2.5.4"
reqwest = { version = "0.12.15", features = ["json"] }

3
src-tauri/build.rs Normal file
View File

@ -0,0 +1,3 @@
fn main() {
tauri_build::build()
}

View File

@ -0,0 +1,10 @@
{
"$schema": "../gen/schemas/desktop-schema.json",
"identifier": "default",
"description": "Capability for the main window",
"windows": ["main"],
"permissions": [
"core:default",
"opener:default"
]
}

BIN
src-tauri/icons/128x128.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB

BIN
src-tauri/icons/32x32.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 974 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 903 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

BIN
src-tauri/icons/icon.icns Normal file

Binary file not shown.

BIN
src-tauri/icons/icon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 85 KiB

BIN
src-tauri/icons/icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

145
src-tauri/src/lib.rs Normal file
View File

@ -0,0 +1,145 @@
use reqwest::header;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use url::{Host, Position, Url};
#[tauri::command]
async fn handle(url: String) -> GetResult {
let handled_url = match Url::parse(&url) {
Ok(o) => o,
Err(_) => return GetResult::fail("无法解析链接"),
};
if handled_url.host_str() != Some("github.com") {
return GetResult::fail("无法解析除了github之外的服务");
}
let url_data: Vec<_> = handled_url.path().split("/").collect();
let (author, repo) = (url_data[1], url_data[2]);
let data = match get_data(author.to_string(), repo.to_string()).await {
Ok(d) => d,
Err(_) => return GetResult::fail("获取失败"),
};
GetResult::new(data)
}
async fn get_data(author: String, repo: String) -> Result<Vec<Data>, Box<dyn std::error::Error>> {
let url = format!("https://api.github.com/repos/{}/{}/releases", author, repo);
let mut headers = header::HeaderMap::new();
headers.insert(
"Accept",
header::HeaderValue::from_static("application/vnd.github+json'"),
);
headers.insert(
"User-Agent",
header::HeaderValue::from_static("Chrome/122.0.6261.95 Safari/537.36"),
);
let body = reqwest::Client::builder()
.default_headers(headers)
.build()?
.get(url)
.send()
.await?
.json::<Vec<Data>>()
.await?;
// println!("body = {body:?}");
Ok(body)
}
#[derive(Debug, Deserialize, Serialize)]
struct Data {
url: String,
assets_url: String,
upload_url: String,
html_url: String,
id: usize,
author: Author,
assets: Vec<Asset>,
node_id: String,
tag_name: String,
target_commitish: String,
name: String,
draft: bool,
prerelease: bool,
created_at: String,
published_at: String,
tarball_url:String,
zipball_url:String
}
#[derive(Debug, Deserialize, Serialize)]
struct Author {
login: String,
id: usize,
node_id: String,
avatar_url: String,
gravatar_id: String,
url: String,
html_url: String,
followers_url: String,
following_url: String,
gists_url: String,
starred_url: String,
subscriptions_url: String,
organizations_url: String,
repos_url: String,
events_url: String,
received_events_url: String,
r#type: String,
user_view_type: String,
site_admin: bool,
}
#[derive(Debug, Deserialize, Serialize)]
struct Asset {
url: String,
id: usize,
node_id: String,
name: String,
label: String,
browser_download_url: String,
created_at:String,
updated_at:String,
size:usize
}
#[derive(Error, Debug)]
pub enum Error {
#[error("链接格式错误")]
ParseError(#[from] url::ParseError),
#[error("链接格式错误")]
NotTargetHost,
#[error("unknown data store error")]
Unknown,
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_opener::init())
.invoke_handler(tauri::generate_handler![handle])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
#[derive(Debug, Deserialize, Serialize)]
struct GetResult {
success: bool,
status: String,
data: Option<Vec<Data>>,
}
impl GetResult {
fn new(data: Vec<Data>) -> Self {
Self {
success: true,
status: "获取成功".into(),
data: Some(data),
}
}
fn fail<S: ToString>(s: S) -> Self {
Self {
success: false,
status: s.to_string(),
data: None,
}
}
}

6
src-tauri/src/main.rs Normal file
View File

@ -0,0 +1,6 @@
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
fn main() {
githubdownloader_lib::run()
}

35
src-tauri/tauri.conf.json Normal file
View File

@ -0,0 +1,35 @@
{
"$schema": "https://schema.tauri.app/config/2",
"productName": "githubdownloader",
"version": "0.1.0",
"identifier": "com.lkhsss.githubdownloader",
"build": {
"beforeDevCommand": "npm run dev",
"devUrl": "http://localhost:1420",
"beforeBuildCommand": "npm run build",
"frontendDist": "../dist"
},
"app": {
"windows": [
{
"title": "githubdownloader",
"width": 800,
"height": 600
}
],
"security": {
"csp": null
}
},
"bundle": {
"active": true,
"targets": "all",
"icon": [
"icons/32x32.png",
"icons/128x128.png",
"icons/128x128@2x.png",
"icons/icon.icns",
"icons/icon.ico"
]
}
}