aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-11-15 15:06:03 +0100
committerChocobozzz <me@florianbigard.com>2019-11-25 10:59:43 +0100
commitd7a25329f9e607894d29ab342b9cb66638b56dc0 (patch)
tree6cd6bc4f2689f78944238b313c93427423a932ac /server/controllers
parent14981d7331da3f63fe6cfaf020ccb7c910006eaf (diff)
downloadPeerTube-d7a25329f9e607894d29ab342b9cb66638b56dc0.tar.gz
PeerTube-d7a25329f9e607894d29ab342b9cb66638b56dc0.tar.zst
PeerTube-d7a25329f9e607894d29ab342b9cb66638b56dc0.zip
Add ability to disable webtorrent
In favour of HLS
Diffstat (limited to 'server/controllers')
-rw-r--r--server/controllers/api/config.ts6
-rw-r--r--server/controllers/api/videos/index.ts12
-rw-r--r--server/controllers/static.ts61
3 files changed, 66 insertions, 13 deletions
diff --git a/server/controllers/api/config.ts b/server/controllers/api/config.ts
index 113c1e9db..70e8aa970 100644
--- a/server/controllers/api/config.ts
+++ b/server/controllers/api/config.ts
@@ -95,6 +95,9 @@ async function getConfig (req: express.Request, res: express.Response) {
95 hls: { 95 hls: {
96 enabled: CONFIG.TRANSCODING.HLS.ENABLED 96 enabled: CONFIG.TRANSCODING.HLS.ENABLED
97 }, 97 },
98 webtorrent: {
99 enabled: CONFIG.TRANSCODING.WEBTORRENT.ENABLED
100 },
98 enabledResolutions: getEnabledResolutions() 101 enabledResolutions: getEnabledResolutions()
99 }, 102 },
100 import: { 103 import: {
@@ -304,6 +307,9 @@ function customConfig (): CustomConfig {
304 '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ], 307 '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ],
305 '2160p': CONFIG.TRANSCODING.RESOLUTIONS[ '2160p' ] 308 '2160p': CONFIG.TRANSCODING.RESOLUTIONS[ '2160p' ]
306 }, 309 },
310 webtorrent: {
311 enabled: CONFIG.TRANSCODING.WEBTORRENT.ENABLED
312 },
307 hls: { 313 hls: {
308 enabled: CONFIG.TRANSCODING.HLS.ENABLED 314 enabled: CONFIG.TRANSCODING.HLS.ENABLED
309 } 315 }
diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts
index 0d1fbc8f4..78948ff24 100644
--- a/server/controllers/api/videos/index.ts
+++ b/server/controllers/api/videos/index.ts
@@ -64,6 +64,8 @@ import { ThumbnailType } from '../../../../shared/models/videos/thumbnail.type'
64import { VideoTranscodingPayload } from '../../../lib/job-queue/handlers/video-transcoding' 64import { VideoTranscodingPayload } from '../../../lib/job-queue/handlers/video-transcoding'
65import { Hooks } from '../../../lib/plugins/hooks' 65import { Hooks } from '../../../lib/plugins/hooks'
66import { MVideoDetails, MVideoFullLight } from '@server/typings/models' 66import { MVideoDetails, MVideoFullLight } from '@server/typings/models'
67import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
68import { getVideoFilename, getVideoFilePath } from '@server/lib/video-paths'
67 69
68const auditLogger = auditLoggerFactory('videos') 70const auditLogger = auditLoggerFactory('videos')
69const videosRouter = express.Router() 71const videosRouter = express.Router()
@@ -203,7 +205,8 @@ async function addVideo (req: express.Request, res: express.Response) {
203 205
204 const videoFile = new VideoFileModel({ 206 const videoFile = new VideoFileModel({
205 extname: extname(videoPhysicalFile.filename), 207 extname: extname(videoPhysicalFile.filename),
206 size: videoPhysicalFile.size 208 size: videoPhysicalFile.size,
209 videoStreamingPlaylistId: null
207 }) 210 })
208 211
209 if (videoFile.isAudio()) { 212 if (videoFile.isAudio()) {
@@ -214,11 +217,10 @@ async function addVideo (req: express.Request, res: express.Response) {
214 } 217 }
215 218
216 // Move physical file 219 // Move physical file
217 const videoDir = CONFIG.STORAGE.VIDEOS_DIR 220 const destination = getVideoFilePath(video, videoFile)
218 const destination = join(videoDir, video.getVideoFilename(videoFile))
219 await move(videoPhysicalFile.path, destination) 221 await move(videoPhysicalFile.path, destination)
220 // This is important in case if there is another attempt in the retry process 222 // This is important in case if there is another attempt in the retry process
221 videoPhysicalFile.filename = video.getVideoFilename(videoFile) 223 videoPhysicalFile.filename = getVideoFilePath(video, videoFile)
222 videoPhysicalFile.path = destination 224 videoPhysicalFile.path = destination
223 225
224 // Process thumbnail or create it from the video 226 // Process thumbnail or create it from the video
@@ -234,7 +236,7 @@ async function addVideo (req: express.Request, res: express.Response) {
234 : await generateVideoMiniature(video, videoFile, ThumbnailType.PREVIEW) 236 : await generateVideoMiniature(video, videoFile, ThumbnailType.PREVIEW)
235 237
236 // Create the torrent file 238 // Create the torrent file
237 await video.createTorrentAndSetInfoHash(videoFile) 239 await createTorrentAndSetInfoHash(video, videoFile)
238 240
239 const { videoCreated } = await sequelizeTypescript.transaction(async t => { 241 const { videoCreated } = await sequelizeTypescript.transaction(async t => {
240 const sequelizeOptions = { transaction: t } 242 const sequelizeOptions = { transaction: t }
diff --git a/server/controllers/static.ts b/server/controllers/static.ts
index 0f4772310..06123518f 100644
--- a/server/controllers/static.ts
+++ b/server/controllers/static.ts
@@ -19,6 +19,9 @@ import { join } from 'path'
19import { root } from '../helpers/core-utils' 19import { root } from '../helpers/core-utils'
20import { CONFIG } from '../initializers/config' 20import { CONFIG } from '../initializers/config'
21import { getPreview, getVideoCaption } from './lazy-static' 21import { getPreview, getVideoCaption } from './lazy-static'
22import { VideoStreamingPlaylistType } from '@shared/models/videos/video-streaming-playlist.type'
23import { MVideoFile, MVideoFullLight } from '@server/typings/models'
24import { getTorrentFilePath, getVideoFilePath } from '@server/lib/video-paths'
22 25
23const staticRouter = express.Router() 26const staticRouter = express.Router()
24 27
@@ -39,6 +42,11 @@ staticRouter.use(
39 asyncMiddleware(videosGetValidator), 42 asyncMiddleware(videosGetValidator),
40 asyncMiddleware(downloadTorrent) 43 asyncMiddleware(downloadTorrent)
41) 44)
45staticRouter.use(
46 STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+)-hls.torrent',
47 asyncMiddleware(videosGetValidator),
48 asyncMiddleware(downloadHLSVideoFileTorrent)
49)
42 50
43// Videos path for webseeding 51// Videos path for webseeding
44staticRouter.use( 52staticRouter.use(
@@ -58,6 +66,12 @@ staticRouter.use(
58 asyncMiddleware(downloadVideoFile) 66 asyncMiddleware(downloadVideoFile)
59) 67)
60 68
69staticRouter.use(
70 STATIC_DOWNLOAD_PATHS.HLS_VIDEOS + ':id-:resolution([0-9]+).:extension',
71 asyncMiddleware(videosGetValidator),
72 asyncMiddleware(downloadHLSVideoFile)
73)
74
61// HLS 75// HLS
62staticRouter.use( 76staticRouter.use(
63 STATIC_PATHS.STREAMING_PLAYLISTS.HLS, 77 STATIC_PATHS.STREAMING_PLAYLISTS.HLS,
@@ -227,24 +241,55 @@ async function generateNodeinfo (req: express.Request, res: express.Response) {
227} 241}
228 242
229async function downloadTorrent (req: express.Request, res: express.Response) { 243async function downloadTorrent (req: express.Request, res: express.Response) {
230 const { video, videoFile } = getVideoAndFile(req, res) 244 const video = res.locals.videoAll
245
246 const videoFile = getVideoFile(req, video.VideoFiles)
247 if (!videoFile) return res.status(404).end()
248
249 return res.download(getTorrentFilePath(video, videoFile), `${video.name}-${videoFile.resolution}p.torrent`)
250}
251
252async function downloadHLSVideoFileTorrent (req: express.Request, res: express.Response) {
253 const video = res.locals.videoAll
254
255 const playlist = getHLSPlaylist(video)
256 if (!playlist) return res.status(404).end
257
258 const videoFile = getVideoFile(req, playlist.VideoFiles)
231 if (!videoFile) return res.status(404).end() 259 if (!videoFile) return res.status(404).end()
232 260
233 return res.download(video.getTorrentFilePath(videoFile), `${video.name}-${videoFile.resolution}p.torrent`) 261 return res.download(getTorrentFilePath(playlist, videoFile), `${video.name}-${videoFile.resolution}p-hls.torrent`)
234} 262}
235 263
236async function downloadVideoFile (req: express.Request, res: express.Response) { 264async function downloadVideoFile (req: express.Request, res: express.Response) {
237 const { video, videoFile } = getVideoAndFile(req, res) 265 const video = res.locals.videoAll
266
267 const videoFile = getVideoFile(req, video.VideoFiles)
238 if (!videoFile) return res.status(404).end() 268 if (!videoFile) return res.status(404).end()
239 269
240 return res.download(video.getVideoFilePath(videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`) 270 return res.download(getVideoFilePath(video, videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`)
241} 271}
242 272
243function getVideoAndFile (req: express.Request, res: express.Response) { 273async function downloadHLSVideoFile (req: express.Request, res: express.Response) {
244 const resolution = parseInt(req.params.resolution, 10)
245 const video = res.locals.videoAll 274 const video = res.locals.videoAll
275 const playlist = getHLSPlaylist(video)
276 if (!playlist) return res.status(404).end
277
278 const videoFile = getVideoFile(req, playlist.VideoFiles)
279 if (!videoFile) return res.status(404).end()
280
281 const filename = `${video.name}-${videoFile.resolution}p-${playlist.getStringType()}${videoFile.extname}`
282 return res.download(getVideoFilePath(playlist, videoFile), filename)
283}
284
285function getVideoFile (req: express.Request, files: MVideoFile[]) {
286 const resolution = parseInt(req.params.resolution, 10)
287 return files.find(f => f.resolution === resolution)
288}
246 289
247 const videoFile = video.VideoFiles.find(f => f.resolution === resolution) 290function getHLSPlaylist (video: MVideoFullLight) {
291 const playlist = video.VideoStreamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
292 if (!playlist) return undefined
248 293
249 return { video, videoFile } 294 return Object.assign(playlist, { Video: video })
250} 295}