From 7520c3feaa52d7e80267b4f3a5b8686b1ca79ff3 Mon Sep 17 00:00:00 2001 From: ThaUnknown <6506529+ThaUnknown@users.noreply.github.com> Date: Thu, 14 Sep 2023 01:22:11 +0200 Subject: [PATCH] feat: #314 --- src/background/background.js | 22 ++++++++++++++++++++-- src/renderer/modules/torrent.js | 5 +++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/background/background.js b/src/background/background.js index 774a840..1c419c0 100644 --- a/src/background/background.js +++ b/src/background/background.js @@ -4,6 +4,9 @@ import HTTPTracker from 'bittorrent-tracker/lib/client/http-tracker.js' import { hex2bin, arr2hex, text2arr } from 'uint8-util' import Parser from './parser.js' import { defaults, fontRx, subRx, videoRx } from '../common/util.js' +import { statfs } from 'fs/promises' + +const LARGE_FILESIZE = 32_000_000_000 class TorrentClient extends WebTorrent { static excludedErrorMessages = ['WebSocket', 'User-Initiated Abort, reason=', 'Connection failed.'] @@ -65,7 +68,7 @@ class TorrentClient extends WebTorrent { if (torrent) this.addTorrent(new Uint8Array(JSON.parse(torrent)), JSON.parse(localStorage.getItem('lastFinished'))) } - handleTorrent (torrent) { + async handleTorrent (torrent) { const files = torrent.files.map(file => { return { infoHash: torrent.infoHash, @@ -76,9 +79,21 @@ class TorrentClient extends WebTorrent { url: 'http://localhost:' + this.server.address().port + file.streamURL } }) + if (torrent.length > LARGE_FILESIZE) { + for (const file of torrent.files) { + file.deselect() + } + this.dispatch('warn', 'Detected Large Torrent! To Conserve Drive Space Files Will Be Downloaded Selectively Instead Of Downloading The Entire Torrent.') + } this.dispatch('files', files) this.dispatch('magnet', { magnet: torrent.magnetURI, hash: torrent.infoHash }) localStorage.setItem('torrent', JSON.stringify([...torrent.torrentFile])) + + const { bsize, bavail } = await statfs(torrent.path) + + if (torrent.length > bsize * bavail) { + this.dispatch('error', 'Torrent Too Big! This Torrent Exceeds The Selected Drive\'s Available Space. Change Download Location In Torrent Settings To A Drive With More Space And Restart The App!') + } } async findFontFiles (targetFile) { @@ -180,11 +195,14 @@ class TorrentClient extends WebTorrent { switch (data.type) { case 'current': { if (data.data) { - const found = (await this.get(data.data.infoHash))?.files.find(file => file.path === data.data.path) + const torrent = await this.get(data.data.infoHash) + const found = torrent?.files.find(file => file.path === data.data.path) + if (!found) return if (this.current) { this.current.removeAllListeners('stream') } this.parser?.destroy() + found.select() this.current = found this.parser = new Parser(this, found) this.findSubtitleFiles(found) diff --git a/src/renderer/modules/torrent.js b/src/renderer/modules/torrent.js index 9143980..bcca394 100644 --- a/src/renderer/modules/torrent.js +++ b/src/renderer/modules/torrent.js @@ -59,6 +59,11 @@ client.on('error', ({ detail }) => { toast.error('Torrent Error', { description: detail.message || detail }) }) +client.on('warn', ({ detail }) => { + console.error(detail) + toast.warning('Torrent Warning', { description: detail.message || detail }) +}) + export async function add (torrentID, hide) { if (torrentID) { console.info('Torrent: adding torrent', { torrentID })