aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/cache-file.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-06-03 14:30:09 +0200
committerChocobozzz <me@florianbigard.com>2021-06-03 16:40:32 +0200
commit49af5ac8c2653cb0ef23479c9d3256c5b724d49d (patch)
tree6783df1833b13e141cfd5dc0177531887c4a4e2e /server/lib/activitypub/cache-file.ts
parent9777fe9eebe53debdf45091cab98f72a5987e05a (diff)
downloadPeerTube-49af5ac8c2653cb0ef23479c9d3256c5b724d49d.tar.gz
PeerTube-49af5ac8c2653cb0ef23479c9d3256c5b724d49d.tar.zst
PeerTube-49af5ac8c2653cb0ef23479c9d3256c5b724d49d.zip
Refactor AP playlists
Diffstat (limited to 'server/lib/activitypub/cache-file.ts')
-rw-r--r--server/lib/activitypub/cache-file.ts91
1 files changed, 46 insertions, 45 deletions
diff --git a/server/lib/activitypub/cache-file.ts b/server/lib/activitypub/cache-file.ts
index 2e6dd34e0..a16d2cd93 100644
--- a/server/lib/activitypub/cache-file.ts
+++ b/server/lib/activitypub/cache-file.ts
@@ -1,54 +1,27 @@
1import { CacheFileObject } from '../../../shared/index'
2import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy'
3import { Transaction } from 'sequelize' 1import { Transaction } from 'sequelize'
4import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type'
5import { MActorId, MVideoRedundancy, MVideoWithAllFiles } from '@server/types/models' 2import { MActorId, MVideoRedundancy, MVideoWithAllFiles } from '@server/types/models'
3import { CacheFileObject } from '../../../shared/index'
4import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type'
5import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy'
6 6
7function cacheFileActivityObjectToDBAttributes (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId) { 7async function createOrUpdateCacheFile (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId, t: Transaction) {
8 8 const redundancyModel = await VideoRedundancyModel.loadByUrl(cacheFileObject.id, t)
9 if (cacheFileObject.url.mediaType === 'application/x-mpegURL') {
10 const url = cacheFileObject.url
11
12 const playlist = video.VideoStreamingPlaylists.find(t => t.type === VideoStreamingPlaylistType.HLS)
13 if (!playlist) throw new Error('Cannot find HLS playlist of video ' + video.url)
14 9
15 return { 10 if (redundancyModel) {
16 expiresOn: cacheFileObject.expires ? new Date(cacheFileObject.expires) : null, 11 return updateCacheFile(cacheFileObject, redundancyModel, video, byActor, t)
17 url: cacheFileObject.id,
18 fileUrl: url.href,
19 strategy: null,
20 videoStreamingPlaylistId: playlist.id,
21 actorId: byActor.id
22 }
23 } 12 }
24 13
25 const url = cacheFileObject.url 14 return createCacheFile(cacheFileObject, video, byActor, t)
26 const videoFile = video.VideoFiles.find(f => {
27 return f.resolution === url.height && f.fps === url.fps
28 })
29
30 if (!videoFile) throw new Error(`Cannot find video file ${url.height} ${url.fps} of video ${video.url}`)
31
32 return {
33 expiresOn: cacheFileObject.expires ? new Date(cacheFileObject.expires) : null,
34 url: cacheFileObject.id,
35 fileUrl: url.href,
36 strategy: null,
37 videoFileId: videoFile.id,
38 actorId: byActor.id
39 }
40} 15}
41 16
42async function createOrUpdateCacheFile (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId, t: Transaction) { 17// ---------------------------------------------------------------------------
43 const redundancyModel = await VideoRedundancyModel.loadByUrl(cacheFileObject.id, t)
44 18
45 if (!redundancyModel) { 19export {
46 await createCacheFile(cacheFileObject, video, byActor, t) 20 createOrUpdateCacheFile
47 } else {
48 await updateCacheFile(cacheFileObject, redundancyModel, video, byActor, t)
49 }
50} 21}
51 22
23// ---------------------------------------------------------------------------
24
52function createCacheFile (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId, t: Transaction) { 25function createCacheFile (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId, t: Transaction) {
53 const attributes = cacheFileActivityObjectToDBAttributes(cacheFileObject, video, byActor) 26 const attributes = cacheFileActivityObjectToDBAttributes(cacheFileObject, video, byActor)
54 27
@@ -74,9 +47,37 @@ function updateCacheFile (
74 return redundancyModel.save({ transaction: t }) 47 return redundancyModel.save({ transaction: t })
75} 48}
76 49
77export { 50function cacheFileActivityObjectToDBAttributes (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId) {
78 createOrUpdateCacheFile, 51
79 createCacheFile, 52 if (cacheFileObject.url.mediaType === 'application/x-mpegURL') {
80 updateCacheFile, 53 const url = cacheFileObject.url
81 cacheFileActivityObjectToDBAttributes 54
55 const playlist = video.VideoStreamingPlaylists.find(t => t.type === VideoStreamingPlaylistType.HLS)
56 if (!playlist) throw new Error('Cannot find HLS playlist of video ' + video.url)
57
58 return {
59 expiresOn: cacheFileObject.expires ? new Date(cacheFileObject.expires) : null,
60 url: cacheFileObject.id,
61 fileUrl: url.href,
62 strategy: null,
63 videoStreamingPlaylistId: playlist.id,
64 actorId: byActor.id
65 }
66 }
67
68 const url = cacheFileObject.url
69 const videoFile = video.VideoFiles.find(f => {
70 return f.resolution === url.height && f.fps === url.fps
71 })
72
73 if (!videoFile) throw new Error(`Cannot find video file ${url.height} ${url.fps} of video ${video.url}`)
74
75 return {
76 expiresOn: cacheFileObject.expires ? new Date(cacheFileObject.expires) : null,
77 url: cacheFileObject.id,
78 fileUrl: url.href,
79 strategy: null,
80 videoFileId: videoFile.id,
81 actorId: byActor.id
82 }
82} 83}