]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/cache-file.ts
Try to improve redundancy tests
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / cache-file.ts
CommitLineData
c48e82b5
C
1import { CacheFileObject } from '../../../shared/index'
2import { VideoModel } from '../../models/video/video'
c48e82b5 3import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy'
e5565833 4import { Transaction } from 'sequelize'
c48e82b5 5
e587e0ec 6function cacheFileActivityObjectToDBAttributes (cacheFileObject: CacheFileObject, video: VideoModel, byActor: { id?: number }) {
c48e82b5
C
7 const url = cacheFileObject.url
8
9 const videoFile = video.VideoFiles.find(f => {
10 return f.resolution === url.height && f.fps === url.fps
11 })
12
13 if (!videoFile) throw new Error(`Cannot find video file ${url.height} ${url.fps} of video ${video.url}`)
14
15 return {
16 expiresOn: new Date(cacheFileObject.expires),
17 url: cacheFileObject.id,
18 fileUrl: cacheFileObject.url.href,
19 strategy: null,
20 videoFileId: videoFile.id,
21 actorId: byActor.id
22 }
23}
24
e5565833
C
25function createCacheFile (cacheFileObject: CacheFileObject, video: VideoModel, byActor: { id?: number }, t: Transaction) {
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,
33 redundancyModel: VideoRedundancyModel,
34 video: VideoModel,
35 byActor: { id?: number },
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
C
43
44 redundancyModel.set('expires', attributes.expiresOn)
45 redundancyModel.set('fileUrl', attributes.fileUrl)
46
e5565833 47 return redundancyModel.save({ transaction: t })
c48e82b5
C
48}
49
50export {
51 createCacheFile,
52 updateCacheFile,
53 cacheFileActivityObjectToDBAttributes
54}