2022-02-11 04:51:33 -05:00
|
|
|
import { readdir, rename } from 'fs-extra'
|
2017-10-03 10:04:14 -04:00
|
|
|
import { join } from 'path'
|
2022-02-11 04:51:33 -05:00
|
|
|
import * as Sequelize from 'sequelize'
|
|
|
|
import { getVideoStreamDimensionsInfo } from '../../helpers/ffmpeg/ffprobe-utils'
|
2019-04-11 05:33:44 -04:00
|
|
|
import { CONFIG } from '../../initializers/config'
|
2017-10-03 10:04:14 -04:00
|
|
|
|
|
|
|
function up (utils: {
|
2020-01-31 10:56:52 -05:00
|
|
|
transaction: Sequelize.Transaction
|
|
|
|
queryInterface: Sequelize.QueryInterface
|
|
|
|
sequelize: Sequelize.Sequelize
|
2017-10-03 10:04:14 -04:00
|
|
|
db: any
|
|
|
|
}): Promise<void> {
|
|
|
|
const torrentDir = CONFIG.STORAGE.TORRENTS_DIR
|
|
|
|
const videoFileDir = CONFIG.STORAGE.VIDEOS_DIR
|
|
|
|
|
2018-08-27 10:23:34 -04:00
|
|
|
return readdir(videoFileDir)
|
2017-10-03 10:04:14 -04: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 05:06:13 -04:00
|
|
|
const uuid = matches[1]
|
|
|
|
const ext = matches[2]
|
|
|
|
|
2022-02-11 04:51:33 -05:00
|
|
|
const p = getVideoStreamDimensionsInfo(join(videoFileDir, videoFile))
|
2021-08-06 07:35:25 -04:00
|
|
|
.then(async ({ resolution }) => {
|
2017-10-09 05:06:13 -04:00
|
|
|
const oldTorrentName = uuid + '.torrent'
|
2021-08-06 07:35:25 -04:00
|
|
|
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 +
|
2017-10-09 05:06:13 -04:00
|
|
|
' WHERE "videoId" = (SELECT "id" FROM "Videos" WHERE "uuid" = \'' + uuid + '\')'
|
|
|
|
return utils.sequelize.query(query)
|
|
|
|
})
|
|
|
|
|
2017-10-03 10:04:14 -04:00
|
|
|
tasks.push(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.all(tasks).then(() => undefined)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function down (options) {
|
|
|
|
throw new Error('Not implemented.')
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
up,
|
|
|
|
down
|
|
|
|
}
|