ActivityPub-federated video streaming platform using P2P directly in your web browser
Fix leaked torrent files
Chocobozzzdd5a4953dbdf
diff --git a/server/core/lib/job-queue/handlers/manage-video-torrent.ts b/server/core/lib/job-queue/handlers/manage-video-torrent.tsindex b2c223abb0..5e8cef9d0f 100644--- a/server/core/lib/job-queue/handlers/manage-video-torrent.ts+++ b/server/core/lib/job-queue/handlers/manage-video-torrent.ts@@ -85,7 +85,14 @@ async function doUpdateMetadataAction (payload: ManageVideoTorrentPayload & { acconst fileMutexReleaser = await VideoPathManager.Instance.lockFiles(extractedVideo.uuid)try {- await updateTorrentForFileAndSave(video || streamingPlaylist, file)+ // Reload the file: another job may have updated it (its torrent filename for example) while we were waiting for the mutex+ const refreshedFile = await VideoFileModel.load(file.id)+ if (!refreshedFile) {+ logger.debug('Do not update torrent metadata for file %d: does not exist anymore.', file.id)+ return+ }++ await updateTorrentForFileAndSave(video || streamingPlaylist, refreshedFile)} finally {fileMutexReleaser()}diff --git a/server/core/lib/transcoding/web-transcoding.ts b/server/core/lib/transcoding/web-transcoding.tsindex fb53884dbf..78250dfd15 100644--- a/server/core/lib/transcoding/web-transcoding.ts+++ b/server/core/lib/transcoding/web-transcoding.ts@@ -234,8 +234,13 @@ export async function onWebVideoFileTranscoding (options: {if (deleteWebInputVideoFile) {await saveNewOriginalFileIfNeeded(video, deleteWebInputVideoFile)- await video.removeWebVideoFile(deleteWebInputVideoFile)- await deleteWebInputVideoFile.destroy()+ // Reload the file: another job may have updated it (its torrent filename for example) while we were transcoding+ const inputFileToDelete = await VideoFileModel.load(deleteWebInputVideoFile.id)++ if (inputFileToDelete) {+ await video.removeWebVideoFile(inputFileToDelete)+ await inputFileToDelete.destroy()+ }}const existingFile = await VideoFileModel.loadWebVideoFile({ videoId: video.id, fps: videoFile.fps, resolution: videoFile.resolution })diff --git a/server/core/lib/video-file.ts b/server/core/lib/video-file.tsindex c7e247ce6e..3c7b6c85de 100644--- a/server/core/lib/video-file.ts+++ b/server/core/lib/video-file.ts@@ -124,7 +124,11 @@ export async function removeAllWebVideoFiles (video: MVideoWithAllFiles, optionsconst videoFileMutexReleaser = await VideoPathManager.Instance.lockFiles(video.uuid)try {- for (const file of video.VideoFiles) {+ // Reload the files: another job may have updated them (their torrent filename for example) while we were waiting for the mutex+ const files = await video.$get('VideoFiles')+ video.VideoFiles = files++ for (const file of files) {if (resolutionExceptions.includes(file.resolution)) continueawait video.removeWebVideoFile(file)@@ -148,11 +152,15 @@ export async function removeWebVideoFile (video: MVideoWithAllFiles, fileToDeletconst videoFileMutexReleaser = await VideoPathManager.Instance.lockFiles(video.uuid)try {- const toDelete = files.find(f => f.id === fileToDeleteId)- await video.removeWebVideoFile(toDelete)- await toDelete.destroy()+ // Reload the file: another job may have updated it (its torrent filename for example) while we were waiting for the mutex+ const toDelete = await VideoFileModel.load(fileToDeleteId)++ if (toDelete) {+ await video.removeWebVideoFile(toDelete)+ await toDelete.destroy()+ }- video.VideoFiles = files.filter(f => f.id !== toDelete.id)+ video.VideoFiles = files.filter(f => f.id !== fileToDeleteId)} finally {videoFileMutexReleaser()}diff --git a/server/core/lib/webtorrent.ts b/server/core/lib/webtorrent.tsindex 5c8ee96d84..0faad4f8b2 100644--- a/server/core/lib/webtorrent.ts+++ b/server/core/lib/webtorrent.ts@@ -3,6 +3,7 @@ import { WEBSERVER } from '@server/initializers/constants.js'import { generateTorrentFileName } from '@server/lib/paths.js'import { VideoPathManager } from '@server/lib/video-path-manager.js'import { createTorrentFromWorker } from '@server/lib/worker/parent-process.js'+import { VideoFileModel } from '@server/models/video/video-file.js'import { VideoInfohashModel } from '@server/models/video/video-infohash.js'import { MVideoFile, MVideoFileInfoHash } from '@server/types/models/video/video-file.js'import { MStreamingPlaylistVideo } from '@server/types/models/video/video-streaming-playlist.js'@@ -190,6 +191,14 @@ export async function updateTorrentForFileAndSave (videoOrPlaylist: MVideo | MStawait writeFile(newTorrentPath, bencode.encode(decoded))await remove(oldTorrentPath)+ // The video file may have been deleted in the meantime, so don't leave the new torrent on disk+ if (!await VideoFileModel.load(videoFile.id)) {+ logger.info('Do not save torrent metadata update %s because the video file does not exist anymore.', newTorrentPath)++ await remove(newTorrentPath)+ return+ }+videoFile.torrentFilename = newTorrentFilenameawait VideoInfohashModel.replaceFileInfohash(videoFile.id, sha1(bencode.encode(decoded.info)))