2017-10-03 09:04:14 -05:00
|
|
|
import * as Sequelize from 'sequelize'
|
|
|
|
import { join } from 'path'
|
2019-04-11 04:33:44 -05:00
|
|
|
import { CONFIG } from '../../initializers/config'
|
2018-02-27 08:57:28 -06:00
|
|
|
import { getVideoFileResolution } from '../../helpers/ffmpeg-utils'
|
2018-08-27 09:23:34 -05:00
|
|
|
import { readdir, rename } from 'fs-extra'
|
2017-10-03 09:04:14 -05:00
|
|
|
|
|
|
|
function up (utils: {
|
|
|
|
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
|
|
|
|
|
2018-08-27 09:23:34 -05:00
|
|
|
return readdir(videoFileDir)
|
2017-10-03 09:04:14 -05:00
|
|
|
.then(videoFiles => {
|
|
|
|
const tasks: Promise<any>[] = []
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-10-09 04:06:13 -05:00
|
|
|
const uuid = matches[1]
|
|
|
|
const ext = matches[2]
|
|
|
|
|
2018-02-27 08:57:28 -06:00
|
|
|
const p = getVideoFileResolution(join(videoFileDir, videoFile))
|
2017-10-09 04:06:13 -05:00
|
|
|
.then(height => {
|
|
|
|
const oldTorrentName = uuid + '.torrent'
|
|
|
|
const newTorrentName = uuid + '-' + height + '.torrent'
|
2018-08-27 09:23:34 -05:00
|
|
|
return rename(join(torrentDir, oldTorrentName), join(torrentDir, newTorrentName)).then(() => height)
|
2017-10-09 04:06:13 -05:00
|
|
|
})
|
|
|
|
.then(height => {
|
|
|
|
const newVideoFileName = uuid + '-' + height + '.' + ext
|
2018-08-27 09:23:34 -05:00
|
|
|
return rename(join(videoFileDir, videoFile), join(videoFileDir, newVideoFileName)).then(() => height)
|
2017-10-09 04:06:13 -05:00
|
|
|
})
|
|
|
|
.then(height => {
|
|
|
|
const query = 'UPDATE "VideoFiles" SET "resolution" = ' + height +
|
|
|
|
' WHERE "videoId" = (SELECT "id" FROM "Videos" WHERE "uuid" = \'' + uuid + '\')'
|
|
|
|
return utils.sequelize.query(query)
|
|
|
|
})
|
|
|
|
|
2017-10-03 09:04:14 -05:00
|
|
|
tasks.push(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.all(tasks).then(() => undefined)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function down (options) {
|
|
|
|
throw new Error('Not implemented.')
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
up,
|
|
|
|
down
|
|
|
|
}
|