]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/initializers/migrations/0075-video-resolutions.ts
Split ffmpeg utils with ffprobe utils
[github/Chocobozzz/PeerTube.git] / server / initializers / migrations / 0075-video-resolutions.ts
CommitLineData
ecb455b6 1import * as Sequelize from 'sequelize'
ecb455b6 2import { join } from 'path'
6dd9de95 3import { CONFIG } from '../../initializers/config'
daf6e480 4import { getVideoFileResolution } from '../../helpers/ffprobe-utils'
62689b94 5import { readdir, rename } from 'fs-extra'
ecb455b6
C
6
7function up (utils: {
a1587156
C
8 transaction: Sequelize.Transaction
9 queryInterface: Sequelize.QueryInterface
10 sequelize: Sequelize.Sequelize
ecb455b6
C
11 db: any
12}): Promise<void> {
13 const torrentDir = CONFIG.STORAGE.TORRENTS_DIR
14 const videoFileDir = CONFIG.STORAGE.VIDEOS_DIR
15
62689b94 16 return readdir(videoFileDir)
ecb455b6
C
17 .then(videoFiles => {
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)
23 continue
24 }
25
14d3270f
C
26 const uuid = matches[1]
27 const ext = matches[2]
28
056aa7f2 29 const p = getVideoFileResolution(join(videoFileDir, videoFile))
14d3270f
C
30 .then(height => {
31 const oldTorrentName = uuid + '.torrent'
32 const newTorrentName = uuid + '-' + height + '.torrent'
62689b94 33 return rename(join(torrentDir, oldTorrentName), join(torrentDir, newTorrentName)).then(() => height)
14d3270f
C
34 })
35 .then(height => {
36 const newVideoFileName = uuid + '-' + height + '.' + ext
62689b94 37 return rename(join(videoFileDir, videoFile), join(videoFileDir, newVideoFileName)).then(() => height)
14d3270f
C
38 })
39 .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)
43 })
44
ecb455b6
C
45 tasks.push(p)
46 }
47
48 return Promise.all(tasks).then(() => undefined)
49 })
50}
51
52function down (options) {
53 throw new Error('Not implemented.')
54}
55
56export {
57 up,
58 down
59}