]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - scripts/update-host.js
Add script when the host/port of a pod change
[github/Chocobozzz/PeerTube.git] / scripts / update-host.js
1 #!/usr/bin/env node
2
3 'use strict'
4
5 // TODO: document this script
6
7 const fs = require('fs')
8 const mongoose = require('mongoose')
9 const parseTorrent = require('parse-torrent')
10
11 const constants = require('../server/initializers/constants')
12 const database = require('../server/initializers/database')
13
14 database.connect()
15
16 const friends = require('../server/lib/friends')
17 const Video = mongoose.model('Video')
18
19 friends.hasFriends(function (err, hasFriends) {
20 if (err) throw err
21
22 if (hasFriends === true) {
23 console.log('Cannot update host because you have friends!')
24 process.exit(-1)
25 }
26
27 console.log('Updating videos host in database.')
28 Video.update({ }, { podHost: constants.CONFIG.WEBSERVER.HOST }, { multi: true }, function (err) {
29 if (err) throw err
30
31 console.log('Updating torrent files.')
32 Video.find().lean().exec(function (err, videos) {
33 if (err) throw err
34
35 videos.forEach(function (video) {
36 const torrentName = video._id + '.torrent'
37 const torrentPath = constants.CONFIG.STORAGE.TORRENTS_DIR + torrentName
38 const filename = video._id + video.extname
39
40 const parsed = parseTorrent(fs.readFileSync(torrentPath))
41 parsed.announce = [ constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOST + '/tracker/socket' ]
42 parsed.urlList = [ constants.CONFIG.WEBSERVER.URL + constants.STATIC_PATHS.WEBSEED + filename ]
43
44 const buf = parseTorrent.toTorrentFile(parsed)
45 fs.writeFileSync(torrentPath, buf)
46 })
47
48 process.exit(0)
49 })
50 })
51 })