1
0
Fork 0
peertube/server/lib/activitypub/cache-file.ts

83 lines
2.8 KiB
TypeScript
Raw Normal View History

2019-02-26 09:55:40 +00:00
import { CacheFileObject } from '../../../shared/index'
2018-09-11 14:27:07 +00:00
import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy'
import { Transaction } from 'sequelize'
2019-01-29 07:37:25 +00:00
import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type'
2019-08-15 09:53:26 +00:00
import { MActorId, MVideoRedundancy, MVideoWithAllFiles } from '@server/typings/models'
2018-09-11 14:27:07 +00:00
2019-08-15 09:53:26 +00:00
function cacheFileActivityObjectToDBAttributes (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId) {
2018-09-11 14:27:07 +00:00
2019-01-29 07:37:25 +00:00
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 {
2020-01-10 09:11:28 +00:00
expiresOn: cacheFileObject.expires ? new Date(cacheFileObject.expires) : null,
2019-01-29 07:37:25 +00:00
url: cacheFileObject.id,
fileUrl: url.href,
strategy: null,
videoStreamingPlaylistId: playlist.id,
actorId: byActor.id
}
}
const url = cacheFileObject.url
2018-09-11 14:27:07 +00:00
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 {
2020-01-10 09:11:28 +00:00
expiresOn: cacheFileObject.expires ? new Date(cacheFileObject.expires) : null,
2018-09-11 14:27:07 +00:00
url: cacheFileObject.id,
2019-01-29 07:37:25 +00:00
fileUrl: url.href,
2018-09-11 14:27:07 +00:00
strategy: null,
videoFileId: videoFile.id,
actorId: byActor.id
}
}
2019-08-15 09:53:26 +00:00
async function createOrUpdateCacheFile (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId, t: Transaction) {
2018-10-02 12:39:35 +00:00
const redundancyModel = await VideoRedundancyModel.loadByUrl(cacheFileObject.id, t)
if (!redundancyModel) {
await createCacheFile(cacheFileObject, video, byActor, t)
} else {
await updateCacheFile(cacheFileObject, redundancyModel, video, byActor, t)
}
}
2019-08-15 09:53:26 +00:00
function createCacheFile (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId, t: Transaction) {
const attributes = cacheFileActivityObjectToDBAttributes(cacheFileObject, video, byActor)
2018-09-11 14:27:07 +00:00
return VideoRedundancyModel.create(attributes, { transaction: t })
2018-09-11 14:27:07 +00:00
}
function updateCacheFile (
cacheFileObject: CacheFileObject,
2019-08-15 09:53:26 +00:00
redundancyModel: MVideoRedundancy,
video: MVideoWithAllFiles,
byActor: MActorId,
t: Transaction
) {
2018-09-19 13:47:55 +00:00
if (redundancyModel.actorId !== byActor.id) {
throw new Error('Cannot update redundancy ' + redundancyModel.url + ' of another actor.')
}
const attributes = cacheFileActivityObjectToDBAttributes(cacheFileObject, video, byActor)
2018-09-11 14:27:07 +00:00
2019-04-18 09:28:17 +00:00
redundancyModel.expiresOn = attributes.expiresOn
redundancyModel.fileUrl = attributes.fileUrl
2018-09-11 14:27:07 +00:00
return redundancyModel.save({ transaction: t })
2018-09-11 14:27:07 +00:00
}
export {
2018-10-02 12:39:35 +00:00
createOrUpdateCacheFile,
2018-09-11 14:27:07 +00:00
createCacheFile,
updateCacheFile,
cacheFileActivityObjectToDBAttributes
}