]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/cache-file.ts
Refactor requests
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / cache-file.ts
CommitLineData
e5565833 1import { Transaction } from 'sequelize'
26d6bf65 2import { MActorId, MVideoRedundancy, MVideoWithAllFiles } from '@server/types/models'
49af5ac8
C
3import { CacheFileObject } from '../../../shared/index'
4import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type'
5import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy'
c48e82b5 6
49af5ac8
C
7async function createOrUpdateCacheFile (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId, t: Transaction) {
8 const redundancyModel = await VideoRedundancyModel.loadByUrl(cacheFileObject.id, t)
09209296 9
49af5ac8
C
10 if (redundancyModel) {
11 return updateCacheFile(cacheFileObject, redundancyModel, video, byActor, t)
09209296
C
12 }
13
49af5ac8 14 return createCacheFile(cacheFileObject, video, byActor, t)
c48e82b5
C
15}
16
49af5ac8 17// ---------------------------------------------------------------------------
b88a4596 18
49af5ac8
C
19export {
20 createOrUpdateCacheFile
b88a4596
C
21}
22
49af5ac8
C
23// ---------------------------------------------------------------------------
24
453e83ea 25function createCacheFile (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId, t: Transaction) {
e5565833 26 const attributes = cacheFileActivityObjectToDBAttributes(cacheFileObject, video, byActor)
c48e82b5 27
e5565833 28 return VideoRedundancyModel.create(attributes, { transaction: t })
c48e82b5
C
29}
30
e5565833
C
31function updateCacheFile (
32 cacheFileObject: CacheFileObject,
453e83ea
C
33 redundancyModel: MVideoRedundancy,
34 video: MVideoWithAllFiles,
35 byActor: MActorId,
e5565833
C
36 t: Transaction
37) {
12ba460e
C
38 if (redundancyModel.actorId !== byActor.id) {
39 throw new Error('Cannot update redundancy ' + redundancyModel.url + ' of another actor.')
40 }
41
e5565833 42 const attributes = cacheFileActivityObjectToDBAttributes(cacheFileObject, video, byActor)
c48e82b5 43
1735c825
C
44 redundancyModel.expiresOn = attributes.expiresOn
45 redundancyModel.fileUrl = attributes.fileUrl
c48e82b5 46
e5565833 47 return redundancyModel.save({ transaction: t })
c48e82b5
C
48}
49
49af5ac8
C
50function 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 }
c48e82b5 83}