diff options
Diffstat (limited to 'server/helpers/webtorrent.ts')
-rw-r--r-- | server/helpers/webtorrent.ts | 69 |
1 files changed, 66 insertions, 3 deletions
diff --git a/server/helpers/webtorrent.ts b/server/helpers/webtorrent.ts index d2a22e8f0..f3e41f8d6 100644 --- a/server/helpers/webtorrent.ts +++ b/server/helpers/webtorrent.ts | |||
@@ -1,11 +1,22 @@ | |||
1 | import { logger } from './logger' | 1 | import { logger } from './logger' |
2 | import { generateVideoImportTmpPath } from './utils' | 2 | import { generateVideoImportTmpPath } from './utils' |
3 | import * as WebTorrent from 'webtorrent' | 3 | import * as WebTorrent from 'webtorrent' |
4 | import { createWriteStream, ensureDir, remove } from 'fs-extra' | 4 | import { createWriteStream, ensureDir, remove, writeFile } from 'fs-extra' |
5 | import { CONFIG } from '../initializers/config' | 5 | import { CONFIG } from '../initializers/config' |
6 | import { dirname, join } from 'path' | 6 | import { dirname, join } from 'path' |
7 | import * as createTorrent from 'create-torrent' | 7 | import * as createTorrent from 'create-torrent' |
8 | import { promisify2 } from './core-utils' | 8 | import { promisify2 } from './core-utils' |
9 | import { MVideo } from '@server/typings/models/video/video' | ||
10 | import { MVideoFile, MVideoFileRedundanciesOpt } from '@server/typings/models/video/video-file' | ||
11 | import { isStreamingPlaylist, MStreamingPlaylistVideo } from '@server/typings/models/video/video-streaming-playlist' | ||
12 | import { STATIC_PATHS, WEBSERVER } from '@server/initializers/constants' | ||
13 | import * as parseTorrent from 'parse-torrent' | ||
14 | import * as magnetUtil from 'magnet-uri' | ||
15 | import { isArray } from '@server/helpers/custom-validators/misc' | ||
16 | import { extractVideo } from '@server/lib/videos' | ||
17 | import { getTorrentFileName, getVideoFilename, getVideoFilePath } from '@server/lib/video-paths' | ||
18 | |||
19 | const createTorrentPromise = promisify2<string, any, any>(createTorrent) | ||
9 | 20 | ||
10 | async function downloadWebTorrentVideo (target: { magnetUri: string, torrentName?: string }, timeout: number) { | 21 | async function downloadWebTorrentVideo (target: { magnetUri: string, torrentName?: string }, timeout: number) { |
11 | const id = target.magnetUri || target.torrentName | 22 | const id = target.magnetUri || target.torrentName |
@@ -59,12 +70,64 @@ async function downloadWebTorrentVideo (target: { magnetUri: string, torrentName | |||
59 | }) | 70 | }) |
60 | } | 71 | } |
61 | 72 | ||
62 | const createTorrentPromise = promisify2<string, any, any>(createTorrent) | 73 | async function createTorrentAndSetInfoHash (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) { |
74 | const video = extractVideo(videoOrPlaylist) | ||
75 | |||
76 | const options = { | ||
77 | // Keep the extname, it's used by the client to stream the file inside a web browser | ||
78 | name: `${video.name} ${videoFile.resolution}p${videoFile.extname}`, | ||
79 | createdBy: 'PeerTube', | ||
80 | announceList: [ | ||
81 | [ WEBSERVER.WS + '://' + WEBSERVER.HOSTNAME + ':' + WEBSERVER.PORT + '/tracker/socket' ], | ||
82 | [ WEBSERVER.URL + '/tracker/announce' ] | ||
83 | ], | ||
84 | urlList: [ WEBSERVER.URL + STATIC_PATHS.WEBSEED + getVideoFilename(videoOrPlaylist, videoFile) ] | ||
85 | } | ||
86 | |||
87 | const torrent = await createTorrentPromise(getVideoFilePath(videoOrPlaylist, videoFile), options) | ||
88 | |||
89 | const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, getTorrentFileName(videoOrPlaylist, videoFile)) | ||
90 | logger.info('Creating torrent %s.', filePath) | ||
91 | |||
92 | await writeFile(filePath, torrent) | ||
93 | |||
94 | const parsedTorrent = parseTorrent(torrent) | ||
95 | videoFile.infoHash = parsedTorrent.infoHash | ||
96 | } | ||
97 | |||
98 | function generateMagnetUri ( | ||
99 | videoOrPlaylist: MVideo | MStreamingPlaylistVideo, | ||
100 | videoFile: MVideoFileRedundanciesOpt, | ||
101 | baseUrlHttp: string, | ||
102 | baseUrlWs: string | ||
103 | ) { | ||
104 | const video = isStreamingPlaylist(videoOrPlaylist) | ||
105 | ? videoOrPlaylist.Video | ||
106 | : videoOrPlaylist | ||
107 | |||
108 | const xs = videoOrPlaylist.getTorrentUrl(videoFile, baseUrlHttp) | ||
109 | const announce = videoOrPlaylist.getTrackerUrls(baseUrlHttp, baseUrlWs) | ||
110 | let urlList = [ videoOrPlaylist.getVideoFileUrl(videoFile, baseUrlHttp) ] | ||
111 | |||
112 | const redundancies = videoFile.RedundancyVideos | ||
113 | if (isArray(redundancies)) urlList = urlList.concat(redundancies.map(r => r.fileUrl)) | ||
114 | |||
115 | const magnetHash = { | ||
116 | xs, | ||
117 | announce, | ||
118 | urlList, | ||
119 | infoHash: videoFile.infoHash, | ||
120 | name: video.name | ||
121 | } | ||
122 | |||
123 | return magnetUtil.encode(magnetHash) | ||
124 | } | ||
63 | 125 | ||
64 | // --------------------------------------------------------------------------- | 126 | // --------------------------------------------------------------------------- |
65 | 127 | ||
66 | export { | 128 | export { |
67 | createTorrentPromise, | 129 | createTorrentAndSetInfoHash, |
130 | generateMagnetUri, | ||
68 | downloadWebTorrentVideo | 131 | downloadWebTorrentVideo |
69 | } | 132 | } |
70 | 133 | ||