]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/initializers/migrations/0065-video-file-size.ts
Move video file metadata in their own table
[github/Chocobozzz/PeerTube.git] / server / initializers / migrations / 0065-video-file-size.ts
1 import * as Sequelize from 'sequelize'
2 import * as Promise from 'bluebird'
3 import { stat } from 'fs'
4
5 import { VideoInstance } from '../../models'
6
7 function up (utils: {
8 transaction: Sequelize.Transaction,
9 queryInterface: Sequelize.QueryInterface,
10 sequelize: Sequelize.Sequelize,
11 db: any
12 }): Promise<void> {
13 return utils.db.Video.listOwnedAndPopulateAuthorAndTags()
14 .then((videos: VideoInstance[]) => {
15 const tasks: Promise<any>[] = []
16
17 videos.forEach(video => {
18 video.VideoFiles.forEach(videoFile => {
19 const p = new Promise((res, rej) => {
20 stat(video.getVideoFilePath(videoFile), (err, stats) => {
21 if (err) return rej(err)
22
23 videoFile.size = stats.size
24 videoFile.save().then(res).catch(rej)
25 })
26 })
27
28 tasks.push(p)
29 })
30 })
31
32 return tasks
33 })
34 .then((tasks: Promise<any>[]) => {
35 return Promise.all(tasks)
36 })
37 }
38
39 function down (options) {
40 throw new Error('Not implemented.')
41 }
42
43 export {
44 up,
45 down
46 }