From ecb455b6c42bd75c9d87294c2479fa53b739d0a8 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 3 Oct 2017 16:04:14 +0200 Subject: Add migration script --- server/initializers/constants.ts | 5 +- .../migrations/0075-video-resolutions.ts | 62 ++++++++++++++++++++++ 2 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 server/initializers/migrations/0075-video-resolutions.ts (limited to 'server/initializers') diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index 073fabd27..f87041a3f 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts @@ -10,13 +10,12 @@ import { RequestEndpoint, RequestVideoEventType, RequestVideoQaduType, - JobState, - VideoResolution + JobState } from '../../shared/models' // --------------------------------------------------------------------------- -const LAST_MIGRATION_VERSION = 70 +const LAST_MIGRATION_VERSION = 75 // --------------------------------------------------------------------------- diff --git a/server/initializers/migrations/0075-video-resolutions.ts b/server/initializers/migrations/0075-video-resolutions.ts new file mode 100644 index 000000000..6bc1e72ab --- /dev/null +++ b/server/initializers/migrations/0075-video-resolutions.ts @@ -0,0 +1,62 @@ +import * as Sequelize from 'sequelize' +import * as Promise from 'bluebird' +import { join } from 'path' + +import { readdirPromise, renamePromise } from '../../helpers/core-utils' +import { CONFIG } from '../../initializers/constants' + +function up (utils: { + transaction: Sequelize.Transaction, + queryInterface: Sequelize.QueryInterface, + sequelize: Sequelize.Sequelize, + db: any +}): Promise { + const torrentDir = CONFIG.STORAGE.TORRENTS_DIR + const videoFileDir = CONFIG.STORAGE.VIDEOS_DIR + + return readdirPromise(torrentDir) + .then(torrentFiles => { + const tasks: Promise[] = [] + for (const torrentFile of torrentFiles) { + const matches = /^([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\.torrent/.exec(torrentFile) + if (matches === null) { + console.log('Invalid torrent file name %s.', torrentFile) + continue + } + + const newTorrentName = matches[1] + '-original.torrent' + const p = renamePromise(join(torrentDir, torrentFile), join(torrentDir, newTorrentName)) + tasks.push(p) + } + + return Promise.all(tasks) + }) + .then(() => { + return readdirPromise(videoFileDir) + }) + .then(videoFiles => { + const tasks: Promise[] = [] + for (const videoFile of videoFiles) { + const matches = /^([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\.([a-z0-9]+)/.exec(videoFile) + if (matches === null) { + console.log('Invalid video file name %s.', videoFile) + continue + } + + const newVideoFileName = matches[1] + '-original.' + matches[2] + const p = renamePromise(join(videoFileDir, videoFile), join(videoFileDir, newVideoFileName)) + tasks.push(p) + } + + return Promise.all(tasks).then(() => undefined) + }) +} + +function down (options) { + throw new Error('Not implemented.') +} + +export { + up, + down +} -- cgit v1.2.3