diff options
author | Chocobozzz <me@florianbigard.com> | 2021-06-03 14:30:09 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-06-03 16:40:32 +0200 |
commit | 49af5ac8c2653cb0ef23479c9d3256c5b724d49d (patch) | |
tree | 6783df1833b13e141cfd5dc0177531887c4a4e2e /server/lib/activitypub/cache-file.ts | |
parent | 9777fe9eebe53debdf45091cab98f72a5987e05a (diff) | |
download | PeerTube-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.ts | 91 |
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 @@ | |||
1 | import { CacheFileObject } from '../../../shared/index' | ||
2 | import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy' | ||
3 | import { Transaction } from 'sequelize' | 1 | import { Transaction } from 'sequelize' |
4 | import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type' | ||
5 | import { MActorId, MVideoRedundancy, MVideoWithAllFiles } from '@server/types/models' | 2 | import { MActorId, MVideoRedundancy, MVideoWithAllFiles } from '@server/types/models' |
3 | import { CacheFileObject } from '../../../shared/index' | ||
4 | import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type' | ||
5 | import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy' | ||
6 | 6 | ||
7 | function cacheFileActivityObjectToDBAttributes (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId) { | 7 | async 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 | ||
42 | async 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) { | 19 | export { |
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 | |||
52 | function createCacheFile (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId, t: Transaction) { | 25 | function 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 | ||
77 | export { | 50 | function 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 | } |