From 8efc27bf14f1fe3ed23cd8a6d2de1f0918a7f769 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 18 Feb 2021 11:28:00 +0100 Subject: [PATCH] Cleanup --- scripts/optimize-old-videos.ts | 2 +- scripts/update-host.ts | 2 +- server/controllers/api/videos/index.ts | 2 +- server/controllers/lazy-static.ts | 1 + server/helpers/webtorrent.ts | 9 +++++---- server/lib/activitypub/videos.ts | 2 +- server/lib/job-queue/handlers/video-file-import.ts | 2 +- server/lib/job-queue/handlers/video-import.ts | 2 +- server/lib/video-transcoding.ts | 4 ++-- server/models/video/thumbnail.ts | 4 ++-- server/models/video/video-caption.ts | 4 ++-- server/models/video/video-file.ts | 4 ++-- server/models/video/video-format-utils.ts | 6 +++--- 13 files changed, 23 insertions(+), 21 deletions(-) diff --git a/scripts/optimize-old-videos.ts b/scripts/optimize-old-videos.ts index 8e2e7fcf4..01d30244f 100644 --- a/scripts/optimize-old-videos.ts +++ b/scripts/optimize-old-videos.ts @@ -72,7 +72,7 @@ async function run () { console.log('Failed to optimize %s, restoring original', basename(currentFile)) await move(backupFile, currentFile, { overwrite: true }) - await createTorrentAndSetInfoHash(video, video, file) + await createTorrentAndSetInfoHash(video, file) await file.save() } } diff --git a/scripts/update-host.ts b/scripts/update-host.ts index d0a1b03cc..e497be4e2 100755 --- a/scripts/update-host.ts +++ b/scripts/update-host.ts @@ -127,7 +127,7 @@ async function run () { for (const file of video.VideoFiles) { console.log('Updating torrent file %s of video %s.', file.resolution, video.uuid) - await createTorrentAndSetInfoHash(video, video, file) + await createTorrentAndSetInfoHash(video, file) } for (const playlist of video.VideoStreamingPlaylists) { diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts index dcd6194ae..e89315930 100644 --- a/server/controllers/api/videos/index.ts +++ b/server/controllers/api/videos/index.ts @@ -222,7 +222,7 @@ async function addVideo (req: express.Request, res: express.Response) { }) // Create the torrent file - await createTorrentAndSetInfoHash(video, video, videoFile) + await createTorrentAndSetInfoHash(video, videoFile) const { videoCreated } = await sequelizeTypescript.transaction(async t => { const sequelizeOptions = { transaction: t } diff --git a/server/controllers/lazy-static.ts b/server/controllers/lazy-static.ts index c2f5c7b56..4e553479b 100644 --- a/server/controllers/lazy-static.ts +++ b/server/controllers/lazy-static.ts @@ -94,5 +94,6 @@ async function getTorrent (req: express.Request, res: express.Response) { const result = await VideosTorrentCache.Instance.getFilePath(req.params.filename) if (!result) return res.sendStatus(HttpStatusCode.NOT_FOUND_404) + // Torrents still use the old naming convention (video uuid + .torrent) return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.SERVER }) } diff --git a/server/helpers/webtorrent.ts b/server/helpers/webtorrent.ts index 4e08c27c6..d8220ba9c 100644 --- a/server/helpers/webtorrent.ts +++ b/server/helpers/webtorrent.ts @@ -7,13 +7,14 @@ import * as WebTorrent from 'webtorrent' import { isArray } from '@server/helpers/custom-validators/misc' import { WEBSERVER } from '@server/initializers/constants' import { generateTorrentFileName, getVideoFilePath } from '@server/lib/video-paths' -import { MVideo, MVideoWithHost } from '@server/types/models/video/video' +import { MVideo } from '@server/types/models/video/video' import { MVideoFile, MVideoFileRedundanciesOpt } from '@server/types/models/video/video-file' import { MStreamingPlaylistVideo } from '@server/types/models/video/video-streaming-playlist' import { CONFIG } from '../initializers/config' import { promisify2 } from './core-utils' import { logger } from './logger' import { generateVideoImportTmpPath } from './utils' +import { extractVideo } from './video' const createTorrentPromise = promisify2(createTorrent) @@ -77,12 +78,12 @@ async function downloadWebTorrentVideo (target: { magnetUri: string, torrentName }) } -// FIXME: refactor/merge videoOrPlaylist and video arguments async function createTorrentAndSetInfoHash ( videoOrPlaylist: MVideo | MStreamingPlaylistVideo, - video: MVideoWithHost, videoFile: MVideoFile ) { + const video = extractVideo(videoOrPlaylist) + const options = { // Keep the extname, it's used by the client to stream the file inside a web browser name: `${video.name} ${videoFile.resolution}p${videoFile.extname}`, @@ -108,7 +109,7 @@ async function createTorrentAndSetInfoHash ( } function generateMagnetUri ( - video: MVideoWithHost, + video: MVideo, videoFile: MVideoFileRedundanciesOpt, trackerUrls: string[] ) { diff --git a/server/lib/activitypub/videos.ts b/server/lib/activitypub/videos.ts index 66330a964..c38edad56 100644 --- a/server/lib/activitypub/videos.ts +++ b/server/lib/activitypub/videos.ts @@ -912,7 +912,7 @@ function getTrackerUrls (object: VideoObject, video: MVideoWithHost) { const trackers = object.url.filter(u => isAPVideoTrackerUrlObject(u)) .map((u: ActivityTrackerUrlObject) => { - if (u.rel.includes('websocket')) wsFound = true + if (isArray(u.rel) && u.rel.includes('websocket')) wsFound = true return u.href }) diff --git a/server/lib/job-queue/handlers/video-file-import.ts b/server/lib/job-queue/handlers/video-file-import.ts index 839916306..71f2cafcd 100644 --- a/server/lib/job-queue/handlers/video-file-import.ts +++ b/server/lib/job-queue/handlers/video-file-import.ts @@ -82,7 +82,7 @@ async function updateVideoFile (video: MVideoFullLight, inputFilePath: string) { await copy(inputFilePath, outputPath) video.VideoFiles.push(newVideoFile) - await createTorrentAndSetInfoHash(video, video, newVideoFile) + await createTorrentAndSetInfoHash(video, newVideoFile) await newVideoFile.save() } diff --git a/server/lib/job-queue/handlers/video-import.ts b/server/lib/job-queue/handlers/video-import.ts index 8fa024105..ed2c5eac0 100644 --- a/server/lib/job-queue/handlers/video-import.ts +++ b/server/lib/job-queue/handlers/video-import.ts @@ -185,7 +185,7 @@ async function processFile (downloader: () => Promise, videoImport: MVid } // Create torrent - await createTorrentAndSetInfoHash(videoImportWithFiles.Video, videoImportWithFiles.Video, videoFile) + await createTorrentAndSetInfoHash(videoImportWithFiles.Video, videoFile) const videoFileSave = videoFile.toJSON() diff --git a/server/lib/video-transcoding.ts b/server/lib/video-transcoding.ts index e3cd18e25..c949dca2e 100644 --- a/server/lib/video-transcoding.ts +++ b/server/lib/video-transcoding.ts @@ -254,7 +254,7 @@ async function onWebTorrentVideoFileTranscoding ( videoFile.fps = fps videoFile.metadata = metadata - await createTorrentAndSetInfoHash(video, video, videoFile) + await createTorrentAndSetInfoHash(video, videoFile) await VideoFileModel.customUpsert(videoFile, 'video', undefined) video.VideoFiles = await video.$get('VideoFiles') @@ -350,7 +350,7 @@ async function generateHlsPlaylistCommon (options: { newVideoFile.fps = await getVideoFileFPS(videoFilePath) newVideoFile.metadata = await getMetadataFromFile(videoFilePath) - await createTorrentAndSetInfoHash(videoStreamingPlaylist, video, newVideoFile) + await createTorrentAndSetInfoHash(videoStreamingPlaylist, newVideoFile) await VideoFileModel.customUpsert(newVideoFile, 'streaming-playlist', undefined) videoStreamingPlaylist.VideoFiles = await videoStreamingPlaylist.$get('VideoFiles') diff --git a/server/models/video/thumbnail.ts b/server/models/video/thumbnail.ts index 319e1175a..f1187c8d6 100644 --- a/server/models/video/thumbnail.ts +++ b/server/models/video/thumbnail.ts @@ -16,7 +16,7 @@ import { UpdatedAt } from 'sequelize-typescript' import { afterCommitIfTransaction } from '@server/helpers/database-utils' -import { MThumbnail, MThumbnailVideo, MVideoWithHost } from '@server/types/models' +import { MThumbnail, MThumbnailVideo, MVideo } from '@server/types/models' import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type' import { logger } from '../../helpers/logger' import { CONFIG } from '../../initializers/config' @@ -163,7 +163,7 @@ export class ThumbnailModel extends Model { return join(directory, filename) } - getFileUrl (video: MVideoWithHost) { + getFileUrl (video: MVideo) { const staticPath = ThumbnailModel.types[this.type].staticPath + this.filename if (video.isOwned()) return WEBSERVER.URL + staticPath diff --git a/server/models/video/video-caption.ts b/server/models/video/video-caption.ts index 0bbe9b752..bfdec73e9 100644 --- a/server/models/video/video-caption.ts +++ b/server/models/video/video-caption.ts @@ -16,7 +16,7 @@ import { UpdatedAt } from 'sequelize-typescript' import { v4 as uuidv4 } from 'uuid' -import { MVideoCaption, MVideoCaptionFormattable, MVideoCaptionVideo, MVideoWithHost } from '@server/types/models' +import { MVideo, MVideoCaption, MVideoCaptionFormattable, MVideoCaptionVideo } from '@server/types/models' import { VideoCaption } from '../../../shared/models/videos/caption/video-caption.model' import { isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions' import { logger } from '../../helpers/logger' @@ -203,7 +203,7 @@ export class VideoCaptionModel extends Model { return remove(CONFIG.STORAGE.CAPTIONS_DIR + this.filename) } - getFileUrl (video: MVideoWithHost) { + getFileUrl (video: MVideo) { if (!this.Video) this.Video = video as VideoModel if (video.isOwned()) return WEBSERVER.URL + this.getCaptionStaticPath() diff --git a/server/models/video/video-file.ts b/server/models/video/video-file.ts index 5a3706259..4df2c20bc 100644 --- a/server/models/video/video-file.ts +++ b/server/models/video/video-file.ts @@ -423,7 +423,7 @@ export class VideoFileModel extends Model { return !!this.videoStreamingPlaylistId } - getFileUrl (video: MVideoWithHost) { + getFileUrl (video: MVideo) { if (!this.Video) this.Video = video as VideoModel if (video.isOwned()) return WEBSERVER.URL + this.getFileStaticPath(video) @@ -449,7 +449,7 @@ export class VideoFileModel extends Model { return buildRemoteVideoBaseUrl(video, path) } - getRemoteTorrentUrl (video: MVideoWithHost) { + getRemoteTorrentUrl (video: MVideo) { if (video.isOwned()) throw new Error(`Video ${video.url} is not a remote video`) return this.torrentUrl diff --git a/server/models/video/video-format-utils.ts b/server/models/video/video-format-utils.ts index 455597d22..a6a1a4f0d 100644 --- a/server/models/video/video-format-utils.ts +++ b/server/models/video/video-format-utils.ts @@ -14,11 +14,11 @@ import { } from '../../lib/activitypub/url' import { MStreamingPlaylistRedundanciesOpt, + MVideo, MVideoAP, MVideoFile, MVideoFormattable, - MVideoFormattableDetails, - MVideoWithHost + MVideoFormattableDetails } from '../../types/models' import { MVideoFileRedundanciesOpt } from '../../types/models/video/video-file' import { VideoModel } from './video' @@ -225,7 +225,7 @@ function videoFilesModelToFormattedJSON ( function addVideoFilesInAPAcc ( acc: ActivityUrlObject[] | ActivityTagObject[], - video: MVideoWithHost, + video: MVideo, files: MVideoFile[] ) { const trackerUrls = video.getTrackerUrls() -- 2.41.0