]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/cache-file.ts
Refactor auth flow
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / cache-file.ts
index f6f068b456828e37455167d0d881f0b09e31263d..2e6dd34e0365aad0f3920c61d38187d0b0c026b1 100644 (file)
@@ -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/types/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,16 @@ 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
   }
 }
 
-async function createOrUpdateCacheFile (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) {
@@ -32,7 +49,7 @@ async function createOrUpdateCacheFile (cacheFileObject: CacheFileObject, video:
   }
 }
 
-function createCacheFile (cacheFileObject: CacheFileObject, video: VideoModel, byActor: { id?: number }, t: Transaction) {
+function createCacheFile (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId, t: Transaction) {
   const attributes = cacheFileActivityObjectToDBAttributes(cacheFileObject, video, byActor)
 
   return VideoRedundancyModel.create(attributes, { transaction: t })
@@ -40,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) {
@@ -51,8 +68,8 @@ 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 })
 }