aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/initializers/migrations/0585-video-file-names.ts
blob: dd5fec3a1966401eb26fa808cdc879a2779ff379 (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
import * as Sequelize from 'sequelize'

async function up (utils: {
  transaction: Sequelize.Transaction
  queryInterface: Sequelize.QueryInterface
  sequelize: Sequelize.Sequelize
  db: any
}): Promise<void> {
  for (const column of [ 'filename', 'fileUrl', 'torrentFilename', 'torrentUrl' ]) {
    const data = {
      type: Sequelize.STRING,
      allowNull: true,
      defaultValue: null
    }

    await utils.queryInterface.addColumn('videoFile', column, data)
  }

  // Generate filenames for webtorrent files
  {
    const webtorrentQuery = `SELECT "videoFile".id, "video".uuid, "videoFile".resolution, "videoFile".extname ` +
    `FROM video INNER JOIN "videoFile" ON "videoFile"."videoId" = video.id`

    const query = `UPDATE "videoFile" ` +
                  `SET filename = t.uuid || '-' || t.resolution || t.extname, ` +
                  `"torrentFilename" = t.uuid || '-' || t.resolution || '.torrent' ` +
                  `FROM (${webtorrentQuery}) AS t WHERE t.id = "videoFile"."id"`

    await utils.sequelize.query(query)
  }

  // Generate filenames for HLS files
  {
    const hlsQuery = `SELECT "videoFile".id, "video".uuid, "videoFile".resolution, "videoFile".extname ` +
                     `FROM video ` +
                     `INNER JOIN "videoStreamingPlaylist" ON "videoStreamingPlaylist"."videoId" = video.id ` +
                     `INNER JOIN "videoFile" ON "videoFile"."videoStreamingPlaylistId" = "videoStreamingPlaylist".id`

    const query = `UPDATE "videoFile" ` +
                  `SET filename = t.uuid || '-' || t.resolution || '-fragmented' || t.extname, ` +
                  `"torrentFilename" = t.uuid || '-' || t.resolution || '-hls.torrent' ` +
                  `FROM (${hlsQuery}) AS t WHERE t.id = "videoFile"."id"`

    await utils.sequelize.query(query)
  }
}

function down (options) {
  throw new Error('Not implemented.')
}

export {
  up,
  down
}