-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
107 lines (90 loc) · 2.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// @ts-check
const Express = require('express')
const sendSeekable = require('send-seekable')
const WebTorrent = require('webtorrent')
const MemoryChunkStore = require('memory-chunk-store')
// Utilities
/**
* @param {Express.Response} res
*/
function doesNotExist(res) {
res.statusCode = 404
res.write('Torrent does not exist in client.')
res.end()
}
/**
* @param {WebTorrent.Torrent} torrent
*/
function destroyTorrent(torrent) {
torrent.destroy({ destroyStore: true })
console.log(`Torrent ${torrent.name} (${torrent.infoHash}) destroyed.`)
}
const app = Express()
app.set('view engine', 'ejs')
app.use(sendSeekable)
const client = new WebTorrent()
client.on('error', console.error)
app.get('/', (req, res) => res.render('index', {title: "Index", torrents: client.torrents}))
process.on('exit', () => client.destroy())
app.get('/add', (req, res) => {
if (!(req.query.location)) {
res.statusCode = 400
res.write('No "location" query parameter.')
return
}
const location = /** @type string */ (req.query.location)
client.add(
location,
{
// Most server solutions have limited storage space, which can cause crashes,
// so we'll use an in-memory solution instead.
store: MemoryChunkStore,
},
(torrent) => {
// Destroy torrents automatically on finishing of download after 6 hours
torrent.on("done", () => setTimeout(() => destroyTorrent(torrent), 1000 * 60 * 60 * 6))
res.redirect(`/preview/${torrent.infoHash}`)
res.end()
}
)
})
app.get('/preview/:infoHash', (req, res) => {
const { infoHash } = req.params
const torrent = client.get(infoHash)
if (torrent) {
res.render('preview', {title: `Preview "${torrent.name}"`, torrent})
res.end()
} else {
doesNotExist(res)
}
})
app.post('/destroy/:infoHash', (req, res) => {
const { infoHash } = req.params
const torrent = client.get(infoHash)
if (torrent) {
destroyTorrent(torrent)
res.redirect('/')
} else {
doesNotExist(res)
}
})
app.get('/download/:infoHash/:filename', (req, res) => {
const { infoHash, filename } = req.params
const torrent = client.get(infoHash)
if (torrent) {
const files = torrent.files.filter(f => f.name == filename)
if (files.length) {
const file = files[0]
const stream = file.createReadStream()
res.sendSeekable(stream, {length: file.length})
} else {
res.statusCode = 404
res.write('File does not exist in torrent.')
return
}
} else {
doesNotExist(res)
return
}
})
app.listen(4000, () => console.log('webtorrent-server listening @ http://localhost:4000/'))