X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Finitializers%2Fmigrations%2F0075-video-resolutions.ts;h=26a188e5e8f834ac0e05f64a6ebea89cb825768e;hb=2c3abc4fa796555eb7d25f416c4f41ab3e3ad8ca;hp=6bc1e72abf7dd63f5b751a777c95fefff1e693b8;hpb=ecb455b6c42bd75c9d87294c2479fa53b739d0a8;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/initializers/migrations/0075-video-resolutions.ts b/server/initializers/migrations/0075-video-resolutions.ts index 6bc1e72ab..26a188e5e 100644 --- a/server/initializers/migrations/0075-video-resolutions.ts +++ b/server/initializers/migrations/0075-video-resolutions.ts @@ -1,9 +1,8 @@ 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' +import { getVideoFileResolution } from '../../helpers/ffmpeg-utils' +import { readdir, rename } from 'fs-extra' function up (utils: { transaction: Sequelize.Transaction, @@ -14,26 +13,7 @@ function up (utils: { 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) - }) + return readdir(videoFileDir) .then(videoFiles => { const tasks: Promise[] = [] for (const videoFile of videoFiles) { @@ -43,8 +23,25 @@ function up (utils: { continue } - const newVideoFileName = matches[1] + '-original.' + matches[2] - const p = renamePromise(join(videoFileDir, videoFile), join(videoFileDir, newVideoFileName)) + const uuid = matches[1] + const ext = matches[2] + + const p = getVideoFileResolution(join(videoFileDir, videoFile)) + .then(height => { + const oldTorrentName = uuid + '.torrent' + const newTorrentName = uuid + '-' + height + '.torrent' + return rename(join(torrentDir, oldTorrentName), join(torrentDir, newTorrentName)).then(() => height) + }) + .then(height => { + const newVideoFileName = uuid + '-' + height + '.' + ext + return rename(join(videoFileDir, videoFile), join(videoFileDir, newVideoFileName)).then(() => height) + }) + .then(height => { + const query = 'UPDATE "VideoFiles" SET "resolution" = ' + height + + ' WHERE "videoId" = (SELECT "id" FROM "Videos" WHERE "uuid" = \'' + uuid + '\')' + return utils.sequelize.query(query) + }) + tasks.push(p) }