peertube-2.1.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { registerTSPaths } from '../../server/helpers/register-ts-paths'
  2. registerTSPaths()
  3. import { initDatabaseModels, sequelizeTypescript } from '../../server/initializers/database'
  4. import * as Sequelize from 'sequelize'
  5. import { join } from 'path'
  6. import { HLS_STREAMING_PLAYLIST_DIRECTORY, STATIC_PATHS, WEBSERVER } from '@server/initializers/constants'
  7. import { pathExists, stat, writeFile } from 'fs-extra'
  8. import { createTorrentPromise } from '@server/helpers/webtorrent'
  9. import { CONFIG } from '@server/initializers/config'
  10. import * as parseTorrent from 'parse-torrent'
  11. import { logger } from '@server/helpers/logger'
  12. run()
  13. .then(() => process.exit(0))
  14. .catch(err => {
  15. console.error(err)
  16. process.exit(-1)
  17. })
  18. async function run () {
  19. logger.info('Creating torrents and updating database for HSL files.')
  20. await initDatabaseModels(true)
  21. const query = 'select "videoFile".id as id, "videoFile".resolution as resolution, "video".uuid as uuid from "videoFile" ' +
  22. 'inner join "videoStreamingPlaylist" ON "videoStreamingPlaylist".id = "videoFile"."videoStreamingPlaylistId" ' +
  23. 'inner join video ON video.id = "videoStreamingPlaylist"."videoId" ' +
  24. 'WHERE video.remote IS FALSE'
  25. const options = {
  26. type: Sequelize.QueryTypes.SELECT
  27. }
  28. const res = await sequelizeTypescript.query(query, options)
  29. for (const row of res) {
  30. const videoFilename = `${row['uuid']}-${row['resolution']}-fragmented.mp4`
  31. const videoFilePath = join(HLS_STREAMING_PLAYLIST_DIRECTORY, row['uuid'], videoFilename)
  32. logger.info('Processing %s.', videoFilePath)
  33. if (!await pathExists(videoFilePath)) {
  34. console.warn('Cannot generate torrent of %s: file does not exist.', videoFilePath)
  35. continue
  36. }
  37. const createTorrentOptions = {
  38. // Keep the extname, it's used by the client to stream the file inside a web browser
  39. name: `video ${row['uuid']}`,
  40. createdBy: 'PeerTube',
  41. announceList: [
  42. [ WEBSERVER.WS + '://' + WEBSERVER.HOSTNAME + ':' + WEBSERVER.PORT + '/tracker/socket' ],
  43. [ WEBSERVER.URL + '/tracker/announce' ]
  44. ],
  45. urlList: [ WEBSERVER.URL + join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, row['uuid'], videoFilename) ]
  46. }
  47. const torrent = await createTorrentPromise(videoFilePath, createTorrentOptions)
  48. const torrentName = `${row['uuid']}-${row['resolution']}-hls.torrent`
  49. const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, torrentName)
  50. await writeFile(filePath, torrent)
  51. const parsedTorrent = parseTorrent(torrent)
  52. const infoHash = parsedTorrent.infoHash
  53. const stats = await stat(videoFilePath)
  54. const size = stats.size
  55. const queryUpdate = 'UPDATE "videoFile" SET "infoHash" = ?, "size" = ? WHERE id = ?'
  56. const options = {
  57. type: Sequelize.QueryTypes.UPDATE,
  58. replacements: [ infoHash, size, row['id'] ]
  59. }
  60. await sequelizeTypescript.query(queryUpdate, options)
  61. }
  62. }