2018-09-11 09:27:07 -05:00
|
|
|
import { CacheFileObject } from '../../../shared/index'
|
|
|
|
import { VideoModel } from '../../models/video/video'
|
|
|
|
import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy'
|
2018-09-24 06:07:33 -05:00
|
|
|
import { Transaction } from 'sequelize'
|
2018-09-11 09:27:07 -05:00
|
|
|
|
2018-09-19 07:44:20 -05:00
|
|
|
function cacheFileActivityObjectToDBAttributes (cacheFileObject: CacheFileObject, video: VideoModel, byActor: { id?: number }) {
|
2018-09-11 09:27:07 -05:00
|
|
|
const url = cacheFileObject.url
|
|
|
|
|
|
|
|
const videoFile = video.VideoFiles.find(f => {
|
|
|
|
return f.resolution === url.height && f.fps === url.fps
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!videoFile) throw new Error(`Cannot find video file ${url.height} ${url.fps} of video ${video.url}`)
|
|
|
|
|
|
|
|
return {
|
|
|
|
expiresOn: new Date(cacheFileObject.expires),
|
|
|
|
url: cacheFileObject.id,
|
|
|
|
fileUrl: cacheFileObject.url.href,
|
|
|
|
strategy: null,
|
|
|
|
videoFileId: videoFile.id,
|
|
|
|
actorId: byActor.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-24 06:07:33 -05:00
|
|
|
function createCacheFile (cacheFileObject: CacheFileObject, video: VideoModel, byActor: { id?: number }, t: Transaction) {
|
|
|
|
const attributes = cacheFileActivityObjectToDBAttributes(cacheFileObject, video, byActor)
|
2018-09-11 09:27:07 -05:00
|
|
|
|
2018-09-24 06:07:33 -05:00
|
|
|
return VideoRedundancyModel.create(attributes, { transaction: t })
|
2018-09-11 09:27:07 -05:00
|
|
|
}
|
|
|
|
|
2018-09-24 06:07:33 -05:00
|
|
|
function updateCacheFile (
|
|
|
|
cacheFileObject: CacheFileObject,
|
|
|
|
redundancyModel: VideoRedundancyModel,
|
|
|
|
video: VideoModel,
|
|
|
|
byActor: { id?: number },
|
|
|
|
t: Transaction
|
|
|
|
) {
|
2018-09-19 08:47:55 -05:00
|
|
|
if (redundancyModel.actorId !== byActor.id) {
|
|
|
|
throw new Error('Cannot update redundancy ' + redundancyModel.url + ' of another actor.')
|
|
|
|
}
|
|
|
|
|
2018-09-24 06:07:33 -05:00
|
|
|
const attributes = cacheFileActivityObjectToDBAttributes(cacheFileObject, video, byActor)
|
2018-09-11 09:27:07 -05:00
|
|
|
|
|
|
|
redundancyModel.set('expires', attributes.expiresOn)
|
|
|
|
redundancyModel.set('fileUrl', attributes.fileUrl)
|
|
|
|
|
2018-09-24 06:07:33 -05:00
|
|
|
return redundancyModel.save({ transaction: t })
|
2018-09-11 09:27:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
createCacheFile,
|
|
|
|
updateCacheFile,
|
|
|
|
cacheFileActivityObjectToDBAttributes
|
|
|
|
}
|