Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unable edit video and progress bar not loading #118

Merged
merged 3 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ biliup = { git = "https://github.com/ForgQi/biliup-rs.git", rev = "20b2b8c" }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
bytes = "1.6.0"
dirs-next = "2.0.0"
tracing-appender = "0.2.3"
tauri-plugin-fs = "2.0.0-beta.7"
tauri-plugin-http = "2.0.0-beta.7"
Expand Down
37 changes: 19 additions & 18 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use tokio::sync::mpsc::UnboundedSender;

use std::sync::{Arc, RwLock};
use biliup::uploader::bilibili::BiliBili;
use tauri::Manager;

pub mod error;

Expand All @@ -22,23 +23,23 @@ pub struct Credential {
}

impl Credential {
pub async fn get_current_user_credential(&self) -> error::Result<Arc<BiliBili>> {
pub async fn get_current_user_credential(&self, app: &tauri::AppHandle) -> error::Result<Arc<BiliBili>> {
{
let read_guard = self.credential.read().unwrap();
if !read_guard.is_none() {
return Ok(read_guard.as_ref().unwrap().clone());
}
}
let login_info = login_by_cookies(cookie_file()?).await?;
let login_info = login_by_cookies(cookie_file(&app)?).await?;
let myinfo: serde_json::Value = login_info
.client
.get("https://api.bilibili.com/x/space/myinfo")
.send()
.await?
.json()
.await?;
let user = config_path()?.join(format!("users/{}.json", myinfo["data"]["mid"]));
user_path(user).await?;
let user = config_path(&app)?.join(format!("users/{}.json", myinfo["data"]["mid"]));
user_path(app, user).await?;
let arc = Arc::new(login_info);
*self.credential.write().unwrap() = Some(arc.clone());
Ok(arc)
Expand All @@ -49,18 +50,18 @@ impl Credential {
}
}

pub fn config_file() -> error::Result<PathBuf> {
Ok(config_path()?.join("config.yaml"))
pub fn config_file(app: &tauri::AppHandle) -> error::Result<PathBuf> {
Ok(config_path(app)?.join("config.yaml"))
}

pub fn cookie_file() -> error::Result<PathBuf> {
Ok(config_path()?.join("cookies.json"))
pub fn cookie_file(app: &tauri::AppHandle) -> error::Result<PathBuf> {
Ok(config_path(app)?.join("cookies.json"))
}

pub fn config_path() -> error::Result<PathBuf> {
pub fn config_path(app: &tauri::AppHandle) -> error::Result<PathBuf> {
// TODO: maybe use tauri's PathResolver
let mut config_dir: PathBuf = dirs_next::config_dir()
.ok_or_else(|| error::Error::Err("config_dir".to_string()))?;
let mut config_dir: PathBuf = app.path().config_dir().unwrap();
// .ok_or_else(|| error::Error::Err("config_dir".to_string()))?;
config_dir.push("biliup");
if !config_dir.exists() {
std::fs::create_dir(&config_dir)?;
Expand All @@ -69,20 +70,20 @@ pub fn config_path() -> error::Result<PathBuf> {
Ok(config_dir)
}

pub async fn user_path(path: PathBuf) -> error::Result<PathBuf> {
let mut users = config_path()?;
pub async fn user_path(app: &tauri::AppHandle, path: PathBuf) -> error::Result<PathBuf> {
let mut users = config_path(app)?;
users.push("users");
if !users.exists() {
std::fs::create_dir(&users)?;
}
std::fs::copy(cookie_file()?, &path)?;
std::fs::copy(cookie_file(app)?, &path)?;
println!("user_path: {path:?}");
Ok(users)
}

pub async fn login_by_password(username: &str, password: &str) -> anyhow::Result<()> {
pub async fn login_by_password(app: &tauri::AppHandle, username: &str, password: &str) -> anyhow::Result<()> {
let info = BiliCredential::default().login_by_password(username, password).await?;
let file = std::fs::File::create(cookie_file()?)?;
let file = std::fs::File::create(cookie_file(app)?)?;
serde_json::to_writer_pretty(&file, &info)?;
println!("密码登录成功,数据保存在{:?}", file);
Ok(())
Expand All @@ -108,7 +109,7 @@ impl Progressbar {
Self { bytes, tx }
}

pub fn progress(&mut self) -> crate::error::Result<Option<Bytes>> {
pub fn progress(&mut self) -> error::Result<Option<Bytes>> {
let pb = &self.tx;

let content_bytes = &mut self.bytes;
Expand All @@ -129,7 +130,7 @@ impl Progressbar {
}

impl Stream for Progressbar {
type Item = crate::error::Result<Bytes>;
type Item = error::Result<Bytes>;

fn poll_next(
mut self: Pin<&mut Self>,
Expand Down
67 changes: 36 additions & 31 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,23 @@ use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::{filter::LevelFilter, prelude::*, Layer, Registry};

#[tauri::command]
fn login(username: &str, password: &str, remember_me: bool) -> Result<String> {
async_runtime::block_on(login_by_password(username, password))?;
fn login(app: tauri::AppHandle, username: &str, password: &str, remember_me: bool) -> Result<String> {
async_runtime::block_on(login_by_password(&app, username, password))?;
if remember_me {
match load() {
match load(app.clone()) {
Ok(mut config) => {
if let Some(ref mut user) = config.user {
user.account.username = username.into();
user.account.password = password.into();
}
save(config)?;
save(app.clone(), config)?;
// let file = std::fs::File::create(config_file()?).with_context(|| "open config.yaml")?;
// serde_yaml::to_writer(file, &config).with_context(|| "write config.yaml")?
}
Err(_) => {
// let file = std::fs::File::create("config.yaml").with_context(|| "create config.yaml")?;
save(Config {
save(app.clone(),
Config {
user: Some(User {
account: Account {
username: username.into(),
Expand All @@ -62,15 +63,15 @@ fn logout(credential: tauri::State<'_, Credential>) {
}

#[tauri::command]
async fn login_by_cookie(credential: tauri::State<'_, Credential>) -> Result<String> {
credential.get_current_user_credential().await?;
async fn login_by_cookie(app: tauri::AppHandle, credential: tauri::State<'_, Credential>) -> Result<String> {
credential.get_current_user_credential(&app).await?;
Ok("登录成功".into())
}

#[tauri::command]
async fn login_by_sms(code: u32, res: serde_json::Value) -> Result<String> {
async fn login_by_sms(app: tauri::AppHandle, code: u32, res: serde_json::Value) -> Result<String> {
let info = BiliCredential::new().login_by_sms(code, res).await?;
let file = std::fs::File::create(cookie_file()?)?;
let file = std::fs::File::create(cookie_file(&app)?)?;
serde_json::to_writer_pretty(&file, &info)?;
println!("短信登录成功,数据保存在{:?}", file);
Ok("登录成功".into())
Expand All @@ -83,9 +84,9 @@ async fn send_sms(country_code: u32, phone: u64) -> Result<serde_json::Value> {
}

#[tauri::command]
async fn login_by_qrcode(res: serde_json::Value) -> Result<String> {
async fn login_by_qrcode(app: tauri::AppHandle, res: serde_json::Value) -> Result<String> {
let info = BiliCredential::new().login_by_qrcode(res).await?;
let file = std::fs::File::create(cookie_file()?)?;
let file = std::fs::File::create(cookie_file(&app)?)?;
serde_json::to_writer_pretty(&file, &info)?;
println!("链接登录成功,数据保存在{:?}", file);
Ok("登录成功".into())
Expand Down Expand Up @@ -122,13 +123,14 @@ async fn get_qrcode() -> Result<serde_json::Value> {

#[tauri::command]
async fn upload(
app: tauri::AppHandle,
mut video: Video,
window: Window,
credential: tauri::State<'_, Credential>,
) -> Result<Video> {
let bili = &*credential.get_current_user_credential().await?;
let bili = &*credential.get_current_user_credential(&app).await?;

let config = load()?;
let config = load(app.clone())?;
let probe = if let Some(line) = config.line {
match line.as_str() {
"kodo" => line::kodo(),
Expand Down Expand Up @@ -196,23 +198,24 @@ async fn upload(

#[tauri::command]
async fn submit(
app: tauri::AppHandle,
studio: Studio,
credential: tauri::State<'_, Credential>,
) -> Result<serde_json::Value> {
let login_info = &*credential.get_current_user_credential().await?;
let login_info = &*credential.get_current_user_credential(&app).await?;
let ret = login_info.submit(&studio).await?;
Ok(ret.data.unwrap())
}

#[tauri::command]
async fn archive_pre(credential: tauri::State<'_, Credential>) -> Result<serde_json::Value> {
let login_info = &*credential.get_current_user_credential().await?;
async fn archive_pre(app: tauri::AppHandle, credential: tauri::State<'_, Credential>) -> Result<serde_json::Value> {
let login_info = &*credential.get_current_user_credential(&app).await?;
Ok(login_info.archive_pre().await?)
}

#[tauri::command]
async fn get_myinfo(credential: tauri::State<'_, Credential>) -> Result<serde_json::Value> {
let login_info = &*credential.get_current_user_credential().await?;
async fn get_myinfo(app: tauri::AppHandle, credential: tauri::State<'_, Credential>) -> Result<serde_json::Value> {
let login_info = &*credential.get_current_user_credential(&app).await?;
Ok(login_info
.client
.get("https://api.bilibili.com/x/space/myinfo")
Expand All @@ -223,10 +226,10 @@ async fn get_myinfo(credential: tauri::State<'_, Credential>) -> Result<serde_js
}

#[tauri::command]
async fn get_others_myinfo(file_name: String) -> Result<serde_json::Value> {
async fn get_others_myinfo(app: tauri::AppHandle, file_name: String) -> Result<serde_json::Value> {
println!("file_name {:?}", file_name);

let login_info = login_by_cookies(config_path()?.join(file_name)).await?;
let login_info = login_by_cookies(config_path(&app)?.join(file_name)).await?;

Ok(login_info
.client
Expand All @@ -238,32 +241,33 @@ async fn get_others_myinfo(file_name: String) -> Result<serde_json::Value> {
}

#[tauri::command]
fn load_account() -> Result<User> {
load()?
fn load_account(app: tauri::AppHandle) -> Result<User> {
load(app)?
.user
.ok_or_else(|| Error::Err("账号信息不存在".into()))
}

#[tauri::command]
fn save(config: Config) -> Result<Config> {
let file = std::fs::File::create(config_file()?)?;
fn save(app: tauri::AppHandle, config: Config) -> Result<Config> {
let file = std::fs::File::create(config_file(&app)?)?;
serde_yaml::to_writer(file, &config)?;
Ok(config)
}

#[tauri::command]
fn load() -> Result<Config> {
let file = std::fs::File::open(config_file()?).with_context(|| "biliup/config.yaml")?;
fn load(app: tauri::AppHandle) -> Result<Config> {
let file = std::fs::File::open(config_file(&app)?).with_context(|| "biliup/config.yaml")?;
let config: Config = serde_yaml::from_reader(file)?;
Ok(config)
}

#[tauri::command]
async fn cover_up(
app: tauri::AppHandle,
input: Cow<'_, [u8]>,
credential: tauri::State<'_, Credential>,
) -> Result<String> {
let bili = &*credential.get_current_user_credential().await?;
let bili = &*credential.get_current_user_credential(&app).await?;
let url = bili.cover_up(&input).await?;
Ok(url)
}
Expand All @@ -274,8 +278,8 @@ fn is_vid(input: &str) -> bool {
}

#[tauri::command]
async fn show_video(input: &str, credential: tauri::State<'_, Credential>) -> Result<Studio> {
let login_info = &*credential.get_current_user_credential().await?;
async fn show_video(app: tauri::AppHandle, input: &str, credential: tauri::State<'_, Credential>) -> Result<Studio> {
let login_info = &*credential.get_current_user_credential(&app).await?;
let data = login_info
.video_data(&biliup::uploader::bilibili::Vid::from_str(input)?)
.await?;
Expand All @@ -296,10 +300,11 @@ async fn show_video(input: &str, credential: tauri::State<'_, Credential>) -> Re

#[tauri::command]
async fn edit_video(
app: tauri::AppHandle,
studio: Studio,
credential: tauri::State<'_, Credential>,
) -> Result<serde_json::Value> {
let ret = credential.get_current_user_credential().await?.edit(&studio).await?;
let ret = credential.get_current_user_credential(&app).await?.edit(&studio).await?;
Ok(ret)
}

Expand All @@ -319,7 +324,7 @@ fn main() {
let stdout_log = tracing_subscriber::fmt::layer()
.pretty()
.with_filter(LevelFilter::INFO);
let file_appender = tracing_appender::rolling::never(config_path()?, "biliup.log");
let file_appender = tracing_appender::rolling::never(config_path(app.handle())?, "biliup.log");
// let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);
let file_layer = tracing_subscriber::fmt::layer()
.with_ansi(false)
Expand Down
8 changes: 5 additions & 3 deletions src/Progress.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@
$: {
$fitSpeed = speed;
$fitProgress = progress;
// console.log("speed", speed);
// console.log("progress", progress);
}
const current = getCurrent();

function strToHexCharCode(str) {
function strToHexCharCode(str: string): string {
if(str === "")
return "";
var hexCharCode = [];
for(var i = 0; i < str.length; i++) {
let hexCharCode = [];
for(let i = 0; i < str.length; i++) {
hexCharCode.push((str.charCodeAt(i)).toString(16));
}
return hexCharCode.join("");
Expand Down
30 changes: 13 additions & 17 deletions src/Upload.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,15 @@
let autoSubmit = false;
$: autoSubmit = !!selectedTemplate?.submitCallback;
function submitCallback() {
// if (!checkInputFields(true)) {
// createPop("部分内容长度不符合要求,无法提交", 5000);
// return;
// }
console.log("submitCallback()");
console.log("selectedTemplate", selectedTemplate);

selectedTemplate.videos = selectedTemplate.files;
let dtime = null;
let noreprint = null;
if (isDtime) {
dtime = new Date(`${date} ${time}`).valueOf()/1000;
}

if ($currentTemplate.selectedTemplate.copyright == CopyrightType.original) {
noreprint = noReprint ? 1 : 0;
}
if (selectedTemplate.desc){
// <input maxlength={} /> will only limits new inputs, if limitation changes, the existing value will not be automatically truncated
selectedTemplate.desc = selectedTemplate.desc.substring(0, contentLimitation.descriptionLengthByZone(selectedTemplate.tid));
Expand All @@ -152,15 +146,17 @@
msg = '投稿';
hires_params = { lossless_music: isHiRes ? 1 : 0 };
}
invoke(invokeMethod, {
studio: {
...selectedTemplate,
tag: tags.join(','),
dtime: dtime,
no_reprint: noreprint,
...hires_params,
}
})
let invokeArgs = {
studio: {
...selectedTemplate,
tag: tags.join(','),
dtime: dtime,
no_reprint: $currentTemplate.selectedTemplate.copyright == CopyrightType.original && noReprint ? 1 : 0,
...hires_params,
}
};
console.log("invokeArgs", invokeArgs);
invoke(invokeMethod, invokeArgs)
.then((res) => {
console.log("res", res);
if (typeof res == "object" && "bvid" in res) {
Expand Down
2 changes: 1 addition & 1 deletion src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type SelectedTemplate = {
desc: string,
dynamic: string,
files: {
completed: boolean,
complete: boolean,
desc: string,
filename: string,
id: string,
Expand Down
Loading