Type webtorrent
[oweals/peertube.git] / scripts / update-host.ts
1 import { readFileSync, writeFileSync } from 'fs'
2 import * as parseTorrent from 'parse-torrent'
3
4 import { CONFIG, STATIC_PATHS } from '../server/initializers/constants'
5 import { database as db } from '../server/initializers/database'
6 import { hasFriends } from '../server/lib/friends'
7
8 db.init(true)
9   .then(() => {
10     return hasFriends()
11   })
12   .then(itHasFriends => {
13     if (itHasFriends === true) {
14       console.log('Cannot update host because you have friends!')
15       process.exit(-1)
16     }
17
18     console.log('Updating torrent files.')
19     return db.Video.list()
20   })
21   .then(videos => {
22     videos.forEach(function (video) {
23       const torrentName = video.id + '.torrent'
24       const torrentPath = CONFIG.STORAGE.TORRENTS_DIR + torrentName
25       const filename = video.id + video.extname
26
27       const parsed = parseTorrent(readFileSync(torrentPath))
28       parsed.announce = [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOST + '/tracker/socket' ]
29       parsed.urlList = [ CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + filename ]
30
31       const buf = parseTorrent.toTorrentFile(parsed)
32       writeFileSync(torrentPath, buf)
33     })
34
35     process.exit(0)
36   })