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'
8 db.init(true, function () {
9 hasFriends(function (err, itHasFriends) {
12 if (itHasFriends === true) {
13 console.log('Cannot update host because you have friends!')
17 console.log('Updating torrent files.')
18 db.Video.list(function (err, videos) {
21 videos.forEach(function (video) {
22 const torrentName = video.id + '.torrent'
23 const torrentPath = CONFIG.STORAGE.TORRENTS_DIR + torrentName
24 const filename = video.id + video.extname
26 const parsed = parseTorrent(readFileSync(torrentPath))
27 parsed.announce = [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOST + '/tracker/socket' ]
28 parsed.urlList = [ CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + filename ]
30 const buf = parseTorrent.toTorrentFile(parsed)
31 writeFileSync(torrentPath, buf)