Skip to content

Commit ee341e0

Browse files
committed
Merge branch 'feat/torrent-to-magnet' into chore/all-my-stuffs
# Conflicts: # components.d.ts # package.json # pnpm-lock.yaml # src/tools/index.ts # vite.config.ts
2 parents dc273dd + e64cafd commit ee341e0

File tree

6 files changed

+224
-6
lines changed

6 files changed

+224
-6
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@
195195
"oui-data": "^1.0.10",
196196
"parse-duration": "^1.1.0",
197197
"path2d-polyfill": "^3.0.1",
198+
"parse-torrent": "^11.0.17",
198199
"pdf-signature-reader": "^1.4.2",
199200
"pdfjs-dist": "^4.0.379",
200201
"pinia": "^2.0.34",

pnpm-lock.yaml

Lines changed: 90 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tools/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ import { tool as potrace } from './potrace';
8686
import { tool as rmbNumbers } from './rmb-numbers';
8787
import { tool as sensitiveDataMasker } from './sensitive-data-masker';
8888
import { tool as textToUnicodeNames } from './text-to-unicode-names';
89+
import { tool as torrentToMagnet } from './torrent-to-magnet';
8990
import { tool as pdfSignatureChecker } from './pdf-signature-checker';
9091
import { tool as numeronymGenerator } from './numeronym-generator';
9192
import { tool as macAddressGenerator } from './mac-address-generator';
@@ -273,6 +274,7 @@ export const toolsByCategory: ToolCategory[] = [
273274
jsonToPhpArray,
274275
phpArrayToJson,
275276
maliciousLinksTester,
277+
torrentToMagnet,
276278
],
277279
},
278280
{

src/tools/torrent-to-magnet/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Bookmarks } from '@vicons/tabler';
2+
import { defineTool } from '../tool';
3+
4+
export const tool = defineTool({
5+
name: 'Torrent to Magnet',
6+
path: '/torrent-to-magnet',
7+
description: 'Convert a torrent file to a Magnet url',
8+
keywords: ['torrent', 'magnet', 'url', 'link'],
9+
component: () => import('./torrent-to-magnet.vue'),
10+
icon: Bookmarks,
11+
createdAt: new Date('2024-04-20'),
12+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare module 'parse-torrent'{
2+
export default function parseTorrent(content: string | ArrayBufferView): Promise<object>;
3+
export function toMagnetURI(parsedTorrent: object): string;
4+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<script setup lang="ts">
2+
import type { Ref } from 'vue';
3+
import parseTorrent, { toMagnetURI } from 'parse-torrent';
4+
import { withDefaultOnError } from '@/utils/defaults';
5+
import { useValidation } from '@/composable/validation';
6+
7+
const inputType = ref<'file' | 'content'>('file');
8+
const torrentContent = ref('');
9+
const fileInput = ref() as Ref<File | null>;
10+
const torrentInfosRaw = computedAsync(async () => {
11+
const file = fileInput.value;
12+
const content = torrentContent.value;
13+
try {
14+
if (inputType.value === 'file' && file) {
15+
return await parseTorrent(new Uint8Array(await file.arrayBuffer()));
16+
}
17+
else {
18+
return await parseTorrent(content);
19+
}
20+
}
21+
catch (e: any) {
22+
return {
23+
error: e.toString(),
24+
};
25+
}
26+
});
27+
const torrentInfos = computed(() => withDefaultOnError(() => {
28+
return Object.entries(torrentInfosRaw.value).map(([k, v]) => {
29+
return {
30+
label: k?.toString() || '',
31+
value: JSON.stringify(v),
32+
};
33+
});
34+
}, []));
35+
const magnetURI = computed(() => withDefaultOnError(() => toMagnetURI(torrentInfosRaw.value), []));
36+
37+
async function onUpload(file: File) {
38+
if (file) {
39+
fileInput.value = file;
40+
}
41+
}
42+
43+
watch(torrentContent, (_, newValue) => {
44+
if (newValue !== '') {
45+
fileInput.value = null;
46+
}
47+
});
48+
49+
const { attrs: validationAttrs } = useValidation({
50+
source: torrentInfos,
51+
rules: [{ message: 'Invalid torrent content', validator: torrent => torrent?.length > 0 }],
52+
});
53+
</script>
54+
55+
<template>
56+
<div>
57+
<n-radio-group v-model:value="inputType" name="radiogroup" mb-2 flex justify-center>
58+
<n-space>
59+
<n-radio
60+
value="file"
61+
label="File"
62+
/>
63+
<n-radio
64+
value="content"
65+
label="Content"
66+
/>
67+
</n-space>
68+
</n-radio-group>
69+
70+
<c-file-upload
71+
v-if="inputType === 'file'"
72+
title="Drag and drop torrent file here, or click to select a file"
73+
@file-upload="onUpload"
74+
/>
75+
76+
<c-input-text
77+
v-if="inputType === 'content'"
78+
v-model:value="torrentContent"
79+
label="Torrent/Magnet Content"
80+
placeholder="Paste your Torrent/Magnet content here"
81+
multiline
82+
mb-2
83+
/>
84+
85+
<n-divider />
86+
87+
<input-copyable
88+
label="Magnet URI"
89+
label-position="left"
90+
label-width="100px"
91+
label-align="right"
92+
mb-2
93+
:value="validationAttrs.validationStatus === 'error' ? '' : magnetURI"
94+
placeholder="Please use a correct torrent"
95+
/>
96+
97+
<input-copyable
98+
v-for="{ label, value } of torrentInfos"
99+
:key="label"
100+
:label="label"
101+
label-position="left"
102+
label-width="100px"
103+
label-align="right"
104+
mb-2
105+
:value="validationAttrs.validationStatus === 'error' ? '' : value"
106+
placeholder="Please use a correct torrent"
107+
/>
108+
</div>
109+
</template>
110+
111+
<style lang="less" scoped>
112+
::v-deep(.n-upload-trigger) {
113+
width: 100%;
114+
}
115+
</style>

0 commit comments

Comments
 (0)