Server: refractoring upload/update video test utils
[oweals/peertube.git] / scripts / update-host.js
1 #!/usr/bin/env node
2
3 'use strict'
4
5 const fs = require('fs')
6 const parseTorrent = require('parse-torrent')
7
8 const constants = require('../server/initializers/constants')
9 const db = require('../server/initializers/database')
10
11 const friends = require('../server/lib/friends')
12
13 db.init(true, function () {
14   friends.hasFriends(function (err, hasFriends) {
15     if (err) throw err
16
17     if (hasFriends === true) {
18       console.log('Cannot update host because you have friends!')
19       process.exit(-1)
20     }
21
22     console.log('Updating torrent files.')
23     db.Video.list(function (err, videos) {
24       if (err) throw err
25
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
30
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 ]
34
35         const buf = parseTorrent.toTorrentFile(parsed)
36         fs.writeFileSync(torrentPath, buf)
37       })
38
39       process.exit(0)
40     })
41   })
42 })