Skip to content

Commit

Permalink
feat: streamed download
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaUnknown committed Jul 3, 2024
1 parent d44ae0d commit 5f67d3c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 46 deletions.
1 change: 1 addition & 0 deletions common/modules/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export const defaults = {
torrentDHT: false,
torrentPeX: false,
torrentPort: 0,
torrentStreamedDownload: true,
dhtPort: 0,
missingFont: true,
maxConns: 50,
Expand Down
22 changes: 8 additions & 14 deletions common/modules/webtorrent.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import { SUPPORTS } from './support.js'
// HACK: this is https only, but electron doesnt run in https, weirdge
if (!globalThis.FileSystemFileHandle) globalThis.FileSystemFileHandle = false

const LARGE_FILESIZE = 32_000_000_000

const announce = [
atob('d3NzOi8vdHJhY2tlci5vcGVud2VidG9ycmVudC5jb20='),
atob('d3NzOi8vdHJhY2tlci53ZWJ0b3JyZW50LmRldg=='),
Expand Down Expand Up @@ -142,19 +140,9 @@ export default class TorrentClient extends WebTorrent {
url: this.serverMode === 'node' ? 'http://localhost:' + this.server.address().port + file.streamURL : 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])) // this won't work on mobile, but really it only speeds stuff up by ~1-2 seconds since magnet data doesn't need to be resolved

if (torrent.length > await this.storageQuota(torrent.path)) {
this.dispatchError('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) {
Expand Down Expand Up @@ -243,7 +231,8 @@ export default class TorrentClient extends WebTorrent {
path: this.torrentPath || undefined,
destroyStoreOnDestroy: !this.settings.torrentPersist,
skipVerify,
announce
announce,
deselect: this.settings.torrentStreamedDownload
})

torrent.once('done', () => {
Expand All @@ -256,17 +245,22 @@ export default class TorrentClient extends WebTorrent {
case 'current': {
if (data.data) {
const torrent = await this.get(data.data.current.infoHash)
const found = torrent?.files.find(file => file.path === data.data.current.path)
if (!torrent) return
const found = torrent.files.find(file => file.path === data.data.current.path)
if (!found) return
if (this.playerProcess) {
this.playerProcess.kill()
this.playerProcess = null
}
if (this.current) {
this.current.removeAllListeners('stream')
this.current.deselect()
}
this.parser?.destroy()
found.select()
if (found.length > await this.storageQuota(torrent.path)) {
this.dispatchError('File Too Big! This File Exceeds The Selected Drive\'s Available Space. Change Download Location In Torrent Settings To A Drive With More Space And Restart The App!')
}
this.current = found
if (data.data.external && this.player) {
this.playerProcess = spawn(this.player, [encodeURI('http://localhost:' + this.server.address().port + found.streamURL)])
Expand Down
6 changes: 6 additions & 0 deletions common/views/Settings/TorrentSettings.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@
</div>
</SettingCard>
{/if}
<SettingCard title='Streamed Download' description="Only downloads the single file that's currently being watched, instead of downloading an entire batch of episodes. Saves bandwidth and reduces strain on the peer swarm.">
<div class='custom-switch'>
<input type='checkbox' id='torrent-streamed-download' bind:checked={settings.torrentStreamedDownload} />
<label for='torrent-streamed-download'>{settings.torrentStreamedDownload ? 'On' : 'Off'}</label>
</div>
</SettingCard>
<SettingCard title='Transfer Speed Limit' description='Download/Upload speed limit for torrents, higher values increase CPU usage, and values higher than your storage write speeds will quickly fill up RAM.'>
<div class='input-group w-100 mw-full'>
<input type='number' inputmode='numeric' pattern={'[0-9]*.?[0-9]*'} bind:value={settings.torrentSpeed} min='0' max='50' step='0.1' class='form-control text-right bg-dark' />
Expand Down
4 changes: 2 additions & 2 deletions electron/src/main/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Menu, app } from 'electron'
import { app } from 'electron'
import App from './app.js'

// Keep a global reference of the window object, if you don't, the window will
Expand All @@ -9,7 +9,7 @@ function createWindow () {
main = new App()
}

Menu.setApplicationMenu(null) // performance
// Menu.setApplicationMenu(null) // performance, but no keyboard shortcuts, sucks
app.on('ready', createWindow)

app.on('activate', () => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"webpack": "^5.91.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^5.0.2",
"webtorrent": "^2.4.1"
"webtorrent": "^2.4.11"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20240222.0",
Expand Down
58 changes: 29 additions & 29 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5f67d3c

Please sign in to comment.