]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/webtorrent.ts
Set ACL when uploading objects
[github/Chocobozzz/PeerTube.git] / server / helpers / webtorrent.ts
CommitLineData
1f6125be 1import * as bencode from 'bencode'
30ff39e7 2import * as createTorrent from 'create-torrent'
1f6125be 3import { createWriteStream, ensureDir, readFile, remove, writeFile } from 'fs-extra'
d7a25329 4import * as magnetUtil from 'magnet-uri'
90a8bd30
C
5import * as parseTorrent from 'parse-torrent'
6import { dirname, join } from 'path'
7import * as WebTorrent from 'webtorrent'
d7a25329 8import { isArray } from '@server/helpers/custom-validators/misc'
90a8bd30 9import { WEBSERVER } from '@server/initializers/constants'
0305db28
JB
10import { generateTorrentFileName } from '@server/lib/paths'
11import { VideoPathManager } from '@server/lib/video-path-manager'
8efc27bf 12import { MVideo } from '@server/types/models/video/video'
90a8bd30
C
13import { MVideoFile, MVideoFileRedundanciesOpt } from '@server/types/models/video/video-file'
14import { MStreamingPlaylistVideo } from '@server/types/models/video/video-streaming-playlist'
15import { CONFIG } from '../initializers/config'
16import { promisify2 } from './core-utils'
17import { logger } from './logger'
18import { generateVideoImportTmpPath } from './utils'
8efc27bf 19import { extractVideo } from './video'
d7a25329
C
20
21const createTorrentPromise = promisify2<string, any, any>(createTorrent)
ce33919c 22
cf9166cf 23async function downloadWebTorrentVideo (target: { magnetUri: string, torrentName?: string }, timeout: number) {
990b6a0b 24 const id = target.magnetUri || target.torrentName
c48e82b5 25 let timer
ce33919c 26
6040f87d 27 const path = generateVideoImportTmpPath(id)
990b6a0b 28 logger.info('Importing torrent video %s', id)
ce33919c 29
6040f87d 30 const directoryPath = join(CONFIG.STORAGE.TMP_DIR, 'webtorrent')
a71de50b
C
31 await ensureDir(directoryPath)
32
ce33919c
C
33 return new Promise<string>((res, rej) => {
34 const webtorrent = new WebTorrent()
c48e82b5 35 let file: WebTorrent.TorrentFile
ce33919c 36
990b6a0b 37 const torrentId = target.magnetUri || join(CONFIG.STORAGE.TORRENTS_DIR, target.torrentName)
541006e3 38
a71de50b 39 const options = { path: directoryPath }
541006e3 40 const torrent = webtorrent.add(torrentId, options, torrent => {
c48e82b5
C
41 if (torrent.files.length !== 1) {
42 if (timer) clearTimeout(timer)
ce33919c 43
a1587156 44 for (const file of torrent.files) {
e37c85e9
C
45 deleteDownloadedFile({ directoryPath, filepath: file.path })
46 }
47
48 return safeWebtorrentDestroy(webtorrent, torrentId, undefined, target.torrentName)
dae4a1c0 49 .then(() => rej(new Error('Cannot import torrent ' + torrentId + ': there are multiple files in it')))
c48e82b5
C
50 }
51
a1587156 52 file = torrent.files[0]
a71de50b
C
53
54 // FIXME: avoid creating another stream when https://github.com/webtorrent/webtorrent/issues/1517 is fixed
55 const writeStream = createWriteStream(path)
56 writeStream.on('finish', () => {
57 if (timer) clearTimeout(timer)
58
a1587156 59 safeWebtorrentDestroy(webtorrent, torrentId, { directoryPath, filepath: file.path }, target.torrentName)
a71de50b 60 .then(() => res(path))
a1587156 61 .catch(err => logger.error('Cannot destroy webtorrent.', { err }))
69fa54a0 62 })
a71de50b
C
63
64 file.createReadStream().pipe(writeStream)
3e17515e 65 })
ce33919c
C
66
67 torrent.on('error', err => rej(err))
c48e82b5 68
a1587156
C
69 timer = setTimeout(() => {
70 const err = new Error('Webtorrent download timeout.')
71
72 safeWebtorrentDestroy(webtorrent, torrentId, file ? { directoryPath, filepath: file.path } : undefined, target.torrentName)
73 .then(() => rej(err))
74 .catch(destroyErr => {
75 logger.error('Cannot destroy webtorrent.', { err: destroyErr })
76 rej(err)
77 })
78
cf9166cf 79 }, timeout)
ce33919c
C
80 })
81}
82
1f6125be 83function createTorrentAndSetInfoHash (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) {
8efc27bf
C
84 const video = extractVideo(videoOrPlaylist)
85
d7a25329
C
86 const options = {
87 // Keep the extname, it's used by the client to stream the file inside a web browser
88 name: `${video.name} ${videoFile.resolution}p${videoFile.extname}`,
89 createdBy: 'PeerTube',
1f6125be
C
90 announceList: buildAnnounceList(),
91 urlList: buildUrlList(video, videoFile)
d7a25329
C
92 }
93
0305db28 94 return VideoPathManager.Instance.makeAvailableVideoFile(videoOrPlaylist, videoFile, async videoPath => {
1f6125be 95 const torrentContent = await createTorrentPromise(videoPath, options)
d7a25329 96
0305db28
JB
97 const torrentFilename = generateTorrentFileName(videoOrPlaylist, videoFile.resolution)
98 const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, torrentFilename)
99 logger.info('Creating torrent %s.', torrentPath)
d7a25329 100
1f6125be 101 await writeFile(torrentPath, torrentContent)
d7a25329 102
0305db28
JB
103 // Remove old torrent file if it existed
104 if (videoFile.hasTorrent()) {
105 await remove(join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename))
106 }
764b1a14 107
1f6125be 108 const parsedTorrent = parseTorrent(torrentContent)
0305db28
JB
109 videoFile.infoHash = parsedTorrent.infoHash
110 videoFile.torrentFilename = torrentFilename
111 })
d7a25329
C
112}
113
1f6125be
C
114async function updateTorrentUrls (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) {
115 const video = extractVideo(videoOrPlaylist)
116
117 const oldTorrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename)
118
119 const torrentContent = await readFile(oldTorrentPath)
120 const decoded = bencode.decode(torrentContent)
121
122 decoded['announce-list'] = buildAnnounceList()
123 decoded.announce = decoded['announce-list'][0][0]
124
125 decoded['url-list'] = buildUrlList(video, videoFile)
126
127 const newTorrentFilename = generateTorrentFileName(videoOrPlaylist, videoFile.resolution)
128 const newTorrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, newTorrentFilename)
129
130 logger.info('Updating torrent URLs %s.', newTorrentPath)
131
132 await writeFile(newTorrentPath, bencode.encode(decoded))
133
134 // Remove old torrent file if it existed
135 if (videoFile.hasTorrent()) {
136 await remove(join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename))
137 }
138
139 videoFile.torrentFilename = newTorrentFilename
140}
141
d7a25329 142function generateMagnetUri (
8efc27bf 143 video: MVideo,
d7a25329 144 videoFile: MVideoFileRedundanciesOpt,
d9a2a031 145 trackerUrls: string[]
d7a25329 146) {
90a8bd30 147 const xs = videoFile.getTorrentUrl()
d9a2a031 148 const announce = trackerUrls
90a8bd30 149 let urlList = [ videoFile.getFileUrl(video) ]
d7a25329
C
150
151 const redundancies = videoFile.RedundancyVideos
152 if (isArray(redundancies)) urlList = urlList.concat(redundancies.map(r => r.fileUrl))
153
154 const magnetHash = {
155 xs,
156 announce,
157 urlList,
158 infoHash: videoFile.infoHash,
159 name: video.name
160 }
161
162 return magnetUtil.encode(magnetHash)
163}
30ff39e7 164
ce33919c
C
165// ---------------------------------------------------------------------------
166
167export {
d441f2ed 168 createTorrentPromise,
1f6125be 169 updateTorrentUrls,
d7a25329
C
170 createTorrentAndSetInfoHash,
171 generateMagnetUri,
ce33919c
C
172 downloadWebTorrentVideo
173}
c48e82b5
C
174
175// ---------------------------------------------------------------------------
176
d0b52b52
C
177function safeWebtorrentDestroy (
178 webtorrent: WebTorrent.Instance,
179 torrentId: string,
180 downloadedFile?: { directoryPath: string, filepath: string },
181 torrentName?: string
182) {
ba5a8d89 183 return new Promise<void>(res => {
c48e82b5
C
184 webtorrent.destroy(err => {
185 // Delete torrent file
186 if (torrentName) {
d0b52b52 187 logger.debug('Removing %s torrent after webtorrent download.', torrentId)
c48e82b5
C
188 remove(torrentId)
189 .catch(err => logger.error('Cannot remove torrent %s in webtorrent download.', torrentId, { err }))
190 }
191
192 // Delete downloaded file
e37c85e9 193 if (downloadedFile) deleteDownloadedFile(downloadedFile)
c48e82b5 194
e37c85e9 195 if (err) logger.warn('Cannot destroy webtorrent in timeout.', { err })
c48e82b5
C
196
197 return res()
198 })
199 })
200}
e37c85e9
C
201
202function deleteDownloadedFile (downloadedFile: { directoryPath: string, filepath: string }) {
203 // We want to delete the base directory
204 let pathToDelete = dirname(downloadedFile.filepath)
205 if (pathToDelete === '.') pathToDelete = downloadedFile.filepath
206
207 const toRemovePath = join(downloadedFile.directoryPath, pathToDelete)
208
209 logger.debug('Removing %s after webtorrent download.', toRemovePath)
210 remove(toRemovePath)
211 .catch(err => logger.error('Cannot remove torrent file %s in webtorrent download.', toRemovePath, { err }))
212}
1f6125be
C
213
214function buildAnnounceList () {
215 return [
216 [ WEBSERVER.WS + '://' + WEBSERVER.HOSTNAME + ':' + WEBSERVER.PORT + '/tracker/socket' ],
217 [ WEBSERVER.URL + '/tracker/announce' ]
218 ]
219}
220
221function buildUrlList (video: MVideo, videoFile: MVideoFile) {
222 return [ videoFile.getFileUrl(video) ]
223}