X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Factivitypub%2Fcache-file.ts;h=c3acd7112adddd4a79eb0d409435d81fa0bc727b;hb=49aa917509568a3b96967732512b5ef4ecc50b1b;hp=9a40414bba221e7537b6cd7ba4065545d4d57d6d;hpb=b718fd22374d64534bcfe69932cf562894abed6a;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/activitypub/cache-file.ts b/server/lib/activitypub/cache-file.ts index 9a40414bb..c3acd7112 100644 --- a/server/lib/activitypub/cache-file.ts +++ b/server/lib/activitypub/cache-file.ts @@ -1,10 +1,52 @@ -import { ActivityPlaylistUrlObject, ActivityVideoUrlObject, 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/types/models' +import { CacheFileObject, VideoStreamingPlaylistType } from '@shared/models' +import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy' + +async function createOrUpdateCacheFile (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId, t: Transaction) { + const redundancyModel = await VideoRedundancyModel.loadByUrl(cacheFileObject.id, t) + + if (redundancyModel) { + return updateCacheFile(cacheFileObject, redundancyModel, video, byActor, t) + } + + return createCacheFile(cacheFileObject, video, byActor, t) +} + +// --------------------------------------------------------------------------- + +export { + createOrUpdateCacheFile +} + +// --------------------------------------------------------------------------- + +function createCacheFile (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId, t: Transaction) { + const attributes = cacheFileActivityObjectToDBAttributes(cacheFileObject, video, byActor) -function cacheFileActivityObjectToDBAttributes (cacheFileObject: CacheFileObject, video: VideoModel, byActor: { id?: number }) { + return VideoRedundancyModel.create(attributes, { transaction: t }) +} + +function updateCacheFile ( + cacheFileObject: CacheFileObject, + redundancyModel: MVideoRedundancy, + video: MVideoWithAllFiles, + byActor: MActorId, + t: Transaction +) { + if (redundancyModel.actorId !== byActor.id) { + throw new Error('Cannot update redundancy ' + redundancyModel.url + ' of another actor.') + } + + const attributes = cacheFileActivityObjectToDBAttributes(cacheFileObject, video, byActor) + + redundancyModel.expiresOn = attributes.expiresOn + redundancyModel.fileUrl = attributes.fileUrl + + return redundancyModel.save({ transaction: t }) +} + +function cacheFileActivityObjectToDBAttributes (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId) { if (cacheFileObject.url.mediaType === 'application/x-mpegURL') { const url = cacheFileObject.url @@ -13,7 +55,7 @@ function cacheFileActivityObjectToDBAttributes (cacheFileObject: CacheFileObject if (!playlist) throw new Error('Cannot find HLS playlist of video ' + video.url) return { - expiresOn: new Date(cacheFileObject.expires), + expiresOn: cacheFileObject.expires ? new Date(cacheFileObject.expires) : null, url: cacheFileObject.id, fileUrl: url.href, strategy: null, @@ -30,7 +72,7 @@ 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: url.href, strategy: null, @@ -38,45 +80,3 @@ function cacheFileActivityObjectToDBAttributes (cacheFileObject: CacheFileObject actorId: byActor.id } } - -async function createOrUpdateCacheFile (cacheFileObject: CacheFileObject, video: VideoModel, byActor: { id?: number }, 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: VideoModel, byActor: { id?: number }, t: Transaction) { - const attributes = cacheFileActivityObjectToDBAttributes(cacheFileObject, video, byActor) - - return VideoRedundancyModel.create(attributes, { transaction: t }) -} - -function updateCacheFile ( - cacheFileObject: CacheFileObject, - redundancyModel: VideoRedundancyModel, - video: VideoModel, - byActor: { id?: number }, - t: Transaction -) { - if (redundancyModel.actorId !== byActor.id) { - throw new Error('Cannot update redundancy ' + redundancyModel.url + ' of another actor.') - } - - const attributes = cacheFileActivityObjectToDBAttributes(cacheFileObject, video, byActor) - - redundancyModel.set('expires', attributes.expiresOn) - redundancyModel.set('fileUrl', attributes.fileUrl) - - return redundancyModel.save({ transaction: t }) -} - -export { - createOrUpdateCacheFile, - createCacheFile, - updateCacheFile, - cacheFileActivityObjectToDBAttributes -}