Server: add migration scripts to the new mongo schemes

pull/24/head
Chocobozzz 2016-11-14 22:49:19 +01:00
parent c92372d0aa
commit 2550fab35e
6 changed files with 200 additions and 2 deletions

View 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)

View 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']

View File

@ -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.')
}

View File

@ -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.')
}

View File

@ -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.')
}

View File

@ -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)
})
}