diff options
Diffstat (limited to 'server/helpers')
-rw-r--r-- | server/helpers/activitypub.ts | 12 | ||||
-rw-r--r-- | server/helpers/webtorrent.ts | 57 |
2 files changed, 35 insertions, 34 deletions
diff --git a/server/helpers/activitypub.ts b/server/helpers/activitypub.ts index 1188d6cf9..02a9d4026 100644 --- a/server/helpers/activitypub.ts +++ b/server/helpers/activitypub.ts | |||
@@ -1,13 +1,13 @@ | |||
1 | import * as Bluebird from 'bluebird' | 1 | import * as Bluebird from 'bluebird' |
2 | import { URL } from 'url' | ||
2 | import validator from 'validator' | 3 | import validator from 'validator' |
4 | import { ContextType } from '@shared/models/activitypub/context' | ||
3 | import { ResultList } from '../../shared/models' | 5 | import { ResultList } from '../../shared/models' |
4 | import { Activity } from '../../shared/models/activitypub' | 6 | import { Activity } from '../../shared/models/activitypub' |
5 | import { ACTIVITY_PUB, REMOTE_SCHEME } from '../initializers/constants' | 7 | import { ACTIVITY_PUB, REMOTE_SCHEME } from '../initializers/constants' |
6 | import { signJsonLDObject } from './peertube-crypto' | 8 | import { MActor, MVideoWithHost } from '../types/models' |
7 | import { pageToStartAndCount } from './core-utils' | 9 | import { pageToStartAndCount } from './core-utils' |
8 | import { URL } from 'url' | 10 | import { signJsonLDObject } from './peertube-crypto' |
9 | import { MActor, MVideoAccountLight } from '../types/models' | ||
10 | import { ContextType } from '@shared/models/activitypub/context' | ||
11 | 11 | ||
12 | function getContextData (type: ContextType) { | 12 | function getContextData (type: ContextType) { |
13 | const context: any[] = [ | 13 | const context: any[] = [ |
@@ -201,8 +201,8 @@ function checkUrlsSameHost (url1: string, url2: string) { | |||
201 | return idHost && actorHost && idHost.toLowerCase() === actorHost.toLowerCase() | 201 | return idHost && actorHost && idHost.toLowerCase() === actorHost.toLowerCase() |
202 | } | 202 | } |
203 | 203 | ||
204 | function buildRemoteVideoBaseUrl (video: MVideoAccountLight, path: string) { | 204 | function buildRemoteVideoBaseUrl (video: MVideoWithHost, path: string) { |
205 | const host = video.VideoChannel.Account.Actor.Server.host | 205 | const host = video.VideoChannel.Actor.Server.host |
206 | 206 | ||
207 | return REMOTE_SCHEME.HTTP + '://' + host + path | 207 | return REMOTE_SCHEME.HTTP + '://' + host + path |
208 | } | 208 | } |
diff --git a/server/helpers/webtorrent.ts b/server/helpers/webtorrent.ts index 9c5df2083..73418aa0a 100644 --- a/server/helpers/webtorrent.ts +++ b/server/helpers/webtorrent.ts | |||
@@ -1,20 +1,19 @@ | |||
1 | import { logger } from './logger' | ||
2 | import { generateVideoImportTmpPath } from './utils' | ||
3 | import * as WebTorrent from 'webtorrent' | ||
4 | import { createWriteStream, ensureDir, remove, writeFile } from 'fs-extra' | ||
5 | import { CONFIG } from '../initializers/config' | ||
6 | import { dirname, join } from 'path' | ||
7 | import * as createTorrent from 'create-torrent' | 1 | import * as createTorrent from 'create-torrent' |
8 | import { promisify2 } from './core-utils' | 2 | import { createWriteStream, ensureDir, remove, writeFile } from 'fs-extra' |
9 | import { MVideo } from '@server/types/models/video/video' | ||
10 | import { MVideoFile, MVideoFileRedundanciesOpt } from '@server/types/models/video/video-file' | ||
11 | import { isStreamingPlaylist, MStreamingPlaylistVideo } from '@server/types/models/video/video-streaming-playlist' | ||
12 | import { WEBSERVER } from '@server/initializers/constants' | ||
13 | import * as parseTorrent from 'parse-torrent' | ||
14 | import * as magnetUtil from 'magnet-uri' | 3 | import * as magnetUtil from 'magnet-uri' |
4 | import * as parseTorrent from 'parse-torrent' | ||
5 | import { dirname, join } from 'path' | ||
6 | import * as WebTorrent from 'webtorrent' | ||
15 | import { isArray } from '@server/helpers/custom-validators/misc' | 7 | import { isArray } from '@server/helpers/custom-validators/misc' |
16 | import { getTorrentFileName, getVideoFilePath } from '@server/lib/video-paths' | 8 | import { WEBSERVER } from '@server/initializers/constants' |
17 | import { extractVideo } from '@server/helpers/video' | 9 | import { generateTorrentFileName, getVideoFilePath } from '@server/lib/video-paths' |
10 | import { MVideo, MVideoWithHost } from '@server/types/models/video/video' | ||
11 | import { MVideoFile, MVideoFileRedundanciesOpt } from '@server/types/models/video/video-file' | ||
12 | import { MStreamingPlaylistVideo } from '@server/types/models/video/video-streaming-playlist' | ||
13 | import { CONFIG } from '../initializers/config' | ||
14 | import { promisify2 } from './core-utils' | ||
15 | import { logger } from './logger' | ||
16 | import { generateVideoImportTmpPath } from './utils' | ||
18 | 17 | ||
19 | const createTorrentPromise = promisify2<string, any, any>(createTorrent) | 18 | const createTorrentPromise = promisify2<string, any, any>(createTorrent) |
20 | 19 | ||
@@ -78,10 +77,12 @@ async function downloadWebTorrentVideo (target: { magnetUri: string, torrentName | |||
78 | }) | 77 | }) |
79 | } | 78 | } |
80 | 79 | ||
81 | async function createTorrentAndSetInfoHash (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) { | 80 | // FIXME: refactor/merge videoOrPlaylist and video arguments |
82 | const video = extractVideo(videoOrPlaylist) | 81 | async function createTorrentAndSetInfoHash ( |
83 | const { baseUrlHttp } = video.getBaseUrls() | 82 | videoOrPlaylist: MVideo | MStreamingPlaylistVideo, |
84 | 83 | video: MVideoWithHost, | |
84 | videoFile: MVideoFile | ||
85 | ) { | ||
85 | const options = { | 86 | const options = { |
86 | // Keep the extname, it's used by the client to stream the file inside a web browser | 87 | // Keep the extname, it's used by the client to stream the file inside a web browser |
87 | name: `${video.name} ${videoFile.resolution}p${videoFile.extname}`, | 88 | name: `${video.name} ${videoFile.resolution}p${videoFile.extname}`, |
@@ -90,33 +91,33 @@ async function createTorrentAndSetInfoHash (videoOrPlaylist: MVideo | MStreaming | |||
90 | [ WEBSERVER.WS + '://' + WEBSERVER.HOSTNAME + ':' + WEBSERVER.PORT + '/tracker/socket' ], | 91 | [ WEBSERVER.WS + '://' + WEBSERVER.HOSTNAME + ':' + WEBSERVER.PORT + '/tracker/socket' ], |
91 | [ WEBSERVER.URL + '/tracker/announce' ] | 92 | [ WEBSERVER.URL + '/tracker/announce' ] |
92 | ], | 93 | ], |
93 | urlList: [ videoOrPlaylist.getVideoFileUrl(videoFile, baseUrlHttp) ] | 94 | urlList: [ videoFile.getFileUrl(video) ] |
94 | } | 95 | } |
95 | 96 | ||
96 | const torrent = await createTorrentPromise(getVideoFilePath(videoOrPlaylist, videoFile), options) | 97 | const torrent = await createTorrentPromise(getVideoFilePath(videoOrPlaylist, videoFile), options) |
97 | 98 | ||
98 | const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, getTorrentFileName(videoOrPlaylist, videoFile)) | 99 | const torrentFilename = generateTorrentFileName(videoOrPlaylist, videoFile.resolution) |
99 | logger.info('Creating torrent %s.', filePath) | 100 | const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, torrentFilename) |
101 | logger.info('Creating torrent %s.', torrentPath) | ||
100 | 102 | ||
101 | await writeFile(filePath, torrent) | 103 | await writeFile(torrentPath, torrent) |
102 | 104 | ||
103 | const parsedTorrent = parseTorrent(torrent) | 105 | const parsedTorrent = parseTorrent(torrent) |
104 | videoFile.infoHash = parsedTorrent.infoHash | 106 | videoFile.infoHash = parsedTorrent.infoHash |
107 | videoFile.torrentFilename = torrentFilename | ||
105 | } | 108 | } |
106 | 109 | ||
110 | // FIXME: merge/refactor videoOrPlaylist and video arguments | ||
107 | function generateMagnetUri ( | 111 | function generateMagnetUri ( |
108 | videoOrPlaylist: MVideo | MStreamingPlaylistVideo, | 112 | videoOrPlaylist: MVideo | MStreamingPlaylistVideo, |
113 | video: MVideoWithHost, | ||
109 | videoFile: MVideoFileRedundanciesOpt, | 114 | videoFile: MVideoFileRedundanciesOpt, |
110 | baseUrlHttp: string, | 115 | baseUrlHttp: string, |
111 | baseUrlWs: string | 116 | baseUrlWs: string |
112 | ) { | 117 | ) { |
113 | const video = isStreamingPlaylist(videoOrPlaylist) | 118 | const xs = videoFile.getTorrentUrl() |
114 | ? videoOrPlaylist.Video | ||
115 | : videoOrPlaylist | ||
116 | |||
117 | const xs = videoOrPlaylist.getTorrentUrl(videoFile, baseUrlHttp) | ||
118 | const announce = videoOrPlaylist.getTrackerUrls(baseUrlHttp, baseUrlWs) | 119 | const announce = videoOrPlaylist.getTrackerUrls(baseUrlHttp, baseUrlWs) |
119 | let urlList = [ videoOrPlaylist.getVideoFileUrl(videoFile, baseUrlHttp) ] | 120 | let urlList = [ videoFile.getFileUrl(video) ] |
120 | 121 | ||
121 | const redundancies = videoFile.RedundancyVideos | 122 | const redundancies = videoFile.RedundancyVideos |
122 | if (isArray(redundancies)) urlList = urlList.concat(redundancies.map(r => r.fileUrl)) | 123 | if (isArray(redundancies)) urlList = urlList.concat(redundancies.map(r => r.fileUrl)) |