Skip to content

Commit

Permalink
✨ feat(onedrive ): add download setting from oendrive
Browse files Browse the repository at this point in the history
  • Loading branch information
CakeAL committed Dec 7, 2024
1 parent 9367519 commit 3eaf267
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
- [x] 测速功能:可以选择不同测速点,贝壳内网,以及其他教育网 ipv6 测速点进行测速(不消耗校园网流量),同时显示当前公网 IP 地址
- [x] 关于页面:一些作者的废话以及我的捐赠打钱二维码,还有一些小 Tips
- [x] 查别人流量,和查电表
- [x] 通过 Onedrive 进行同步配置文件

## TODO

- [x] 通过校园网 VPN 转换校园网内网相关链接,在校外也可以使用这个 APP
- [x] 迁移框架版本至 Tauri 2
- [x] 更新提醒
- [ ] 前端页面美化
- [x] 前端页面美化
- [ ] 前端加入图表展示,更加直观
- [ ] 密码输入错误超过 3 次之后显示验证码(懒得搞,输错3次密码自己等半小时。)
- [ ] 给应用签名(苹果要钱,不签了)
Expand Down
50 changes: 47 additions & 3 deletions src-tauri/src/onedrive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use sha2::{Digest, Sha256};
use tauri::{Manager, WebviewWindow};
use tauri_plugin_dialog::DialogExt;

use crate::entities::AppState;
use crate::{entities::AppState, setting::Setting};

#[tauri::command(async)]
pub async fn open_microsoft_login(app_handle: tauri::AppHandle) -> Result<(), String> {
Expand Down Expand Up @@ -110,7 +110,7 @@ pub async fn code_to_access_token(

match ans {
true => upload_setting_to_onedrive(&app_handle, token_response).await,
false => download_setting_to_onedrive().await,
false => download_setting_to_onedrive(&app_handle, token_response).await,
}

let _ = window.close();
Expand Down Expand Up @@ -145,7 +145,51 @@ async fn upload_setting_to_onedrive(app_handle: &tauri::AppHandle, token_respons
}
}

async fn download_setting_to_onedrive() {}
async fn download_setting_to_onedrive(
app_handle: &tauri::AppHandle,
token_response: TokenResponse,
) {
let response = Client::new()
.get("https://graph.microsoft.com/v1.0/drive/special/approot:/setting.txt:/content")
.bearer_auth(token_response.access_token.unwrap())
.send()
.await;
if let Err(e) = response {
app_handle
.dialog()
.message("下载失败!可能由于网络问题")
.blocking_show();
println!("Error downloading setting: {}", e);
} else {
let text = response.unwrap().text().await.unwrap();
let text = match URL_SAFE.decode(text.as_bytes()) {
Ok(text) => text,
Err(e) => {
app_handle
.dialog()
.message(format!("解码base64错误:{e:?}"))
.blocking_show();
return;
}
};
let setting: Setting = match serde_json::from_slice(&text) {
Ok(s) => s,
Err(e) => {
app_handle
.dialog()
.message(format!("配置文件格式不正确!{e:?}"))
.blocking_show();
return;
}
};
// dbg!(&setting);
let state = app_handle.state::<AppState>();
*state.setting.write().unwrap() = setting;
let _ = state.setting.write().unwrap().write_setting(app_handle);
app_handle.dialog().message("下载成功!").blocking_show();
// dbg!(response.unwrap().text().await);
}
}

fn generate_random_string(length: usize) -> String {
const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~";
Expand Down

0 comments on commit 3eaf267

Please sign in to comment.