ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

131 lines
5.0 KiB

  1. import * as Sequelize from 'sequelize'
  2. async function up (utils: {
  3. transaction: Sequelize.Transaction
  4. queryInterface: Sequelize.QueryInterface
  5. sequelize: Sequelize.Sequelize
  6. db: any
  7. }): Promise<void> {
  8. // Torrent and file URLs
  9. {
  10. const fromQueryWebtorrent = `SELECT 'https://' || server.host AS "serverUrl", '/static/webseed/' AS "filePath", "videoFile".id ` +
  11. `FROM video ` +
  12. `INNER JOIN "videoChannel" ON "videoChannel".id = video."channelId" ` +
  13. `INNER JOIN actor ON actor.id = "videoChannel"."actorId" ` +
  14. `INNER JOIN server ON server.id = actor."serverId" ` +
  15. `INNER JOIN "videoFile" ON "videoFile"."videoId" = video.id ` +
  16. `WHERE video.remote IS TRUE`
  17. const fromQueryHLS = `SELECT 'https://' || server.host AS "serverUrl", ` +
  18. `'/static/streaming-playlists/hls/' || video.uuid || '/' AS "filePath", "videoFile".id ` +
  19. `FROM video ` +
  20. `INNER JOIN "videoChannel" ON "videoChannel".id = video."channelId" ` +
  21. `INNER JOIN actor ON actor.id = "videoChannel"."actorId" ` +
  22. `INNER JOIN server ON server.id = actor."serverId" ` +
  23. `INNER JOIN "videoStreamingPlaylist" ON "videoStreamingPlaylist"."videoId" = video.id ` +
  24. `INNER JOIN "videoFile" ON "videoFile"."videoStreamingPlaylistId" = "videoStreamingPlaylist".id ` +
  25. `WHERE video.remote IS TRUE`
  26. for (const fromQuery of [ fromQueryWebtorrent, fromQueryHLS ]) {
  27. const query = `UPDATE "videoFile" ` +
  28. `SET "torrentUrl" = t."serverUrl" || '/static/torrents/' || "videoFile"."torrentFilename", ` +
  29. `"fileUrl" = t."serverUrl" || t."filePath" || "videoFile"."filename" ` +
  30. `FROM (${fromQuery}) AS t WHERE t.id = "videoFile"."id" AND "videoFile"."fileUrl" IS NULL`
  31. await utils.sequelize.query(query)
  32. }
  33. }
  34. // Caption URLs
  35. {
  36. const fromQuery = `SELECT 'https://' || server.host AS "serverUrl", "video".uuid, "videoCaption".id ` +
  37. `FROM video ` +
  38. `INNER JOIN "videoChannel" ON "videoChannel".id = video."channelId" ` +
  39. `INNER JOIN actor ON actor.id = "videoChannel"."actorId" ` +
  40. `INNER JOIN server ON server.id = actor."serverId" ` +
  41. `INNER JOIN "videoCaption" ON "videoCaption"."videoId" = video.id ` +
  42. `WHERE video.remote IS TRUE`
  43. const query = `UPDATE "videoCaption" ` +
  44. `SET "fileUrl" = t."serverUrl" || '/lazy-static/video-captions/' || t.uuid || '-' || "videoCaption"."language" || '.vtt' ` +
  45. `FROM (${fromQuery}) AS t WHERE t.id = "videoCaption"."id" AND "videoCaption"."fileUrl" IS NULL`
  46. await utils.sequelize.query(query)
  47. }
  48. // Thumbnail URLs
  49. {
  50. const fromQuery = `SELECT 'https://' || server.host AS "serverUrl", "video".uuid, "thumbnail".id ` +
  51. `FROM video ` +
  52. `INNER JOIN "videoChannel" ON "videoChannel".id = video."channelId" ` +
  53. `INNER JOIN actor ON actor.id = "videoChannel"."actorId" ` +
  54. `INNER JOIN server ON server.id = actor."serverId" ` +
  55. `INNER JOIN "thumbnail" ON "thumbnail"."videoId" = video.id ` +
  56. `WHERE video.remote IS TRUE`
  57. // Thumbnails
  58. {
  59. const query = `UPDATE "thumbnail" ` +
  60. `SET "fileUrl" = t."serverUrl" || '/static/thumbnails/' || t.uuid || '.jpg' ` +
  61. `FROM (${fromQuery}) AS t WHERE t.id = "thumbnail"."id" AND "thumbnail"."fileUrl" IS NULL AND thumbnail.type = 1`
  62. await utils.sequelize.query(query)
  63. }
  64. {
  65. // Previews
  66. const query = `UPDATE "thumbnail" ` +
  67. `SET "fileUrl" = t."serverUrl" || '/lazy-static/previews/' || t.uuid || '.jpg' ` +
  68. `FROM (${fromQuery}) AS t WHERE t.id = "thumbnail"."id" AND "thumbnail"."fileUrl" IS NULL AND thumbnail.type = 2`
  69. await utils.sequelize.query(query)
  70. }
  71. }
  72. // Trackers
  73. {
  74. const trackerUrls = [
  75. `'https://' || server.host || '/tracker/announce'`,
  76. `'wss://' || server.host || '/tracker/socket'`
  77. ]
  78. for (const trackerUrl of trackerUrls) {
  79. {
  80. const query = `INSERT INTO "tracker" ("url", "createdAt", "updatedAt") ` +
  81. `SELECT ${trackerUrl} AS "url", NOW(), NOW() ` +
  82. `FROM video ` +
  83. `INNER JOIN "videoChannel" ON "videoChannel".id = video."channelId" ` +
  84. `INNER JOIN actor ON actor.id = "videoChannel"."actorId" ` +
  85. `INNER JOIN server ON server.id = actor."serverId" ` +
  86. `WHERE video.remote IS TRUE ` +
  87. `ON CONFLICT DO NOTHING`
  88. await utils.sequelize.query(query)
  89. }
  90. {
  91. const query = `INSERT INTO "videoTracker" ("videoId", "trackerId", "createdAt", "updatedAt") ` +
  92. `SELECT video.id, (SELECT tracker.id FROM tracker WHERE url = ${trackerUrl}) AS "trackerId", NOW(), NOW()` +
  93. `FROM video ` +
  94. `INNER JOIN "videoChannel" ON "videoChannel".id = video."channelId" ` +
  95. `INNER JOIN actor ON actor.id = "videoChannel"."actorId" ` +
  96. `INNER JOIN server ON server.id = actor."serverId" ` +
  97. `WHERE video.remote IS TRUE`
  98. await utils.sequelize.query(query)
  99. }
  100. }
  101. }
  102. }
  103. function down (options) {
  104. throw new Error('Not implemented.')
  105. }
  106. export {
  107. up,
  108. down
  109. }