From d7a25329f9e607894d29ab342b9cb66638b56dc0 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 15 Nov 2019 15:06:03 +0100 Subject: Add ability to disable webtorrent In favour of HLS --- server/controllers/api/config.ts | 6 ++++ server/controllers/api/videos/index.ts | 12 ++++--- server/controllers/static.ts | 61 +++++++++++++++++++++++++++++----- 3 files changed, 66 insertions(+), 13 deletions(-) (limited to 'server/controllers') 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) { hls: { enabled: CONFIG.TRANSCODING.HLS.ENABLED }, + webtorrent: { + enabled: CONFIG.TRANSCODING.WEBTORRENT.ENABLED + }, enabledResolutions: getEnabledResolutions() }, import: { @@ -304,6 +307,9 @@ function customConfig (): CustomConfig { '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ], '2160p': CONFIG.TRANSCODING.RESOLUTIONS[ '2160p' ] }, + webtorrent: { + enabled: CONFIG.TRANSCODING.WEBTORRENT.ENABLED + }, hls: { enabled: CONFIG.TRANSCODING.HLS.ENABLED } 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' import { VideoTranscodingPayload } from '../../../lib/job-queue/handlers/video-transcoding' import { Hooks } from '../../../lib/plugins/hooks' import { MVideoDetails, MVideoFullLight } from '@server/typings/models' +import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent' +import { getVideoFilename, getVideoFilePath } from '@server/lib/video-paths' const auditLogger = auditLoggerFactory('videos') const videosRouter = express.Router() @@ -203,7 +205,8 @@ async function addVideo (req: express.Request, res: express.Response) { const videoFile = new VideoFileModel({ extname: extname(videoPhysicalFile.filename), - size: videoPhysicalFile.size + size: videoPhysicalFile.size, + videoStreamingPlaylistId: null }) if (videoFile.isAudio()) { @@ -214,11 +217,10 @@ async function addVideo (req: express.Request, res: express.Response) { } // Move physical file - const videoDir = CONFIG.STORAGE.VIDEOS_DIR - const destination = join(videoDir, video.getVideoFilename(videoFile)) + const destination = getVideoFilePath(video, videoFile) await move(videoPhysicalFile.path, destination) // This is important in case if there is another attempt in the retry process - videoPhysicalFile.filename = video.getVideoFilename(videoFile) + videoPhysicalFile.filename = getVideoFilePath(video, videoFile) videoPhysicalFile.path = destination // Process thumbnail or create it from the video @@ -234,7 +236,7 @@ async function addVideo (req: express.Request, res: express.Response) { : await generateVideoMiniature(video, videoFile, ThumbnailType.PREVIEW) // Create the torrent file - await video.createTorrentAndSetInfoHash(videoFile) + await createTorrentAndSetInfoHash(video, videoFile) const { videoCreated } = await sequelizeTypescript.transaction(async t => { 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' import { root } from '../helpers/core-utils' import { CONFIG } from '../initializers/config' import { getPreview, getVideoCaption } from './lazy-static' +import { VideoStreamingPlaylistType } from '@shared/models/videos/video-streaming-playlist.type' +import { MVideoFile, MVideoFullLight } from '@server/typings/models' +import { getTorrentFilePath, getVideoFilePath } from '@server/lib/video-paths' const staticRouter = express.Router() @@ -39,6 +42,11 @@ staticRouter.use( asyncMiddleware(videosGetValidator), asyncMiddleware(downloadTorrent) ) +staticRouter.use( + STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+)-hls.torrent', + asyncMiddleware(videosGetValidator), + asyncMiddleware(downloadHLSVideoFileTorrent) +) // Videos path for webseeding staticRouter.use( @@ -58,6 +66,12 @@ staticRouter.use( asyncMiddleware(downloadVideoFile) ) +staticRouter.use( + STATIC_DOWNLOAD_PATHS.HLS_VIDEOS + ':id-:resolution([0-9]+).:extension', + asyncMiddleware(videosGetValidator), + asyncMiddleware(downloadHLSVideoFile) +) + // HLS staticRouter.use( STATIC_PATHS.STREAMING_PLAYLISTS.HLS, @@ -227,24 +241,55 @@ async function generateNodeinfo (req: express.Request, res: express.Response) { } async function downloadTorrent (req: express.Request, res: express.Response) { - const { video, videoFile } = getVideoAndFile(req, res) + const video = res.locals.videoAll + + const videoFile = getVideoFile(req, video.VideoFiles) + if (!videoFile) return res.status(404).end() + + return res.download(getTorrentFilePath(video, videoFile), `${video.name}-${videoFile.resolution}p.torrent`) +} + +async function downloadHLSVideoFileTorrent (req: express.Request, res: express.Response) { + const video = res.locals.videoAll + + const playlist = getHLSPlaylist(video) + if (!playlist) return res.status(404).end + + const videoFile = getVideoFile(req, playlist.VideoFiles) if (!videoFile) return res.status(404).end() - return res.download(video.getTorrentFilePath(videoFile), `${video.name}-${videoFile.resolution}p.torrent`) + return res.download(getTorrentFilePath(playlist, videoFile), `${video.name}-${videoFile.resolution}p-hls.torrent`) } async function downloadVideoFile (req: express.Request, res: express.Response) { - const { video, videoFile } = getVideoAndFile(req, res) + const video = res.locals.videoAll + + const videoFile = getVideoFile(req, video.VideoFiles) if (!videoFile) return res.status(404).end() - return res.download(video.getVideoFilePath(videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`) + return res.download(getVideoFilePath(video, videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`) } -function getVideoAndFile (req: express.Request, res: express.Response) { - const resolution = parseInt(req.params.resolution, 10) +async function downloadHLSVideoFile (req: express.Request, res: express.Response) { const video = res.locals.videoAll + const playlist = getHLSPlaylist(video) + if (!playlist) return res.status(404).end + + const videoFile = getVideoFile(req, playlist.VideoFiles) + if (!videoFile) return res.status(404).end() + + const filename = `${video.name}-${videoFile.resolution}p-${playlist.getStringType()}${videoFile.extname}` + return res.download(getVideoFilePath(playlist, videoFile), filename) +} + +function getVideoFile (req: express.Request, files: MVideoFile[]) { + const resolution = parseInt(req.params.resolution, 10) + return files.find(f => f.resolution === resolution) +} - const videoFile = video.VideoFiles.find(f => f.resolution === resolution) +function getHLSPlaylist (video: MVideoFullLight) { + const playlist = video.VideoStreamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS) + if (!playlist) return undefined - return { video, videoFile } + return Object.assign(playlist, { Video: video }) } -- cgit v1.2.3