From 7b6b445d91d3f0bcbb526cb1e8c1f26b96f0a971 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 25 Jul 2022 10:57:16 +0200 Subject: Regenerate video filenames on transcoding In particular when using manual transcoding, to invalidate potential HTTP caches in front of peertube --- server/models/video/video-file.ts | 41 ++++++++++++++++++++++++++++++++++----- server/models/video/video.ts | 21 +++++++++++++++++++- 2 files changed, 56 insertions(+), 6 deletions(-) (limited to 'server/models/video') diff --git a/server/models/video/video-file.ts b/server/models/video/video-file.ts index 4aaee1ffa..d4f07f85f 100644 --- a/server/models/video/video-file.ts +++ b/server/models/video/video-file.ts @@ -405,15 +405,16 @@ export class VideoFileModel extends Model mode: 'streaming-playlist' | 'video', transaction: Transaction ) { - const baseWhere = { + const baseFind = { fps: videoFile.fps, - resolution: videoFile.resolution + resolution: videoFile.resolution, + transaction } - if (mode === 'streaming-playlist') Object.assign(baseWhere, { videoStreamingPlaylistId: videoFile.videoStreamingPlaylistId }) - else Object.assign(baseWhere, { videoId: videoFile.videoId }) + const element = mode === 'streaming-playlist' + ? await VideoFileModel.loadHLSFile({ ...baseFind, playlistId: videoFile.videoStreamingPlaylistId }) + : await VideoFileModel.loadWebTorrentFile({ ...baseFind, videoId: videoFile.videoId }) - const element = await VideoFileModel.findOne({ where: baseWhere, transaction }) if (!element) return videoFile.save({ transaction }) for (const k of Object.keys(videoFile.toJSON())) { @@ -423,6 +424,36 @@ export class VideoFileModel extends Model return element.save({ transaction }) } + static async loadWebTorrentFile (options: { + videoId: number + fps: number + resolution: number + transaction?: Transaction + }) { + const where = { + fps: options.fps, + resolution: options.resolution, + videoId: options.videoId + } + + return VideoFileModel.findOne({ where, transaction: options.transaction }) + } + + static async loadHLSFile (options: { + playlistId: number + fps: number + resolution: number + transaction?: Transaction + }) { + const where = { + fps: options.fps, + resolution: options.resolution, + videoStreamingPlaylistId: options.playlistId + } + + return VideoFileModel.findOne({ where, transaction: options.transaction }) + } + static removeHLSFilesOfVideoId (videoStreamingPlaylistId: number) { const options = { where: { videoStreamingPlaylistId } diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 55da53058..27e605be6 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -26,7 +26,7 @@ import { } from 'sequelize-typescript' import { getPrivaciesForFederation, isPrivacyForFederation, isStateForFederation } from '@server/helpers/video' import { LiveManager } from '@server/lib/live/live-manager' -import { removeHLSObjectStorage, removeWebTorrentObjectStorage } from '@server/lib/object-storage' +import { removeHLSFileObjectStorage, removeHLSObjectStorage, removeWebTorrentObjectStorage } from '@server/lib/object-storage' import { getHLSDirectory, getHLSRedundancyDirectory } from '@server/lib/paths' import { VideoPathManager } from '@server/lib/video-path-manager' import { getServerActor } from '@server/models/application/application' @@ -1816,6 +1816,25 @@ export class VideoModel extends Model>> { } } + async removeStreamingPlaylistVideoFile (streamingPlaylist: MStreamingPlaylist, videoFile: MVideoFile) { + const filePath = VideoPathManager.Instance.getFSHLSOutputPath(this, videoFile.filename) + await videoFile.removeTorrent() + await remove(filePath) + + if (videoFile.storage === VideoStorage.OBJECT_STORAGE) { + await removeHLSFileObjectStorage(streamingPlaylist.withVideo(this), videoFile.filename) + } + } + + async removeStreamingPlaylistFile (streamingPlaylist: MStreamingPlaylist, filename: string) { + const filePath = VideoPathManager.Instance.getFSHLSOutputPath(this, filename) + await remove(filePath) + + if (streamingPlaylist.storage === VideoStorage.OBJECT_STORAGE) { + await removeHLSFileObjectStorage(streamingPlaylist.withVideo(this), filename) + } + } + isOutdated () { if (this.isOwned()) return false -- cgit v1.2.3