Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
jlvihv committed Jun 9, 2024
1 parent c64e15e commit 637cc29
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 21 deletions.
14 changes: 14 additions & 0 deletions src-tauri/src/kv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ pub mod plan {
Ok(plans)
}

/// 获取一个录制计划
pub fn get(url: String) -> Result<Option<RecordingPlan>> {
let read_txn = db().begin_read()?;
let table = read_txn.open_table(TABLE)?;
let result = table.get(format!("plan:{}", url).as_str())?;
match result {
Some(plan) => {
let plan: RecordingPlan = serde_json::from_slice(&plan.value())?;
Ok(Some(plan))
}
None => Ok(None),
}
}

/// 获取所有启用的录制计划
pub fn get_enabled() -> Result<Vec<RecordingPlan>> {
let read_txn = db().begin_read()?;
Expand Down
5 changes: 3 additions & 2 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub fn run() {
manager::history::delete_history,
manager::history::open_in_folder,
manager::plan::get_all_plans,
manager::plan::get_plan,
manager::plan::add_plan,
manager::plan::add_plan_with_url,
manager::plan::delete_plan,
Expand All @@ -46,8 +47,8 @@ pub fn run() {
manager::ffmpeg_api::check_ffmpeg_version,
manager::ffmpeg_api::check_ffmpeg_availability,
manager::ffmpeg_api::download_ffmpeg,
manager::ffmpeg_api::request,
manager::ffmpeg_api::request_post,
manager::request_api::request,
manager::request_api::request_post,
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
Expand Down
23 changes: 16 additions & 7 deletions src-tauri/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::model::LiveInfo;
use crate::model::{PlatformKind, Stream};
use crate::{
ffmpeg, kv,
model::{RecordStatus, RecordingHistory, RecordingPlan},
utils,
model::{JsonMap, JsonValue, RecordStatus, RecordingHistory, RecordingPlan},
request, utils,
};
use chrono::Utc;
use dashmap::DashMap;
Expand Down Expand Up @@ -205,6 +205,16 @@ pub mod plan {
Ok(plans)
}

/// 获取所有录制计划
#[tauri::command]
pub async fn get_plan(url: String) -> Result<Option<RecordingPlan>, String> {
let plan = kv::plan::get(url).map_err(|e| {
eprintln!("Could not get recording plan: {}", e);
e.to_string()
})?;
Ok(plan)
}

/// 新增录制计划
#[tauri::command]
pub async fn add_plan(plan: RecordingPlan) -> Result<(), String> {
Expand Down Expand Up @@ -339,11 +349,6 @@ pub mod config {
}

pub mod ffmpeg_api {
use crate::{
model::{JsonMap, JsonValue},
request,
};

use super::*;

/// 检查 ffmpeg
Expand Down Expand Up @@ -373,6 +378,10 @@ pub mod ffmpeg_api {
kv::config::set(&config).map_err(|e| format!("Could not set config: {}", e))?;
Ok(path)
}
}

pub mod request_api {
use super::*;

/// 请求 url,得到 text
#[tauri::command]
Expand Down
66 changes: 55 additions & 11 deletions src/lib/components/record-list.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@
import { closeDialog, openDialog, formatFileSize, getPlatformIcon } from '$lib/utils';
import { invoke } from '@tauri-apps/api/core';
import { t } from '@/translations';
import RecordPlan from './record-plan.svelte';
dayjs.extend(relativeTime);
// icon fluent 28
let stopRecordDialogId = 'stopRecord';
let deleteHistoryDialogId = 'deleteHistory';
let list: RecordingHistory[] = $state([]);
let dialogUrl = $state('');
let intervalId: number | undefined = $state();
let deleteHistoryParams: { url: string; startTime: number; deleted: boolean } | undefined =
$state();
let stopRecordParams: { url: string; inPlan: boolean } | undefined = $state();
// 组件挂载时,获取录制历史,并每隔两秒获取一次
onMount(async () => {
Expand Down Expand Up @@ -71,13 +72,23 @@
});
}
async function stopRecord(url: string) {
async function stopRecord(url: string, disablePlan: boolean) {
stopRecordParams = undefined;
closeDialog(stopRecordDialogId);
dialogUrl = '';
if (url === '') {
toast.error($t('urlCannotBeEmpty'));
return;
}
if (disablePlan) {
// 禁用此计划
invoke('update_plan_status', { url, enabled: false })
.then(() => {})
.catch((e) => {
toast.error($t('disablePlanFailed'), {
description: e
});
});
}
invoke('stop_record', { url })
.then(() => {
toast.success($t('recordAlreadyStopped'));
Expand Down Expand Up @@ -110,21 +121,52 @@
}
return dayjs(startTime).to(dayjs(endTime), true);
}
// 获取对应 url 的计划信息
async function getPlan(url: string): Promise<RecordPlan | null> {
try {
let data = await invoke('get_plan', { url });
let plan = data as RecordPlan;
return plan;
} catch (e) {
return null;
}
}
// 判断是否在计划中,且计划为开启
async function isInPlan(url: string): Promise<boolean> {
const plan = await getPlan(url);
return plan !== null && plan.enabled;
}
</script>

<!-- 原生对话框 -->
<Dialog id={stopRecordDialogId} text={$t('confirmStopRecord')}>
<Dialog
id={stopRecordDialogId}
text={stopRecordParams?.inPlan ? $t('confirmStopRecordAndDisablePlan') : $t('confirmStopRecord')}
>
<button
class="btn w-24"
onclick={() => {
closeDialog(stopRecordDialogId);
dialogUrl = '';
stopRecordParams = undefined;
}}>{$t('cancel')}</button
>
<!-- svelte-ignore a11y_autofocus -->
<button autofocus class="btn btn-primary w-32" onclick={() => stopRecord(dialogUrl)}
>{$t('confirm')}</button
<button
autofocus
class="btn btn-primary w-32"
onclick={() => stopRecord(stopRecordParams?.url || '', false)}
>{stopRecordParams?.inPlan ? $t('onlyStopRecord') : $t('confirm')}</button
>
{#if stopRecordParams?.inPlan}
<!-- svelte-ignore a11y_autofocus -->
<button
class="btn btn-primary w-32"
onclick={() => stopRecord(stopRecordParams?.url || '', true)}
>{$t('stopRecordAndDisablePlan')}</button
>
{/if}
</Dialog>

<Dialog
Expand All @@ -140,7 +182,7 @@
>
<!-- svelte-ignore a11y_autofocus -->
<button
autofocus={!deleteHistoryParams?.deleted}
autofocus
class="btn btn-primary w-32"
onclick={() =>
deleteHistory(
Expand All @@ -153,7 +195,6 @@
{#if !deleteHistoryParams?.deleted}
<!-- svelte-ignore a11y_autofocus -->
<button
autofocus
class="btn btn-primary w-32"
onclick={() => deleteHistory(
deleteHistoryParams!.url,
Expand Down Expand Up @@ -263,9 +304,12 @@
<button
class="tooltip"
data-tip={$t('stopRecord')}
onclick={() => {
onclick={async () => {
stopRecordParams = {
url: row.url,
inPlan: await isInPlan(row.url)
};
openDialog(stopRecordDialogId);
dialogUrl = row.url;
}}
>
<svg
Expand Down
3 changes: 3 additions & 0 deletions src/lib/i18n/cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"downloadedFFmpeg": "已为您下载并设置好 ffmpeg 了",
"downloadFFmpegFailed": "下载 ffmpeg 失败",
"confirmStopRecord": "确定停止录制吗?",
"confirmStopRecordAndDisablePlan": "该录制在计划中,停止录制后,会按照计划重新开始录制。仅停止录制,还是同时禁用此计划?",
"onlyStopRecord": "仅停止录制",
"stopRecordAndDisablePlan": "同时禁用此计划",
"confirm": "确定",
"cancel": "取消",
"confirmInstallFFmpeg": "您可能没有安装 ffmpeg,是否为您自动安装?",
Expand Down
5 changes: 4 additions & 1 deletion src/lib/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,8 @@
"save": "Save",
"language": "Language",
"auto": "Auto",
"forHuyaError": "For Huya platform, this information may be incorrect. You can add it to the recording plan, and the program will automatically retry it"
"forHuyaError": "For Huya platform, this information may be incorrect. You can add it to the recording plan, and the program will automatically retry it",
"confirmStopRecordAndDisablePlan": "This recording is in the plan. After stopping the recording, it will be recorded again according to the plan. Do you want to stop the recording only or disable this plan at the same time?",
"onlyStopRecord": "Stop recording only",
"stopRecordAndDisablePlan": "Disable this plan at the same time"
}

0 comments on commit 637cc29

Please sign in to comment.