peertube-2.1.ts 2.8 KB

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