aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers')
-rw-r--r--server/helpers/activitypub.ts12
-rw-r--r--server/helpers/webtorrent.ts57
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 @@
1import * as Bluebird from 'bluebird' 1import * as Bluebird from 'bluebird'
2import { URL } from 'url'
2import validator from 'validator' 3import validator from 'validator'
4import { ContextType } from '@shared/models/activitypub/context'
3import { ResultList } from '../../shared/models' 5import { ResultList } from '../../shared/models'
4import { Activity } from '../../shared/models/activitypub' 6import { Activity } from '../../shared/models/activitypub'
5import { ACTIVITY_PUB, REMOTE_SCHEME } from '../initializers/constants' 7import { ACTIVITY_PUB, REMOTE_SCHEME } from '../initializers/constants'
6import { signJsonLDObject } from './peertube-crypto' 8import { MActor, MVideoWithHost } from '../types/models'
7import { pageToStartAndCount } from './core-utils' 9import { pageToStartAndCount } from './core-utils'
8import { URL } from 'url' 10import { signJsonLDObject } from './peertube-crypto'
9import { MActor, MVideoAccountLight } from '../types/models'
10import { ContextType } from '@shared/models/activitypub/context'
11 11
12function getContextData (type: ContextType) { 12function 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
204function buildRemoteVideoBaseUrl (video: MVideoAccountLight, path: string) { 204function 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 @@
1import { logger } from './logger'
2import { generateVideoImportTmpPath } from './utils'
3import * as WebTorrent from 'webtorrent'
4import { createWriteStream, ensureDir, remove, writeFile } from 'fs-extra'
5import { CONFIG } from '../initializers/config'
6import { dirname, join } from 'path'
7import * as createTorrent from 'create-torrent' 1import * as createTorrent from 'create-torrent'
8import { promisify2 } from './core-utils' 2import { createWriteStream, ensureDir, remove, writeFile } from 'fs-extra'
9import { MVideo } from '@server/types/models/video/video'
10import { MVideoFile, MVideoFileRedundanciesOpt } from '@server/types/models/video/video-file'
11import { isStreamingPlaylist, MStreamingPlaylistVideo } from '@server/types/models/video/video-streaming-playlist'
12import { WEBSERVER } from '@server/initializers/constants'
13import * as parseTorrent from 'parse-torrent'
14import * as magnetUtil from 'magnet-uri' 3import * as magnetUtil from 'magnet-uri'
4import * as parseTorrent from 'parse-torrent'
5import { dirname, join } from 'path'
6import * as WebTorrent from 'webtorrent'
15import { isArray } from '@server/helpers/custom-validators/misc' 7import { isArray } from '@server/helpers/custom-validators/misc'
16import { getTorrentFileName, getVideoFilePath } from '@server/lib/video-paths' 8import { WEBSERVER } from '@server/initializers/constants'
17import { extractVideo } from '@server/helpers/video' 9import { generateTorrentFileName, getVideoFilePath } from '@server/lib/video-paths'
10import { MVideo, MVideoWithHost } from '@server/types/models/video/video'
11import { MVideoFile, MVideoFileRedundanciesOpt } from '@server/types/models/video/video-file'
12import { MStreamingPlaylistVideo } from '@server/types/models/video/video-streaming-playlist'
13import { CONFIG } from '../initializers/config'
14import { promisify2 } from './core-utils'
15import { logger } from './logger'
16import { generateVideoImportTmpPath } from './utils'
18 17
19const createTorrentPromise = promisify2<string, any, any>(createTorrent) 18const 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
81async function createTorrentAndSetInfoHash (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) { 80// FIXME: refactor/merge videoOrPlaylist and video arguments
82 const video = extractVideo(videoOrPlaylist) 81async 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
107function generateMagnetUri ( 111function 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))