0065-video-file-size.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import * as Sequelize from 'sequelize'
  2. import * as Promise from 'bluebird'
  3. import { stat } from 'fs-extra'
  4. import { VideoModel } from '../../models/video/video'
  5. function up (utils: {
  6. transaction: Sequelize.Transaction,
  7. queryInterface: Sequelize.QueryInterface,
  8. sequelize: Sequelize.Sequelize,
  9. db: any
  10. }): Promise<void> {
  11. return utils.db.Video.listOwnedAndPopulateAuthorAndTags()
  12. .then((videos: VideoModel[]) => {
  13. const tasks: Promise<any>[] = []
  14. videos.forEach(video => {
  15. video.VideoFiles.forEach(videoFile => {
  16. const p = new Promise((res, rej) => {
  17. stat(video.getVideoFilePath(videoFile), (err, stats) => {
  18. if (err) return rej(err)
  19. videoFile.size = stats.size
  20. videoFile.save().then(res).catch(rej)
  21. })
  22. })
  23. tasks.push(p)
  24. })
  25. })
  26. return tasks
  27. })
  28. .then((tasks: Promise<any>[]) => {
  29. return Promise.all(tasks)
  30. })
  31. }
  32. function down (options) {
  33. throw new Error('Not implemented.')
  34. }
  35. export {
  36. up,
  37. down
  38. }