From 2550fab35e0113264369f9637e1bea169efdfc8f Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 14 Nov 2016 22:49:19 +0100 Subject: [PATCH] Server: add migration scripts to the new mongo schemes --- server/controllers/client.js | 4 +- server/initializers/constants.js | 16 +++++ .../migrations/0025-video-filenames.js | 57 +++++++++++++++++ .../migrations/0030-video-magnet.js | 32 ++++++++++ .../migrations/0035-url-to-host.js | 30 +++++++++ .../migrations/0040-video-remote-id.js | 63 +++++++++++++++++++ 6 files changed, 200 insertions(+), 2 deletions(-) create mode 100644 server/initializers/migrations/0025-video-filenames.js create mode 100644 server/initializers/migrations/0030-video-magnet.js create mode 100644 server/initializers/migrations/0035-url-to-host.js create mode 100644 server/initializers/migrations/0040-video-remote-id.js diff --git a/server/controllers/client.js b/server/controllers/client.js index 746c9b62b..68ddfccf2 100644 --- a/server/controllers/client.js +++ b/server/controllers/client.js @@ -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) diff --git a/server/initializers/constants.js b/server/initializers/constants.js index d8a13d066..40e1c5381 100644 --- a/server/initializers/constants.js +++ b/server/initializers/constants.js @@ -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 index 000000000..df21494d7 --- /dev/null +++ b/server/initializers/migrations/0025-video-filenames.js @@ -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 index 000000000..b9119d61c --- /dev/null +++ b/server/initializers/migrations/0030-video-magnet.js @@ -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 index 000000000..6243304d5 --- /dev/null +++ b/server/initializers/migrations/0035-url-to-host.js @@ -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 index 000000000..5cf856b2e --- /dev/null +++ b/server/initializers/migrations/0040-video-remote-id.js @@ -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) + }) +} -- 2.25.1