]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - scripts/update-host.ts
Automatic resolution according to user bandwidth V1
[github/Chocobozzz/PeerTube.git] / scripts / update-host.ts
index 0f6af094280eba035ef948d8d910f00abe4b82c6..ed8b999a9f42c8d43a24e2a1b9b0e1425b3a0516 100755 (executable)
@@ -1,37 +1,35 @@
-import { readFileSync, writeFileSync } from 'fs'
-import * as parseTorrent from 'parse-torrent'
-
-import { CONFIG, STATIC_PATHS } from '../server/initializers/constants'
-import { database as db } from '../server/initializers/database'
-import { hasFriends } from '../server/lib/friends'
-
-db.init(true, function () {
-  hasFriends(function (err, itHasFriends) {
-    if (err) throw err
-
-    if (itHasFriends === true) {
-      console.log('Cannot update host because you have friends!')
+import { getServerActor } from '../server/helpers/utils'
+import { initDatabaseModels } from '../server/initializers'
+import { ActorFollowModel } from '../server/models/activitypub/actor-follow'
+import { VideoModel } from '../server/models/video/video'
+
+initDatabaseModels(true)
+  .then(() => {
+    return getServerActor()
+  })
+  .then(serverAccount => {
+    return ActorFollowModel.listAcceptedFollowingUrlsForApi([ serverAccount.id ], undefined)
+  })
+  .then(res => {
+    return res.total > 0
+  })
+  .then(hasFollowing => {
+    if (hasFollowing === true) {
+      console.log('Cannot update host because you follow other servers!')
       process.exit(-1)
     }
 
     console.log('Updating torrent files.')
-    db.Video.list(function (err, videos) {
-      if (err) throw err
-
-      videos.forEach(function (video) {
-        const torrentName = video.id + '.torrent'
-        const torrentPath = CONFIG.STORAGE.TORRENTS_DIR + torrentName
-        const filename = video.id + video.extname
-
-        const parsed = parseTorrent(readFileSync(torrentPath))
-        parsed.announce = [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOST + '/tracker/socket' ]
-        parsed.urlList = [ CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + filename ]
-
-        const buf = parseTorrent.toTorrentFile(parsed)
-        writeFileSync(torrentPath, buf)
-      })
-
-      process.exit(0)
-    })
+    return VideoModel.list()
+  })
+  .then(async videos => {
+    for (const video of videos) {
+      for (const file of video.VideoFiles) {
+        await video.createTorrentAndSetInfoHash(file)
+        console.log('Updated video ' + video.uuid)
+      }
+    }
+  })
+  .then(() => {
+    process.exit(0)
   })
-})