aboutsummaryrefslogtreecommitdiffhomepage
path: root/scripts/migrations/peertube-5.0.ts
blob: a0f51a64c53adc0c17ac346bb4885326748bc3c1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { ensureDir } from 'fs-extra'
import { Op } from 'sequelize'
import { updateTorrentMetadata } from '@server/helpers/webtorrent'
import { DIRECTORIES } from '@server/initializers/constants'
import { moveFilesIfPrivacyChanged } from '@server/lib/video-privacy'
import { VideoModel } from '@server/models/video/video'
import { MVideoFullLight } from '@server/types/models'
import { VideoPrivacy } from '@shared/models'
import { initDatabaseModels } from '../../server/initializers/database'

run()
  .then(() => process.exit(0))
  .catch(err => {
    console.error(err)
    process.exit(-1)
  })

async function run () {
  console.log('Moving private video files in dedicated folders.')

  await ensureDir(DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE)
  await ensureDir(DIRECTORIES.VIDEOS.PRIVATE)

  await initDatabaseModels(true)

  const videos = await VideoModel.unscoped().findAll({
    attributes: [ 'uuid' ],
    where: {
      privacy: {
        [Op.in]: [ VideoPrivacy.PRIVATE, VideoPrivacy.INTERNAL ]
      }
    }
  })

  for (const { uuid } of videos) {
    try {
      console.log('Moving files of video %s.', uuid)

      const video = await VideoModel.loadFull(uuid)

      try {
        await moveFilesIfPrivacyChanged(video, VideoPrivacy.PUBLIC)
      } catch (err) {
        console.error('Cannot move files of video %s.', uuid, err)
      }

      try {
        await updateTorrents(video)
      } catch (err) {
        console.error('Cannot regenerate torrents of video %s.', uuid, err)
      }
    } catch (err) {
      console.error('Cannot process video %s.', uuid, err)
    }
  }
}

async function updateTorrents (video: MVideoFullLight) {
  for (const file of video.VideoFiles) {
    await updateTorrentMetadata(video, file)

    await file.save()
  }

  const playlist = video.getHLSPlaylist()
  for (const file of (playlist?.VideoFiles || [])) {
    await updateTorrentMetadata(playlist, file)

    await file.save()
  }
}