]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/webtorrent.ts
Merge branch 'release/4.0.0' into develop
[github/Chocobozzz/PeerTube.git] / server / helpers / webtorrent.ts
index 5e1ea61983c2197ff8ad59eeba01148ff645b819..68d532c48995a2f5efde250c233aec100f800594 100644 (file)
@@ -13,6 +13,7 @@ import { VideoPathManager } from '@server/lib/video-path-manager'
 import { MVideo } from '@server/types/models/video/video'
 import { MVideoFile, MVideoFileRedundanciesOpt } from '@server/types/models/video/video-file'
 import { MStreamingPlaylistVideo } from '@server/types/models/video/video-streaming-playlist'
+import { sha1 } from '@shared/extra-utils'
 import { CONFIG } from '../initializers/config'
 import { promisify2 } from './core-utils'
 import { logger } from './logger'
@@ -94,13 +95,13 @@ function createTorrentAndSetInfoHash (videoOrPlaylist: MVideo | MStreamingPlayli
 
   const options = {
     // Keep the extname, it's used by the client to stream the file inside a web browser
-    name: `${video.name} ${videoFile.resolution}p${videoFile.extname}`,
+    name: buildInfoName(video, videoFile),
     createdBy: 'PeerTube',
     announceList: buildAnnounceList(),
     urlList: buildUrlList(video, videoFile)
   }
 
-  return VideoPathManager.Instance.makeAvailableVideoFile(videoOrPlaylist, videoFile, async videoPath => {
+  return VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(videoOrPlaylist), async videoPath => {
     const torrentContent = await createTorrentPromise(videoPath, options)
 
     const torrentFilename = generateTorrentFileName(videoOrPlaylist, videoFile.resolution)
@@ -120,7 +121,7 @@ function createTorrentAndSetInfoHash (videoOrPlaylist: MVideo | MStreamingPlayli
   })
 }
 
-async function updateTorrentUrls (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) {
+async function updateTorrentMetadata (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) {
   const video = extractVideo(videoOrPlaylist)
 
   const oldTorrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename)
@@ -133,15 +134,19 @@ async function updateTorrentUrls (videoOrPlaylist: MVideo | MStreamingPlaylistVi
 
   decoded['url-list'] = buildUrlList(video, videoFile)
 
+  decoded.info.name = buildInfoName(video, videoFile)
+  decoded['creation date'] = Math.ceil(Date.now() / 1000)
+
   const newTorrentFilename = generateTorrentFileName(videoOrPlaylist, videoFile.resolution)
   const newTorrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, newTorrentFilename)
 
-  logger.info('Updating torrent URLs %s -> %s.', oldTorrentPath, newTorrentPath)
+  logger.info('Updating torrent metadata %s -> %s.', oldTorrentPath, newTorrentPath)
 
   await writeFile(newTorrentPath, encode(decoded))
   await remove(join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename))
 
   videoFile.torrentFilename = newTorrentFilename
+  videoFile.infoHash = sha1(encode(decoded.info))
 }
 
 function generateMagnetUri (
@@ -171,7 +176,7 @@ function generateMagnetUri (
 
 export {
   createTorrentPromise,
-  updateTorrentUrls,
+  updateTorrentMetadata,
   createTorrentAndSetInfoHash,
   generateMagnetUri,
   downloadWebTorrentVideo
@@ -226,3 +231,7 @@ function buildAnnounceList () {
 function buildUrlList (video: MVideo, videoFile: MVideoFile) {
   return [ videoFile.getFileUrl(video) ]
 }
+
+function buildInfoName (video: MVideo, videoFile: MVideoFile) {
+  return `${video.name} ${videoFile.resolution}p${videoFile.extname}`
+}