1 import { readFileSync, writeFileSync } from 'fs'
2 import * as parseTorrent from 'parse-torrent'
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'
12 .then(itHasFriends => {
13 if (itHasFriends === true) {
14 console.log('Cannot update host because you have friends!')
18 console.log('Updating torrent files.')
19 return db.Video.list()
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
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 ]
31 const buf = parseTorrent.toTorrentFile(parsed)
32 writeFileSync(torrentPath, buf)