主进程代码优化

for/master 1.1.2
caorushizi 3 years ago
parent 96dbb1ecef
commit 816a6c3047

@ -32,7 +32,7 @@ class NM3u8dlCliDownloader extends Downloader {
.reduce((prev: string[], [key, value]) => {
if (key === "url") return prev;
if (value && typeof value === "boolean") prev.push(`--${key}`);
if (value && typeof value === "string")
if (value && (typeof value === "string" || typeof value === "number"))
prev.push(`--${key} "${value}"`);
return prev;
}, [])

@ -58,8 +58,7 @@ interface FormItem {
const renderItem = (
item: FormItem,
onChange: (item: FormItem) => void,
onDelete: (item: FormItem) => void,
onInputBlur: () => void
onDelete: (item: FormItem) => void
) => {
return (
<Row className={"header-item-container"} key={item.id}>
@ -79,7 +78,6 @@ const renderItem = (
onChange={(value) => {
onChange({ ...item, key: value });
}}
onBlur={onInputBlur}
/>
</Col>
<Col span={14}>
@ -88,7 +86,6 @@ const renderItem = (
onChange={(e) => {
onChange({ ...item, value: e.target.value });
}}
onBlur={onInputBlur}
/>
</Col>
<Col span={1} className={"form-item-action"}>
@ -138,16 +135,17 @@ const HeaderFieldInput: FC<HeaderFieldInputProps> = ({ value, onChange }) => {
changeItem.value = item.value;
setFormValues(copiedFormValues);
onChange?.(postHeader(copiedFormValues));
};
const onInputDelete = (item: FormItem) => {
const changeItemIndex = formValues.findIndex((i) => i.id === item.id);
if (changeItemIndex < 0) return;
const copiedFormItem = formValues.slice();
copiedFormItem.splice(changeItemIndex, 1);
setFormValues(copiedFormItem);
onChange?.(postHeader(copiedFormItem));
const copiedFormValues = formValues.slice();
copiedFormValues.splice(changeItemIndex, 1);
setFormValues(copiedFormValues);
onChange?.(postHeader(copiedFormValues));
};
const onInputAdd = () => {
@ -155,26 +153,23 @@ const HeaderFieldInput: FC<HeaderFieldInputProps> = ({ value, onChange }) => {
setFormValues(changedValue);
};
const onInputBlur = () => {
onChange?.(postHeader(formValues));
};
return (
<Box>
{formValues.length > 0 && (
<Box className={"header-field-container"}>
<Box className={"header-field-container"} mb={6}>
{formValues.map((formItem) => {
return renderItem(
formItem,
onInputChange,
onInputDelete,
onInputBlur
);
return renderItem(formItem, onInputChange, onInputDelete);
})}
</Box>
)}
<Box mt={6} d={"flex"} justifyContent={"flex-end"}>
<Button type={"link"} icon={<PlusOutlined />} onClick={onInputAdd}>
<Box d={"flex"} justifyContent={"space-between"} alignItems={"center"}>
<Box>{formValues.length <= 0 && "点击添加 header"}</Box>
<Button
size={"small"}
type={"link"}
icon={<PlusOutlined />}
onClick={onInputAdd}
>
</Button>
</Box>

@ -82,10 +82,6 @@ interface Props {
updateTableData: () => Promise<void>;
}
const headersPlaceholder = `[可空] 请输入一行一个Header例如
Origin: https://www.sample.com
Referer: https://www.sample.com`;
const colorMap = {
ready: "#108ee9",
downloading: "#2db7f5",
@ -224,11 +220,12 @@ const DownloadList: React.FC<Props> = ({
const insertUpdateTableData = async (
item: SourceItemForm
): Promise<SourceItem> => {
const { workspace } = settings;
const { workspace, exeFile } = settings;
const sourceItem: SourceItem = {
id: nanoid(),
status: SourceStatus.Ready,
type: SourceType.M3u8,
exeFile,
directory: workspace,
title: item.title,
duration: 0,
@ -300,9 +297,10 @@ const DownloadList: React.FC<Props> = ({
const downloadFile = async (item: SourceItem): Promise<void> => {
await changeSourceStatus(item, SourceStatus.Downloading);
onEvent.tableStartDownload();
const exeFile = await window.electron.store.get("exeFile");
const { title, headers, url, exeFile: formExeFile } = item;
const exeFile = formExeFile || (await window.electron.store.get("exeFile"));
const workspace = await window.electron.store.get("workspace");
const { title, headers, url } = item;
let args: MediaGoArgs | M3u8DLArgs;
if (exeFile === "mediago") {
@ -316,6 +314,22 @@ const DownloadList: React.FC<Props> = ({
headers: headersString,
};
} else {
const {
checkbox,
maxThreads,
minThreads,
retryCount,
timeOut,
stopSpeed,
maxSpeed,
} = item;
const checkboxObj = Object.values(checkbox! || []).reduce(
(prev: Record<string, boolean>, cur) => {
prev[cur] = true;
return prev;
},
{}
);
const headersString = Object.entries(headers || {})
.map(([key, value]) => `${key}:${value}`)
.join("|");
@ -325,8 +339,16 @@ const DownloadList: React.FC<Props> = ({
saveName: title, // 设定存储文件名(不包括后缀)
headers: headersString,
enableDelAfterDone: item.deleteSegments,
...checkboxObj,
maxThreads,
minThreads,
retryCount,
timeOut,
stopSpeed,
maxSpeed,
};
}
console.log("args: ", args);
const { code, msg } = await ipcExec(exeFile, args);
if (code === 0) {
@ -625,9 +647,11 @@ const DownloadList: React.FC<Props> = ({
flex={1}
className={"list-item-inner"}
onClick={() => {
const { exeFile } = settings;
setCurrentSourceItem(item);
detailForm.setFieldsValue(item);
detailForm.setFieldsValue({ ...item, exeFile });
calcMaxWidth();
setMoreOptions(exeFile !== "mediago");
dispatch(updateNotifyCount(0));
}}
>
@ -737,35 +761,56 @@ const DownloadList: React.FC<Props> = ({
</Divider>
{!expanded && (
<>
<Form.Item>
<Form.Item
name={"checkbox"}
initialValue={["enableDelAfterDone"]}
>
<Checkbox.Group style={{ width: "100%" }}>
<Row>
<Col span={8} style={{ marginBottom: "8px" }}>
<Checkbox value="A"></Checkbox>
<Checkbox value="enableDelAfterDone">
</Checkbox>
</Col>
<Col span={8} style={{ marginBottom: "8px" }}>
<Checkbox value="B"></Checkbox>
<Checkbox value="disableDateInfo">
</Checkbox>
</Col>
<Col span={8} style={{ marginBottom: "8px" }}>
<Checkbox value="C">使</Checkbox>
<Checkbox value="noProxy">
使
</Checkbox>
</Col>
<Col span={8} style={{ marginBottom: "8px" }}>
<Checkbox value="D">m3u8</Checkbox>
<Checkbox value="enableParseOnly">
m3u8
</Checkbox>
</Col>
<Col span={8} style={{ marginBottom: "8px" }}>
<Checkbox value="E">MP4</Checkbox>
<Checkbox value="enableMuxFastStart">
MP4
</Checkbox>
</Col>
<Col span={8} style={{ marginBottom: "8px" }}>
<Checkbox value="F"></Checkbox>
<Checkbox value="noMerge">
</Checkbox>
</Col>
<Col span={8}>
<Checkbox value="G">使</Checkbox>
<Checkbox value="enableBinaryMerge">
使
</Checkbox>
</Col>
<Col span={8}>
<Checkbox value="H"></Checkbox>
<Checkbox value="enableAudioOnly">
</Checkbox>
</Col>
<Col span={8}>
<Checkbox value="I"></Checkbox>
<Checkbox value="disableIntegrityCheck">
</Checkbox>
</Col>
</Row>
</Checkbox.Group>
@ -773,80 +818,68 @@ const DownloadList: React.FC<Props> = ({
<Row>
<Col span={8}>
<Form.Item
name={""}
name={"maxThreads"}
label={"最大线程"}
labelCol={{ style: { width: "86px" } }}
labelAlign={"left"}
initialValue={32}
>
<InputNumber
placeholder="placeholder"
defaultValue={32}
/>
<InputNumber placeholder="placeholder" />
</Form.Item>
</Col>
<Col span={8}>
<Form.Item
name={""}
name={"minThreads"}
label={"最小线程"}
labelCol={{ style: { width: "86px" } }}
labelAlign={"left"}
initialValue={16}
>
<InputNumber
placeholder="placeholder"
defaultValue={16}
/>
<InputNumber placeholder="placeholder" />
</Form.Item>
</Col>
<Col span={8}>
<Form.Item
name={""}
name={"retryCount"}
label={"重试次数"}
labelCol={{ style: { width: "86px" } }}
labelAlign={"left"}
initialValue={15}
>
<InputNumber
placeholder="placeholder"
defaultValue={15}
/>
<InputNumber placeholder="placeholder" />
</Form.Item>
</Col>
<Col span={8}>
<Form.Item
name={""}
name={"timeOut"}
label={"超时时长(s)"}
labelCol={{ style: { width: "86px" } }}
labelAlign={"left"}
initialValue={10}
>
<InputNumber
placeholder="placeholder"
defaultValue={10}
/>
<InputNumber placeholder="placeholder" />
</Form.Item>
</Col>
<Col span={8}>
<Form.Item
name={""}
name={"stopSpeed"}
label={"停速(KB/s)"}
labelCol={{ style: { width: "86px" } }}
labelAlign={"left"}
initialValue={0}
>
<InputNumber
placeholder="placeholder"
defaultValue={0}
/>
<InputNumber placeholder="placeholder" />
</Form.Item>
</Col>
<Col span={8}>
<Form.Item
name={""}
name={"maxSpeed"}
label={"限速(KB/s)"}
labelCol={{ style: { width: "86px" } }}
labelAlign={"left"}
initialValue={0}
>
<InputNumber
placeholder="placeholder"
defaultValue={0}
/>
<InputNumber placeholder="placeholder" />
</Form.Item>
</Col>
</Row>
@ -925,13 +958,7 @@ const DownloadList: React.FC<Props> = ({
>
<Input placeholder="[可空] 默认当前时间戳" allowClear />
</Form.Item>
<Form.Item label="请求标头" name="headers">
<Input.TextArea
rows={3}
placeholder={headersPlaceholder}
allowClear
/>
</Form.Item>
<HeaderEdit label={"请求标头"} name={"headers"} />
<Form.Item
label="下载完成是否删除"
name="delete"

@ -15,6 +15,15 @@ declare type SourceItem = SourceUrl & {
deleteSegments: boolean;
directory: string;
createdAt: number;
exeFile: string;
// 额外字段
checkbox?: string[];
maxThreads?: number;
minThreads?: number;
retryCount?: number;
timeOut?: number;
stopSpeed?: number;
maxSpeed?: number;
};
declare interface Fav {

Loading…
Cancel
Save