5 const fs = require('fs')
6 const parseTorrent = require('parse-torrent')
8 const constants = require('../server/initializers/constants')
9 const db = require('../server/initializers/database')
11 const friends = require('../server/lib/friends')
13 db.init(true, function () {
14 friends.hasFriends(function (err, hasFriends) {
17 if (hasFriends === true) {
18 console.log('Cannot update host because you have friends!')
22 console.log('Updating torrent files.')
23 db.Video.list(function (err, videos) {
26 videos.forEach(function (video) {
27 const torrentName = video.id + '.torrent'
28 const torrentPath = constants.CONFIG.STORAGE.TORRENTS_DIR + torrentName
29 const filename = video.id + video.extname
31 const parsed = parseTorrent(fs.readFileSync(torrentPath))
32 parsed.announce = [ constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOST + '/tracker/socket' ]
33 parsed.urlList = [ constants.CONFIG.WEBSERVER.URL + constants.STATIC_PATHS.WEBSEED + filename ]
35 const buf = parseTorrent.toTorrentFile(parsed)
36 fs.writeFileSync(torrentPath, buf)