]>
Commit | Line | Data |
---|---|---|
418d092a | 1 | import { CacheFileObject } from '../../../shared/index' |
c48e82b5 | 2 | import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy' |
e5565833 | 3 | import { Transaction } from 'sequelize' |
09209296 | 4 | import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type' |
453e83ea | 5 | import { MActorId, MVideoRedundancy, MVideoWithAllFiles } from '@server/typings/models' |
c48e82b5 | 6 | |
453e83ea | 7 | function cacheFileActivityObjectToDBAttributes (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId) { |
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 { | |
b764380a | 16 | expiresOn: cacheFileObject.expires ? new Date(cacheFileObject.expires) : null, |
09209296 C |
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 { | |
b764380a | 33 | expiresOn: cacheFileObject.expires ? new Date(cacheFileObject.expires) : null, |
c48e82b5 | 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 | ||
453e83ea | 42 | async function createOrUpdateCacheFile (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId, t: Transaction) { |
b88a4596 C |
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 | ||
453e83ea | 52 | function createCacheFile (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId, t: Transaction) { |
e5565833 | 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, | |
453e83ea C |
60 | redundancyModel: MVideoRedundancy, |
61 | video: MVideoWithAllFiles, | |
62 | byActor: MActorId, | |
e5565833 C |
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 | 70 | |
1735c825 C |
71 | redundancyModel.expiresOn = attributes.expiresOn |
72 | redundancyModel.fileUrl = attributes.fileUrl | |
c48e82b5 | 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 | } |