]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/cache-file.ts
Fix tests
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / cache-file.ts
index 2e6dd34e0365aad0f3920c61d38187d0b0c026b1..c3acd7112adddd4a79eb0d409435d81fa0bc727b 100644 (file)
@@ -1,54 +1,26 @@
-import { CacheFileObject } from '../../../shared/index'
-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'
 
-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)
+async function createOrUpdateCacheFile (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId, t: Transaction) {
+  const redundancyModel = await VideoRedundancyModel.loadByUrl(cacheFileObject.id, t)
 
-    return {
-      expiresOn: cacheFileObject.expires ? new Date(cacheFileObject.expires) : null,
-      url: cacheFileObject.id,
-      fileUrl: url.href,
-      strategy: null,
-      videoStreamingPlaylistId: playlist.id,
-      actorId: byActor.id
-    }
+  if (redundancyModel) {
+    return updateCacheFile(cacheFileObject, redundancyModel, video, byActor, t)
   }
 
-  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
-  }
+  return createCacheFile(cacheFileObject, video, byActor, t)
 }
 
-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)
-  }
+export {
+  createOrUpdateCacheFile
 }
 
+// ---------------------------------------------------------------------------
+
 function createCacheFile (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId, t: Transaction) {
   const attributes = cacheFileActivityObjectToDBAttributes(cacheFileObject, video, byActor)
 
@@ -74,9 +46,37 @@ function updateCacheFile (
   return redundancyModel.save({ transaction: t })
 }
 
-export {
-  createOrUpdateCacheFile,
-  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
+  }
 }