Skip to content

Commit c8f7640

Browse files
1 parent b663678 commit c8f7640

File tree

10 files changed

+1795
-1871
lines changed

10 files changed

+1795
-1871
lines changed

Cargo.toml

+5-5
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ serde_json = { workspace = true }
3232
thiserror = { workspace = true }
3333
tokio = "1"
3434
reqwest = { version = "0.12", default-features = false, features = [
35-
"json",
36-
"stream",
35+
"json",
36+
"stream",
3737
] }
3838
url = { workspace = true }
3939
http = "1"
@@ -49,9 +49,9 @@ percent-encoding = "2.3"
4949
[target."cfg(target_os = \"windows\")".dependencies]
5050
zip = { version = "2", default-features = false, optional = true }
5151
windows-sys = { version = "0.59.0", features = [
52-
"Win32_Foundation",
53-
"Win32_UI_WindowsAndMessaging",
54-
"Win32_UI_Shell",
52+
"Win32_Foundation",
53+
"Win32_UI_WindowsAndMessaging",
54+
"Win32_UI_Shell",
5555
] }
5656

5757
[target."cfg(target_os = \"linux\")".dependencies]

Source/commands.rs

+142-144
Original file line numberDiff line numberDiff line change
@@ -2,181 +2,179 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
use crate::{Result, Update, UpdaterExt};
5+
use std::{str::FromStr, time::Duration};
66

77
use http::{HeaderMap, HeaderName, HeaderValue};
88
use serde::Serialize;
99
use tauri::{ipc::Channel, Manager, Resource, ResourceId, Runtime, Webview};
10-
11-
use std::{str::FromStr, time::Duration};
1210
use url::Url;
1311

12+
use crate::{Result, Update, UpdaterExt};
13+
1414
#[derive(Debug, Clone, Serialize)]
1515
#[serde(tag = "event", content = "data")]
1616
pub enum DownloadEvent {
17-
#[serde(rename_all = "camelCase")]
18-
Started {
19-
content_length: Option<u64>,
20-
},
21-
#[serde(rename_all = "camelCase")]
22-
Progress {
23-
chunk_length: usize,
24-
},
25-
Finished,
17+
#[serde(rename_all = "camelCase")]
18+
Started {
19+
content_length:Option<u64>,
20+
},
21+
#[serde(rename_all = "camelCase")]
22+
Progress {
23+
chunk_length:usize,
24+
},
25+
Finished,
2626
}
2727

2828
#[derive(Serialize, Default)]
2929
#[serde(rename_all = "camelCase")]
3030
pub(crate) struct Metadata {
31-
rid: Option<ResourceId>,
32-
available: bool,
33-
current_version: String,
34-
version: String,
35-
date: Option<String>,
36-
body: Option<String>,
31+
rid:Option<ResourceId>,
32+
available:bool,
33+
current_version:String,
34+
version:String,
35+
date:Option<String>,
36+
body:Option<String>,
3737
}
3838

3939
struct DownloadedBytes(pub Vec<u8>);
4040
impl Resource for DownloadedBytes {}
4141

4242
#[tauri::command]
43-
pub(crate) async fn check<R: Runtime>(
44-
webview: Webview<R>,
45-
headers: Option<Vec<(String, String)>>,
46-
timeout: Option<u64>,
47-
proxy: Option<String>,
48-
target: Option<String>,
43+
pub(crate) async fn check<R:Runtime>(
44+
webview:Webview<R>,
45+
headers:Option<Vec<(String, String)>>,
46+
timeout:Option<u64>,
47+
proxy:Option<String>,
48+
target:Option<String>,
4949
) -> Result<Metadata> {
50-
let mut builder = webview.updater_builder();
51-
if let Some(headers) = headers {
52-
for (k, v) in headers {
53-
builder = builder.header(k, v)?;
54-
}
55-
}
56-
if let Some(timeout) = timeout {
57-
builder = builder.timeout(Duration::from_millis(timeout));
58-
}
59-
if let Some(ref proxy) = proxy {
60-
let url = Url::parse(proxy.as_str())?;
61-
builder = builder.proxy(url);
62-
}
63-
if let Some(target) = target {
64-
builder = builder.target(target);
65-
}
66-
67-
let updater = builder.build()?;
68-
let update = updater.check().await?;
69-
let mut metadata = Metadata::default();
70-
if let Some(update) = update {
71-
metadata.available = true;
72-
metadata.current_version.clone_from(&update.current_version);
73-
metadata.version.clone_from(&update.version);
74-
metadata.date = update.date.map(|d| d.to_string());
75-
metadata.body.clone_from(&update.body);
76-
metadata.rid = Some(webview.resources_table().add(update));
77-
}
78-
79-
Ok(metadata)
50+
let mut builder = webview.updater_builder();
51+
if let Some(headers) = headers {
52+
for (k, v) in headers {
53+
builder = builder.header(k, v)?;
54+
}
55+
}
56+
if let Some(timeout) = timeout {
57+
builder = builder.timeout(Duration::from_millis(timeout));
58+
}
59+
if let Some(ref proxy) = proxy {
60+
let url = Url::parse(proxy.as_str())?;
61+
builder = builder.proxy(url);
62+
}
63+
if let Some(target) = target {
64+
builder = builder.target(target);
65+
}
66+
67+
let updater = builder.build()?;
68+
let update = updater.check().await?;
69+
let mut metadata = Metadata::default();
70+
if let Some(update) = update {
71+
metadata.available = true;
72+
metadata.current_version.clone_from(&update.current_version);
73+
metadata.version.clone_from(&update.version);
74+
metadata.date = update.date.map(|d| d.to_string());
75+
metadata.body.clone_from(&update.body);
76+
metadata.rid = Some(webview.resources_table().add(update));
77+
}
78+
79+
Ok(metadata)
8080
}
8181

8282
#[tauri::command]
83-
pub(crate) async fn download<R: Runtime>(
84-
webview: Webview<R>,
85-
rid: ResourceId,
86-
on_event: Channel<DownloadEvent>,
87-
headers: Option<Vec<(String, String)>>,
88-
timeout: Option<u64>,
83+
pub(crate) async fn download<R:Runtime>(
84+
webview:Webview<R>,
85+
rid:ResourceId,
86+
on_event:Channel<DownloadEvent>,
87+
headers:Option<Vec<(String, String)>>,
88+
timeout:Option<u64>,
8989
) -> Result<ResourceId> {
90-
let update = webview.resources_table().get::<Update>(rid)?;
91-
92-
let mut update = (*update).clone();
93-
94-
if let Some(headers) = headers {
95-
let mut map = HeaderMap::new();
96-
for (k, v) in headers {
97-
map.append(HeaderName::from_str(&k)?, HeaderValue::from_str(&v)?);
98-
}
99-
update.headers = map;
100-
}
101-
102-
if let Some(timeout) = timeout {
103-
update.timeout = Some(Duration::from_millis(timeout));
104-
}
105-
106-
let mut first_chunk = true;
107-
let bytes = update
108-
.download(
109-
|chunk_length, content_length| {
110-
if first_chunk {
111-
first_chunk = !first_chunk;
112-
let _ = on_event.send(DownloadEvent::Started { content_length });
113-
}
114-
let _ = on_event.send(DownloadEvent::Progress { chunk_length });
115-
},
116-
|| {
117-
let _ = on_event.send(DownloadEvent::Finished);
118-
},
119-
)
120-
.await?;
121-
122-
Ok(webview.resources_table().add(DownloadedBytes(bytes)))
90+
let update = webview.resources_table().get::<Update>(rid)?;
91+
92+
let mut update = (*update).clone();
93+
94+
if let Some(headers) = headers {
95+
let mut map = HeaderMap::new();
96+
for (k, v) in headers {
97+
map.append(HeaderName::from_str(&k)?, HeaderValue::from_str(&v)?);
98+
}
99+
update.headers = map;
100+
}
101+
102+
if let Some(timeout) = timeout {
103+
update.timeout = Some(Duration::from_millis(timeout));
104+
}
105+
106+
let mut first_chunk = true;
107+
let bytes = update
108+
.download(
109+
|chunk_length, content_length| {
110+
if first_chunk {
111+
first_chunk = !first_chunk;
112+
let _ = on_event.send(DownloadEvent::Started { content_length });
113+
}
114+
let _ = on_event.send(DownloadEvent::Progress { chunk_length });
115+
},
116+
|| {
117+
let _ = on_event.send(DownloadEvent::Finished);
118+
},
119+
)
120+
.await?;
121+
122+
Ok(webview.resources_table().add(DownloadedBytes(bytes)))
123123
}
124124

125125
#[tauri::command]
126-
pub(crate) async fn install<R: Runtime>(
127-
webview: Webview<R>,
128-
update_rid: ResourceId,
129-
bytes_rid: ResourceId,
126+
pub(crate) async fn install<R:Runtime>(
127+
webview:Webview<R>,
128+
update_rid:ResourceId,
129+
bytes_rid:ResourceId,
130130
) -> Result<()> {
131-
let update = webview.resources_table().get::<Update>(update_rid)?;
132-
let bytes = webview
133-
.resources_table()
134-
.get::<DownloadedBytes>(bytes_rid)?;
135-
update.install(&bytes.0)?;
136-
let _ = webview.resources_table().close(bytes_rid);
137-
Ok(())
131+
let update = webview.resources_table().get::<Update>(update_rid)?;
132+
let bytes = webview.resources_table().get::<DownloadedBytes>(bytes_rid)?;
133+
update.install(&bytes.0)?;
134+
let _ = webview.resources_table().close(bytes_rid);
135+
Ok(())
138136
}
139137

140138
#[tauri::command]
141-
pub(crate) async fn download_and_install<R: Runtime>(
142-
webview: Webview<R>,
143-
rid: ResourceId,
144-
on_event: Channel<DownloadEvent>,
145-
headers: Option<Vec<(String, String)>>,
146-
timeout: Option<u64>,
139+
pub(crate) async fn download_and_install<R:Runtime>(
140+
webview:Webview<R>,
141+
rid:ResourceId,
142+
on_event:Channel<DownloadEvent>,
143+
headers:Option<Vec<(String, String)>>,
144+
timeout:Option<u64>,
147145
) -> Result<()> {
148-
let update = webview.resources_table().get::<Update>(rid)?;
149-
150-
let mut update = (*update).clone();
151-
152-
if let Some(headers) = headers {
153-
let mut map = HeaderMap::new();
154-
for (k, v) in headers {
155-
map.append(HeaderName::from_str(&k)?, HeaderValue::from_str(&v)?);
156-
}
157-
update.headers = map;
158-
}
159-
160-
if let Some(timeout) = timeout {
161-
update.timeout = Some(Duration::from_millis(timeout));
162-
}
163-
164-
let mut first_chunk = true;
165-
166-
update
167-
.download_and_install(
168-
|chunk_length, content_length| {
169-
if first_chunk {
170-
first_chunk = !first_chunk;
171-
let _ = on_event.send(DownloadEvent::Started { content_length });
172-
}
173-
let _ = on_event.send(DownloadEvent::Progress { chunk_length });
174-
},
175-
|| {
176-
let _ = on_event.send(DownloadEvent::Finished);
177-
},
178-
)
179-
.await?;
180-
181-
Ok(())
146+
let update = webview.resources_table().get::<Update>(rid)?;
147+
148+
let mut update = (*update).clone();
149+
150+
if let Some(headers) = headers {
151+
let mut map = HeaderMap::new();
152+
for (k, v) in headers {
153+
map.append(HeaderName::from_str(&k)?, HeaderValue::from_str(&v)?);
154+
}
155+
update.headers = map;
156+
}
157+
158+
if let Some(timeout) = timeout {
159+
update.timeout = Some(Duration::from_millis(timeout));
160+
}
161+
162+
let mut first_chunk = true;
163+
164+
update
165+
.download_and_install(
166+
|chunk_length, content_length| {
167+
if first_chunk {
168+
first_chunk = !first_chunk;
169+
let _ = on_event.send(DownloadEvent::Started { content_length });
170+
}
171+
let _ = on_event.send(DownloadEvent::Progress { chunk_length });
172+
},
173+
|| {
174+
let _ = on_event.send(DownloadEvent::Finished);
175+
},
176+
)
177+
.await?;
178+
179+
Ok(())
182180
}

0 commit comments

Comments
 (0)