Server: add migration scripts to the new mongo schemes
authorChocobozzz <florian.bigard@gmail.com>
Mon, 14 Nov 2016 21:49:19 +0000 (22:49 +0100)
committerChocobozzz <florian.bigard@gmail.com>
Wed, 16 Nov 2016 19:29:26 +0000 (20:29 +0100)
server/controllers/client.js
server/initializers/constants.js
server/initializers/migrations/0025-video-filenames.js [new file with mode: 0644]
server/initializers/migrations/0030-video-magnet.js [new file with mode: 0644]
server/initializers/migrations/0035-url-to-host.js [new file with mode: 0644]
server/initializers/migrations/0040-video-remote-id.js [new file with mode: 0644]

index 746c9b62bbf7945f3f432cd84fda56214b2d8326..68ddfccf2523a0ab52934fb077c068d1d8a65904 100644 (file)
@@ -39,7 +39,7 @@ function addOpenGraphTags (htmlStringPage, video) {
   if (video.isOwned()) {
     baseUrlHttp = constants.CONFIG.WEBSERVER.URL
   } else {
-    baseUrlHttp = constants.REMOTE_SCHEME.HTTP + '://' + video.podUrl
+    baseUrlHttp = constants.REMOTE_SCHEME.HTTP + '://' + video.podHost
   }
 
   // We fetch the remote preview (bigger than the thumbnail)
@@ -88,7 +88,7 @@ function generateWatchHtmlPage (req, res, next) {
     if (err) return next(err)
 
     const html = results.file.toString()
-    const video = results.video.toFormatedJSON()
+    const video = results.video
 
     const htmlStringPageWithTags = addOpenGraphTags(html, video)
     res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)
index d8a13d066930adab55fd196ab7453995b0fa8246..40e1c5381c9968e3215a630fdd5dd22ceb1f5c21 100644 (file)
@@ -104,6 +104,22 @@ const MONGO_MIGRATION_SCRIPTS = [
   {
     script: '0020-requests-endpoint',
     version: 20
+  },
+  {
+    script: '0025-video-filenames',
+    version: 25
+  },
+  {
+    script: '0030-video-magnet',
+    version: 30
+  },
+  {
+    script: '0035-url-to-host',
+    version: 35
+  },
+  {
+    script: '0040-video-remote-id',
+    version: 40
   }
 ]
 const LAST_MONGO_SCHEMA_VERSION = (maxBy(MONGO_MIGRATION_SCRIPTS, 'version'))['version']
diff --git a/server/initializers/migrations/0025-video-filenames.js b/server/initializers/migrations/0025-video-filenames.js
new file mode 100644 (file)
index 0000000..df21494
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+  Rename thumbnails and video filenames to _id.extension
+*/
+
+const each = require('async/each')
+const fs = require('fs')
+const path = require('path')
+const mongoose = require('mongoose')
+
+const constants = require('../constants')
+const logger = require('../../helpers/logger')
+
+const Video = mongoose.model('Video')
+
+exports.up = function (callback) {
+  // Use of lean because the new Video scheme does not have filename field
+  Video.find({ filename: { $ne: null } }).lean().exec(function (err, videos) {
+    if (err) throw err
+
+    each(videos, function (video, callbackEach) {
+      const torrentName = video.filename + '.torrent'
+      const thumbnailName = video.thumbnail
+      const thumbnailExtension = path.extname(thumbnailName)
+      const videoName = video.filename
+      const videoExtension = path.extname(videoName)
+
+      const newTorrentName = video._id + '.torrent'
+      const newThumbnailName = video._id + thumbnailExtension
+      const newVideoName = video._id + videoExtension
+
+      const torrentsDir = constants.CONFIG.STORAGE.TORRENTS_DIR
+      const thumbnailsDir = constants.CONFIG.STORAGE.THUMBNAILS_DIR
+      const videosDir = constants.CONFIG.STORAGE.VIDEOS_DIR
+
+      logger.info('Renaming %s to %s.', torrentsDir + torrentName, torrentsDir + newTorrentName)
+      fs.renameSync(torrentsDir + torrentName, torrentsDir + newTorrentName)
+
+      logger.info('Renaming %s to %s.', thumbnailsDir + thumbnailName, thumbnailsDir + newThumbnailName)
+      fs.renameSync(thumbnailsDir + thumbnailName, thumbnailsDir + newThumbnailName)
+
+      logger.info('Renaming %s to %s.', videosDir + videoName, videosDir + newVideoName)
+      fs.renameSync(videosDir + videoName, videosDir + newVideoName)
+
+      Video.load(video._id, function (err, videoObj) {
+        if (err) return callbackEach(err)
+
+        videoObj.extname = videoExtension
+        videoObj.remoteId = null
+        videoObj.save(callbackEach)
+      })
+    }, callback)
+  })
+}
+
+exports.down = function (callback) {
+  throw new Error('Not implemented.')
+}
diff --git a/server/initializers/migrations/0030-video-magnet.js b/server/initializers/migrations/0030-video-magnet.js
new file mode 100644 (file)
index 0000000..b9119d6
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+  Change video magnet structures
+*/
+
+const each = require('async/each')
+const magnet = require('magnet-uri')
+const mongoose = require('mongoose')
+
+const Video = mongoose.model('Video')
+
+exports.up = function (callback) {
+  // Use of lean because the new Video scheme does not have magnetUri field
+  Video.find({ }).lean().exec(function (err, videos) {
+    if (err) throw err
+
+    each(videos, function (video, callbackEach) {
+      const parsed = magnet.decode(video.magnetUri)
+      const infoHash = parsed.infoHash
+
+      Video.load(video._id, function (err, videoObj) {
+        if (err) return callbackEach(err)
+
+        videoObj.magnet.infoHash = infoHash
+        videoObj.save(callbackEach)
+      })
+    }, callback)
+  })
+}
+
+exports.down = function (callback) {
+  throw new Error('Not implemented.')
+}
diff --git a/server/initializers/migrations/0035-url-to-host.js b/server/initializers/migrations/0035-url-to-host.js
new file mode 100644 (file)
index 0000000..6243304
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+  Change video magnet structures
+*/
+
+const each = require('async/each')
+const mongoose = require('mongoose')
+
+const Video = mongoose.model('Video')
+
+exports.up = function (callback) {
+  // Use of lean because the new Video scheme does not have podUrl field
+  Video.find({ }).lean().exec(function (err, videos) {
+    if (err) throw err
+
+    each(videos, function (video, callbackEach) {
+      Video.load(video._id, function (err, videoObj) {
+        if (err) return callbackEach(err)
+
+        const host = video.podUrl.split('://')[1]
+
+        videoObj.podHost = host
+        videoObj.save(callbackEach)
+      })
+    }, callback)
+  })
+}
+
+exports.down = function (callback) {
+  throw new Error('Not implemented.')
+}
diff --git a/server/initializers/migrations/0040-video-remote-id.js b/server/initializers/migrations/0040-video-remote-id.js
new file mode 100644 (file)
index 0000000..5cf856b
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+  Use remote id as identifier
+*/
+
+const each = require('async/each')
+const map = require('lodash/map')
+const mongoose = require('mongoose')
+const readline = require('readline')
+
+const rl = readline.createInterface({
+  input: process.stdin,
+  output: process.stdout
+})
+
+const logger = require('../../helpers/logger')
+const friends = require('../../lib/friends')
+
+const Pod = mongoose.model('Pod')
+const Video = mongoose.model('Video')
+
+exports.up = function (callback) {
+  Pod.find({}).lean().exec(function (err, pods) {
+    if (err) return callback(err)
+
+    // We need to quit friends first
+    if (pods.length === 0) {
+      return setVideosRemoteId(callback)
+    }
+
+    const timeout = setTimeout(function () {
+      throw new Error('You need to enter a value!')
+    }, 10000)
+
+    rl.question('I am sorry but I need to quit friends for upgrading. Do you want to continue? (yes/*)', function (answer) {
+      if (answer !== 'yes') throw new Error('I cannot continue.')
+
+      clearTimeout(timeout)
+      rl.close()
+
+      const urls = map(pods, 'url')
+      logger.info('Saying goodbye to: ' + urls.join(', '))
+
+      friends.quitFriends(function () {
+        setVideosRemoteId(callback)
+      })
+    })
+  })
+}
+
+exports.down = function (callback) {
+  throw new Error('Not implemented.')
+}
+
+function setVideosRemoteId (callback) {
+  Video.find({}, function (err, videos) {
+    if (err) return callback(err)
+
+    each(videos, function (video, callbackEach) {
+      video.remoteId = null
+      video.save(callbackEach)
+    }, callback)
+  })
+}