X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Factivitypub%2Fcache-file.ts;h=8252e95e92b99eed1b82690f650c8c89d386ba59;hb=9d94e5d7b96332d628ed835c67c2986289ead9b2;hp=5286d8e6ddc9294900c1402f8b63730f5ecd3f6b;hpb=e5565833f62b97f62ea75eba5b479963ae78b873;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/activitypub/cache-file.ts b/server/lib/activitypub/cache-file.ts index 5286d8e6d..8252e95e9 100644 --- a/server/lib/activitypub/cache-file.ts +++ b/server/lib/activitypub/cache-file.ts @@ -1,11 +1,28 @@ import { CacheFileObject } from '../../../shared/index' -import { VideoModel } from '../../models/video/video' import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy' import { Transaction } from 'sequelize' +import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type' +import { MActorId, MVideoRedundancy, MVideoWithAllFiles } from '@server/typings/models' -function cacheFileActivityObjectToDBAttributes (cacheFileObject: CacheFileObject, video: VideoModel, byActor: { id?: number }) { - const url = cacheFileObject.url +function cacheFileActivityObjectToDBAttributes (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId) { + + if (cacheFileObject.url.mediaType === 'application/x-mpegURL') { + const url = cacheFileObject.url + + const playlist = video.VideoStreamingPlaylists.find(t => t.type === VideoStreamingPlaylistType.HLS) + if (!playlist) throw new Error('Cannot find HLS playlist of video ' + video.url) + + return { + expiresOn: cacheFileObject.expires ? new Date(cacheFileObject.expires) : null, + url: cacheFileObject.id, + fileUrl: url.href, + strategy: null, + videoStreamingPlaylistId: playlist.id, + actorId: byActor.id + } + } + const url = cacheFileObject.url const videoFile = video.VideoFiles.find(f => { return f.resolution === url.height && f.fps === url.fps }) @@ -13,16 +30,26 @@ function cacheFileActivityObjectToDBAttributes (cacheFileObject: CacheFileObject if (!videoFile) throw new Error(`Cannot find video file ${url.height} ${url.fps} of video ${video.url}`) return { - expiresOn: new Date(cacheFileObject.expires), + expiresOn: cacheFileObject.expires ? new Date(cacheFileObject.expires) : null, url: cacheFileObject.id, - fileUrl: cacheFileObject.url.href, + fileUrl: url.href, strategy: null, videoFileId: videoFile.id, actorId: byActor.id } } -function createCacheFile (cacheFileObject: CacheFileObject, video: VideoModel, byActor: { id?: number }, t: Transaction) { +async function createOrUpdateCacheFile (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId, t: Transaction) { + const redundancyModel = await VideoRedundancyModel.loadByUrl(cacheFileObject.id, t) + + if (!redundancyModel) { + await createCacheFile(cacheFileObject, video, byActor, t) + } else { + await updateCacheFile(cacheFileObject, redundancyModel, video, byActor, t) + } +} + +function createCacheFile (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId, t: Transaction) { const attributes = cacheFileActivityObjectToDBAttributes(cacheFileObject, video, byActor) return VideoRedundancyModel.create(attributes, { transaction: t }) @@ -30,9 +57,9 @@ function createCacheFile (cacheFileObject: CacheFileObject, video: VideoModel, b function updateCacheFile ( cacheFileObject: CacheFileObject, - redundancyModel: VideoRedundancyModel, - video: VideoModel, - byActor: { id?: number }, + redundancyModel: MVideoRedundancy, + video: MVideoWithAllFiles, + byActor: MActorId, t: Transaction ) { if (redundancyModel.actorId !== byActor.id) { @@ -41,13 +68,14 @@ function updateCacheFile ( const attributes = cacheFileActivityObjectToDBAttributes(cacheFileObject, video, byActor) - redundancyModel.set('expires', attributes.expiresOn) - redundancyModel.set('fileUrl', attributes.fileUrl) + redundancyModel.expiresOn = attributes.expiresOn + redundancyModel.fileUrl = attributes.fileUrl return redundancyModel.save({ transaction: t }) } export { + createOrUpdateCacheFile, createCacheFile, updateCacheFile, cacheFileActivityObjectToDBAttributes