5 // TODO: document this script
7 const fs = require('fs')
8 const parseTorrent = require('parse-torrent')
10 const constants = require('../server/initializers/constants')
11 const db = require('../server/initializers/database')
13 const friends = require('../server/lib/friends')
15 db.init(true, function () {
16 friends.hasFriends(function (err, hasFriends) {
19 if (hasFriends === true) {
20 console.log('Cannot update host because you have friends!')
24 console.log('Updating torrent files.')
25 db.Video.list(function (err, videos) {
28 videos.forEach(function (video) {
29 const torrentName = video._id + '.torrent'
30 const torrentPath = constants.CONFIG.STORAGE.TORRENTS_DIR + torrentName
31 const filename = video._id + video.extname
33 const parsed = parseTorrent(fs.readFileSync(torrentPath))
34 parsed.announce = [ constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOST + '/tracker/socket' ]
35 parsed.urlList = [ constants.CONFIG.WEBSERVER.URL + constants.STATIC_PATHS.WEBSEED + filename ]
37 const buf = parseTorrent.toTorrentFile(parsed)
38 fs.writeFileSync(torrentPath, buf)