1 import * as Sequelize from 'sequelize'
2 import { join } from 'path'
4 import { readdirPromise, renamePromise } from '../../helpers/core-utils'
5 import { CONFIG } from '../../initializers/constants'
6 import { getVideoFileResolution } from '../../helpers/ffmpeg-utils'
9 transaction: Sequelize.Transaction,
10 queryInterface: Sequelize.QueryInterface,
11 sequelize: Sequelize.Sequelize,
14 const torrentDir = CONFIG.STORAGE.TORRENTS_DIR
15 const videoFileDir = CONFIG.STORAGE.VIDEOS_DIR
17 return readdirPromise(videoFileDir)
19 const tasks: Promise<any>[] = []
20 for (const videoFile of videoFiles) {
21 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)
22 if (matches === null) {
23 console.log('Invalid video file name %s.', videoFile)
27 const uuid = matches[1]
28 const ext = matches[2]
30 const p = getVideoFileResolution(join(videoFileDir, videoFile))
32 const oldTorrentName = uuid + '.torrent'
33 const newTorrentName = uuid + '-' + height + '.torrent'
34 return renamePromise(join(torrentDir, oldTorrentName), join(torrentDir, newTorrentName)).then(() => height)
37 const newVideoFileName = uuid + '-' + height + '.' + ext
38 return renamePromise(join(videoFileDir, videoFile), join(videoFileDir, newVideoFileName)).then(() => height)
41 const query = 'UPDATE "VideoFiles" SET "resolution" = ' + height +
42 ' WHERE "videoId" = (SELECT "id" FROM "Videos" WHERE "uuid" = \'' + uuid + '\')'
43 return utils.sequelize.query(query)
49 return Promise.all(tasks).then(() => undefined)
53 function down (options) {
54 throw new Error('Not implemented.')