]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/cache-file.ts
Remove deprecated transcoding job names
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / cache-file.ts
index 5286d8e6ddc9294900c1402f8b63730f5ecd3f6b..a16d2cd93f1a489f13a78b7d269d4560a68212ea 100644 (file)
@@ -1,28 +1,28 @@
+import { Transaction } from 'sequelize'
+import { MActorId, MVideoRedundancy, MVideoWithAllFiles } from '@server/types/models'
 import { CacheFileObject } from '../../../shared/index'
-import { VideoModel } from '../../models/video/video'
+import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type'
 import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy'
-import { Transaction } from 'sequelize'
 
-function cacheFileActivityObjectToDBAttributes (cacheFileObject: CacheFileObject, video: VideoModel, byActor: { id?: number }) {
-  const url = cacheFileObject.url
+async function createOrUpdateCacheFile (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId, t: Transaction) {
+  const redundancyModel = await VideoRedundancyModel.loadByUrl(cacheFileObject.id, t)
 
-  const videoFile = video.VideoFiles.find(f => {
-    return f.resolution === url.height && f.fps === url.fps
-  })
+  if (redundancyModel) {
+    return updateCacheFile(cacheFileObject, redundancyModel, video, byActor, t)
+  }
 
-  if (!videoFile) throw new Error(`Cannot find video file ${url.height} ${url.fps} of video ${video.url}`)
+  return createCacheFile(cacheFileObject, video, byActor, t)
+}
 
-  return {
-    expiresOn: new Date(cacheFileObject.expires),
-    url: cacheFileObject.id,
-    fileUrl: cacheFileObject.url.href,
-    strategy: null,
-    videoFileId: videoFile.id,
-    actorId: byActor.id
-  }
+// ---------------------------------------------------------------------------
+
+export {
+  createOrUpdateCacheFile
 }
 
-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 })
@@ -30,9 +30,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,14 +41,43 @@ 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 {
-  createCacheFile,
-  updateCacheFile,
-  cacheFileActivityObjectToDBAttributes
+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
+  })
+
+  if (!videoFile) throw new Error(`Cannot find video file ${url.height} ${url.fps} of video ${video.url}`)
+
+  return {
+    expiresOn: cacheFileObject.expires ? new Date(cacheFileObject.expires) : null,
+    url: cacheFileObject.id,
+    fileUrl: url.href,
+    strategy: null,
+    videoFileId: videoFile.id,
+    actorId: byActor.id
+  }
 }