Skip to content

Commit

Permalink
新增 aria2c input格式
Browse files Browse the repository at this point in the history
  • Loading branch information
Rovniced committed Aug 14, 2024
1 parent 39933d2 commit dffc4b0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
29 changes: 24 additions & 5 deletions src/pages/Parsed.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {useRoute, useRouter} from 'vue-router';
import {useWorker} from '@/hooks/useWorker';
import {renderSize} from '@/utils/render-size';
import {sendToRPC} from '@/utils/send-to-rpc';
import { package2IDMLinks, packageDownloadLinks } from '@/utils/package-download-links';
import { package2Aria2Input, package2IDMLinks, packageDownloadLinks } from '@/utils/package-download-links';
import {getFileList} from '@/services/parse';
import {dealFileList} from '@/utils/deal-file-list';
import type {ParsedFile, ShareInfo, TreeFileInfo, WorkerRequestBody, WorkerResponse} from '@/types';
Expand Down Expand Up @@ -47,7 +47,7 @@ const userRef = storeToRefs(userStore);
const rpcRef = userRef.rpc;
const exportFormat = userRef.exportFormat;
const downloadType = ref<{
code: 'web' | 'jsonrpc' | 'idm' |''
code: 'web' | 'jsonrpc' | 'idm' | 'aria2input' |''
}>({
code: ''
});
Expand Down Expand Up @@ -185,6 +185,10 @@ const stop = () => {
const onWorkerMessage = async (m: WorkerResponse) => {
if (m.type === 'done') {
stop();
if (results.length === 0) {
message.warn('全部任务失败,请重新获取!');
return;
}
message.success('全部任务已完成!');
if (downloadType.value.code === 'web') {
await packageDownloadLinks(results, userRef.exportFormat.value);
Expand All @@ -194,6 +198,10 @@ const onWorkerMessage = async (m: WorkerResponse) => {
await package2IDMLinks(results);
stop();
return;
} else if (downloadType.value.code === 'aria2input') {
await package2Aria2Input(results);
stop();
return;
}
stop();
Expand All @@ -219,8 +227,7 @@ const onWorkerMessage = async (m: WorkerResponse) => {
// Web
results.push({
filename: m!!.body!!.filename,
link: m!!.body!!.dlink,
ua: m!!.body!!.ua
link: m!!.body!!.dlink
});
}
if (m.type === 'error') {
Expand Down Expand Up @@ -307,7 +314,10 @@ worker.setCallback(onWorkerMessage);
<div class="field col">
<label for="type">下载方式</label>
<Dropdown v-model="downloadType" :disabled="blocked"
:options="[{name: 'Web', code: 'web'}, {name: 'JSON RPC', code: 'jsonrpc'},{name: 'IDM 下载', code: 'idm'}]"
:options="[{name: 'Web', code: 'web'},
{name: 'Aria2 JSON RPC (推荐)', code: 'jsonrpc'},
{name: 'IDM 下载', code: 'idm'},
{name: 'Aria2 Input', code: 'aria2input'}]"
optionLabel="name" placeholder="选择下载方式"/>
</div>
</div>
Expand Down Expand Up @@ -375,6 +385,15 @@ worker.setCallback(onWorkerMessage);
此功能会自动帮你设置UA,无需在IDM设置中更改。<br>
</p>
</template>
<template v-if="downloadType.code === 'aria2input'">
<Divider align="left" type="solid">
<b>Aria2 Input方式</b>
</Divider>
<Divider></Divider>
<p><strong>非专业人士请使用Aria2 JSON RPC模式</strong><br>
使用 aria2 命令行输入文件开始下载. (aria2c -i task.txt)<br>
</p>
</template>
</Fieldset>
<Fieldset legend="准备下载">
<ProgressBar :mode="starting && progress === 0 ? 'indeterminate' : 'determinate'"
Expand Down
1 change: 0 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export type TreeFileInfo = {
export type ParsedFile = {
filename: string;
link: string;
ua: string;
};

export type ParsedFileResult = {
Expand Down
28 changes: 27 additions & 1 deletion src/utils/package-download-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ export async function packageDownloadLinks(results: ParsedFile[], format: string
export async function package2IDMLinks(results: ParsedFile[]) {
const zip = new JSZip();
const message = useMessage();
const systemConfig = useSystemConfigStore();
const UA = systemConfig.parse_ua
zip.file('说明.txt', getREADME());
// https://github.com/MotooriKashin/ef2 第三方的ef2工具支持指定文件名,官方的不支持,但是不会影响读取。
const content = results.map(v => {
return `<\r\n${v.link}\r\nUser-Agent: ${v.ua}\r\nfilename: ${v.filename}\r\n>`;
return `<\r\n${v.link}\r\nUser-Agent: ${UA}\r\nfilename: ${v.filename}\r\n>`;
}).join('\r\n');
zip.file('任务.ef2', content + '\r\n');
try {
Expand All @@ -77,3 +79,27 @@ export async function package2IDMLinks(results: ParsedFile[]) {
}
return false;
}

/**
* Aria2 Input格式打包下载
* @param results
*/
export async function package2Aria2Input(results: ParsedFile[]) {
const message = useMessage();
const README = getREADME().split('\n').map(line => '# ' + line).join('\n');
const systemConfig = useSystemConfigStore();
const UA = systemConfig.parse_ua
const content = README + '\r\n' + results.map(v => {
return `\r\n${v.link}\r\n user-agent=${UA}\r\n out=${v.filename}\r\n`;
}).join('\r\n');
try {
const blob = new Blob([content], { type: 'text/plain;charset=utf-8' });
FileSaver.saveAs(blob, 'task.txt');
message.success('生成成功!正在下载……');
return true;
} catch (e) {
console.error(e);
message.error('生成失败,请重新尝试或使用 JSON RPC 下载。');
}
return false;
}

0 comments on commit dffc4b0

Please sign in to comment.