]>
Commit | Line | Data |
---|---|---|
1 | import { Transaction } from 'sequelize' | |
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 | ||
7 | async function createOrUpdateCacheFile (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId, t: Transaction) { | |
8 | const redundancyModel = await VideoRedundancyModel.loadByUrl(cacheFileObject.id, t) | |
9 | ||
10 | if (redundancyModel) { | |
11 | return updateCacheFile(cacheFileObject, redundancyModel, video, byActor, t) | |
12 | } | |
13 | ||
14 | return createCacheFile(cacheFileObject, video, byActor, t) | |
15 | } | |
16 | ||
17 | // --------------------------------------------------------------------------- | |
18 | ||
19 | export { | |
20 | createOrUpdateCacheFile | |
21 | } | |
22 | ||
23 | // --------------------------------------------------------------------------- | |
24 | ||
25 | function createCacheFile (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId, t: Transaction) { | |
26 | const attributes = cacheFileActivityObjectToDBAttributes(cacheFileObject, video, byActor) | |
27 | ||
28 | return VideoRedundancyModel.create(attributes, { transaction: t }) | |
29 | } | |
30 | ||
31 | function updateCacheFile ( | |
32 | cacheFileObject: CacheFileObject, | |
33 | redundancyModel: MVideoRedundancy, | |
34 | video: MVideoWithAllFiles, | |
35 | byActor: MActorId, | |
36 | t: Transaction | |
37 | ) { | |
38 | if (redundancyModel.actorId !== byActor.id) { | |
39 | throw new Error('Cannot update redundancy ' + redundancyModel.url + ' of another actor.') | |
40 | } | |
41 | ||
42 | const attributes = cacheFileActivityObjectToDBAttributes(cacheFileObject, video, byActor) | |
43 | ||
44 | redundancyModel.expiresOn = attributes.expiresOn | |
45 | redundancyModel.fileUrl = attributes.fileUrl | |
46 | ||
47 | return redundancyModel.save({ transaction: t }) | |
48 | } | |
49 | ||
50 | function cacheFileActivityObjectToDBAttributes (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId) { | |
51 | ||
52 | if (cacheFileObject.url.mediaType === 'application/x-mpegURL') { | |
53 | const url = cacheFileObject.url | |
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 | } | |
83 | } |