6bc1e72abf7dd63f5b751a777c95fefff1e693b8
[oweals/peertube.git] / server / initializers / migrations / 0075-video-resolutions.ts
1 import * as Sequelize from 'sequelize'
2 import * as Promise from 'bluebird'
3 import { join } from 'path'
4
5 import { readdirPromise, renamePromise } from '../../helpers/core-utils'
6 import { CONFIG } from '../../initializers/constants'
7
8 function up (utils: {
9   transaction: Sequelize.Transaction,
10   queryInterface: Sequelize.QueryInterface,
11   sequelize: Sequelize.Sequelize,
12   db: any
13 }): Promise<void> {
14   const torrentDir = CONFIG.STORAGE.TORRENTS_DIR
15   const videoFileDir = CONFIG.STORAGE.VIDEOS_DIR
16
17   return readdirPromise(torrentDir)
18     .then(torrentFiles => {
19       const tasks: Promise<any>[] = []
20       for (const torrentFile of torrentFiles) {
21         const matches = /^([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\.torrent/.exec(torrentFile)
22         if (matches === null) {
23           console.log('Invalid torrent file name %s.', torrentFile)
24           continue
25         }
26
27         const newTorrentName = matches[1] + '-original.torrent'
28         const p = renamePromise(join(torrentDir, torrentFile), join(torrentDir, newTorrentName))
29         tasks.push(p)
30       }
31
32       return Promise.all(tasks)
33     })
34     .then(() => {
35       return readdirPromise(videoFileDir)
36     })
37     .then(videoFiles => {
38       const tasks: Promise<any>[] = []
39       for (const videoFile of videoFiles) {
40         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)
41         if (matches === null) {
42           console.log('Invalid video file name %s.', videoFile)
43           continue
44         }
45
46         const newVideoFileName = matches[1] + '-original.' + matches[2]
47         const p = renamePromise(join(videoFileDir, videoFile), join(videoFileDir, newVideoFileName))
48         tasks.push(p)
49       }
50
51       return Promise.all(tasks).then(() => undefined)
52     })
53 }
54
55 function down (options) {
56   throw new Error('Not implemented.')
57 }
58
59 export {
60   up,
61   down
62 }