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 { CONFIG } from '../../initializers/config'
+import { getVideoFileResolution } from '../../helpers/ffprobe-utils'
+import { readdir, rename } from 'fs-extra'
function up (utils: {
- transaction: Sequelize.Transaction,
- queryInterface: Sequelize.QueryInterface,
- sequelize: Sequelize.Sequelize,
+ transaction: Sequelize.Transaction
+ queryInterface: Sequelize.QueryInterface
+ sequelize: Sequelize.Sequelize
db: any
}): Promise<void> {
const torrentDir = CONFIG.STORAGE.TORRENTS_DIR
const videoFileDir = CONFIG.STORAGE.VIDEOS_DIR
- return readdirPromise(torrentDir)
- .then(torrentFiles => {
- const tasks: Promise<any>[] = []
- 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<any>[] = []
for (const videoFile of videoFiles) {
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(async ({ resolution }) => {
+ const oldTorrentName = uuid + '.torrent'
+ const newTorrentName = uuid + '-' + resolution + '.torrent'
+ await rename(join(torrentDir, oldTorrentName), join(torrentDir, newTorrentName)).then(() => resolution)
+
+ const newVideoFileName = uuid + '-' + resolution + '.' + ext
+ await rename(join(videoFileDir, videoFile), join(videoFileDir, newVideoFileName)).then(() => resolution)
+
+ const query = 'UPDATE "VideoFiles" SET "resolution" = ' + resolution +
+ ' WHERE "videoId" = (SELECT "id" FROM "Videos" WHERE "uuid" = \'' + uuid + '\')'
+ return utils.sequelize.query(query)
+ })
+
tasks.push(p)
}