2018-09-24 06:07:33 -05:00
|
|
|
import { Transaction } from 'sequelize'
|
2020-06-18 03:45:25 -05:00
|
|
|
import { MActorId, MVideoRedundancy, MVideoWithAllFiles } from '@server/types/models'
|
2021-12-24 03:14:47 -06:00
|
|
|
import { CacheFileObject, VideoStreamingPlaylistType } from '@shared/models'
|
2021-06-03 07:30:09 -05:00
|
|
|
import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy'
|
2018-09-11 09:27:07 -05:00
|
|
|
|
2021-06-03 07:30:09 -05:00
|
|
|
async function createOrUpdateCacheFile (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId, t: Transaction) {
|
|
|
|
const redundancyModel = await VideoRedundancyModel.loadByUrl(cacheFileObject.id, t)
|
2019-01-29 01:37:25 -06:00
|
|
|
|
2021-06-03 07:30:09 -05:00
|
|
|
if (redundancyModel) {
|
|
|
|
return updateCacheFile(cacheFileObject, redundancyModel, video, byActor, t)
|
2019-01-29 01:37:25 -06:00
|
|
|
}
|
|
|
|
|
2021-06-03 07:30:09 -05:00
|
|
|
return createCacheFile(cacheFileObject, video, byActor, t)
|
2018-09-11 09:27:07 -05:00
|
|
|
}
|
|
|
|
|
2021-06-03 07:30:09 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
2018-10-02 07:39:35 -05:00
|
|
|
|
2021-06-03 07:30:09 -05:00
|
|
|
export {
|
|
|
|
createOrUpdateCacheFile
|
2018-10-02 07:39:35 -05:00
|
|
|
}
|
|
|
|
|
2021-06-03 07:30:09 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2019-08-15 04:53:26 -05:00
|
|
|
function createCacheFile (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId, t: Transaction) {
|
2018-09-24 06:07:33 -05:00
|
|
|
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,
|
2019-08-15 04:53:26 -05:00
|
|
|
redundancyModel: MVideoRedundancy,
|
|
|
|
video: MVideoWithAllFiles,
|
|
|
|
byActor: MActorId,
|
2018-09-24 06:07:33 -05:00
|
|
|
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
|
|
|
|
2019-04-18 04:28:17 -05:00
|
|
|
redundancyModel.expiresOn = attributes.expiresOn
|
|
|
|
redundancyModel.fileUrl = attributes.fileUrl
|
2018-09-11 09:27:07 -05:00
|
|
|
|
2018-09-24 06:07:33 -05:00
|
|
|
return redundancyModel.save({ transaction: t })
|
2018-09-11 09:27:07 -05:00
|
|
|
}
|
|
|
|
|
2021-06-03 07:30:09 -05:00
|
|
|
function cacheFileActivityObjectToDBAttributes (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId) {
|
|
|
|
|
|
|
|
if (cacheFileObject.url.mediaType === 'application/x-mpegURL') {
|
|
|
|
const url = cacheFileObject.url
|
|
|
|
|
|
|
|
const playlist = video.VideoStreamingPlaylists.find(t => t.type === VideoStreamingPlaylistType.HLS)
|
|
|
|
if (!playlist) throw new Error('Cannot find HLS playlist of video ' + video.url)
|
|
|
|
|
|
|
|
return {
|
|
|
|
expiresOn: cacheFileObject.expires ? new Date(cacheFileObject.expires) : null,
|
|
|
|
url: cacheFileObject.id,
|
|
|
|
fileUrl: url.href,
|
|
|
|
strategy: null,
|
|
|
|
videoStreamingPlaylistId: playlist.id,
|
|
|
|
actorId: byActor.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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: cacheFileObject.expires ? new Date(cacheFileObject.expires) : null,
|
|
|
|
url: cacheFileObject.id,
|
|
|
|
fileUrl: url.href,
|
|
|
|
strategy: null,
|
|
|
|
videoFileId: videoFile.id,
|
|
|
|
actorId: byActor.id
|
|
|
|
}
|
2018-09-11 09:27:07 -05:00
|
|
|
}
|