]>
Commit | Line | Data |
---|---|---|
09209296 | 1 | import { ActivityPlaylistUrlObject, ActivityVideoUrlObject, CacheFileObject } from '../../../shared/index' |
c48e82b5 | 2 | import { VideoModel } from '../../models/video/video' |
c48e82b5 | 3 | import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy' |
e5565833 | 4 | import { Transaction } from 'sequelize' |
09209296 | 5 | import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type' |
c48e82b5 | 6 | |
e587e0ec | 7 | function cacheFileActivityObjectToDBAttributes (cacheFileObject: CacheFileObject, video: VideoModel, byActor: { id?: number }) { |
c48e82b5 | 8 | |
09209296 C |
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 | ||
15 | return { | |
16 | expiresOn: new Date(cacheFileObject.expires), | |
17 | url: cacheFileObject.id, | |
18 | fileUrl: url.href, | |
19 | strategy: null, | |
20 | videoStreamingPlaylistId: playlist.id, | |
21 | actorId: byActor.id | |
22 | } | |
23 | } | |
24 | ||
25 | const url = cacheFileObject.url | |
c48e82b5 C |
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: new Date(cacheFileObject.expires), | |
34 | url: cacheFileObject.id, | |
09209296 | 35 | fileUrl: url.href, |
c48e82b5 C |
36 | strategy: null, |
37 | videoFileId: videoFile.id, | |
38 | actorId: byActor.id | |
39 | } | |
40 | } | |
41 | ||
b88a4596 C |
42 | async function createOrUpdateCacheFile (cacheFileObject: CacheFileObject, video: VideoModel, byActor: { id?: number }, t: Transaction) { |
43 | const redundancyModel = await VideoRedundancyModel.loadByUrl(cacheFileObject.id, t) | |
44 | ||
45 | if (!redundancyModel) { | |
46 | await createCacheFile(cacheFileObject, video, byActor, t) | |
47 | } else { | |
48 | await updateCacheFile(cacheFileObject, redundancyModel, video, byActor, t) | |
49 | } | |
50 | } | |
51 | ||
e5565833 C |
52 | function createCacheFile (cacheFileObject: CacheFileObject, video: VideoModel, byActor: { id?: number }, t: Transaction) { |
53 | const attributes = cacheFileActivityObjectToDBAttributes(cacheFileObject, video, byActor) | |
c48e82b5 | 54 | |
e5565833 | 55 | return VideoRedundancyModel.create(attributes, { transaction: t }) |
c48e82b5 C |
56 | } |
57 | ||
e5565833 C |
58 | function updateCacheFile ( |
59 | cacheFileObject: CacheFileObject, | |
60 | redundancyModel: VideoRedundancyModel, | |
61 | video: VideoModel, | |
62 | byActor: { id?: number }, | |
63 | t: Transaction | |
64 | ) { | |
12ba460e C |
65 | if (redundancyModel.actorId !== byActor.id) { |
66 | throw new Error('Cannot update redundancy ' + redundancyModel.url + ' of another actor.') | |
67 | } | |
68 | ||
e5565833 | 69 | const attributes = cacheFileActivityObjectToDBAttributes(cacheFileObject, video, byActor) |
c48e82b5 C |
70 | |
71 | redundancyModel.set('expires', attributes.expiresOn) | |
72 | redundancyModel.set('fileUrl', attributes.fileUrl) | |
73 | ||
e5565833 | 74 | return redundancyModel.save({ transaction: t }) |
c48e82b5 C |
75 | } |
76 | ||
77 | export { | |
b88a4596 | 78 | createOrUpdateCacheFile, |
c48e82b5 C |
79 | createCacheFile, |
80 | updateCacheFile, | |
81 | cacheFileActivityObjectToDBAttributes | |
82 | } |