1 import * as Sequelize from 'sequelize'
2 import { join } from 'path'
3 import { CONFIG } from '../../initializers/constants'
4 import { getVideoFileResolution } from '../../helpers/ffmpeg-utils'
5 import { readdir, rename } from 'fs-extra'
8 transaction: Sequelize.Transaction,
9 queryInterface: Sequelize.QueryInterface,
10 sequelize: Sequelize.Sequelize,
13 const torrentDir = CONFIG.STORAGE.TORRENTS_DIR
14 const videoFileDir = CONFIG.STORAGE.VIDEOS_DIR
16 return readdir(videoFileDir)
18 const tasks: Promise<any>[] = []
19 for (const videoFile of videoFiles) {
20 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)
21 if (matches === null) {
22 console.log('Invalid video file name %s.', videoFile)
26 const uuid = matches[1]
27 const ext = matches[2]
29 const p = getVideoFileResolution(join(videoFileDir, videoFile))
31 const oldTorrentName = uuid + '.torrent'
32 const newTorrentName = uuid + '-' + height + '.torrent'
33 return rename(join(torrentDir, oldTorrentName), join(torrentDir, newTorrentName)).then(() => height)
36 const newVideoFileName = uuid + '-' + height + '.' + ext
37 return rename(join(videoFileDir, videoFile), join(videoFileDir, newVideoFileName)).then(() => height)
40 const query = 'UPDATE "VideoFiles" SET "resolution" = ' + height +
41 ' WHERE "videoId" = (SELECT "id" FROM "Videos" WHERE "uuid" = \'' + uuid + '\')'
42 return utils.sequelize.query(query)
48 return Promise.all(tasks).then(() => undefined)
52 function down (options) {
53 throw new Error('Not implemented.')