0075-video-resolutions.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import * as Sequelize from 'sequelize'
  2. import { join } from 'path'
  3. import { CONFIG } from '../../initializers/config'
  4. import { getVideoFileResolution } from '../../helpers/ffmpeg-utils'
  5. import { readdir, rename } from 'fs-extra'
  6. function up (utils: {
  7. transaction: Sequelize.Transaction,
  8. queryInterface: Sequelize.QueryInterface,
  9. sequelize: Sequelize.Sequelize,
  10. db: any
  11. }): Promise<void> {
  12. const torrentDir = CONFIG.STORAGE.TORRENTS_DIR
  13. const videoFileDir = CONFIG.STORAGE.VIDEOS_DIR
  14. return readdir(videoFileDir)
  15. .then(videoFiles => {
  16. const tasks: Promise<any>[] = []
  17. for (const videoFile of videoFiles) {
  18. 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)
  19. if (matches === null) {
  20. console.log('Invalid video file name %s.', videoFile)
  21. continue
  22. }
  23. const uuid = matches[1]
  24. const ext = matches[2]
  25. const p = getVideoFileResolution(join(videoFileDir, videoFile))
  26. .then(height => {
  27. const oldTorrentName = uuid + '.torrent'
  28. const newTorrentName = uuid + '-' + height + '.torrent'
  29. return rename(join(torrentDir, oldTorrentName), join(torrentDir, newTorrentName)).then(() => height)
  30. })
  31. .then(height => {
  32. const newVideoFileName = uuid + '-' + height + '.' + ext
  33. return rename(join(videoFileDir, videoFile), join(videoFileDir, newVideoFileName)).then(() => height)
  34. })
  35. .then(height => {
  36. const query = 'UPDATE "VideoFiles" SET "resolution" = ' + height +
  37. ' WHERE "videoId" = (SELECT "id" FROM "Videos" WHERE "uuid" = \'' + uuid + '\')'
  38. return utils.sequelize.query(query)
  39. })
  40. tasks.push(p)
  41. }
  42. return Promise.all(tasks).then(() => undefined)
  43. })
  44. }
  45. function down (options) {
  46. throw new Error('Not implemented.')
  47. }
  48. export {
  49. up,
  50. down
  51. }