cache-file.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { Transaction } from 'sequelize'
  2. import { MActorId, MVideoRedundancy, MVideoWithAllFiles } from '@server/types/models/index.js'
  3. import { CacheFileObject, VideoStreamingPlaylistType } from '@peertube/peertube-models'
  4. import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy.js'
  5. import { exists } from '@server/helpers/custom-validators/misc.js'
  6. async function createOrUpdateCacheFile (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId, t: Transaction) {
  7. const redundancyModel = await VideoRedundancyModel.loadByUrl(cacheFileObject.id, t)
  8. if (redundancyModel) {
  9. return updateCacheFile(cacheFileObject, redundancyModel, video, byActor, t)
  10. }
  11. return createCacheFile(cacheFileObject, video, byActor, t)
  12. }
  13. // ---------------------------------------------------------------------------
  14. export {
  15. createOrUpdateCacheFile
  16. }
  17. // ---------------------------------------------------------------------------
  18. function createCacheFile (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId, t: Transaction) {
  19. const attributes = cacheFileActivityObjectToDBAttributes(cacheFileObject, video, byActor)
  20. return VideoRedundancyModel.create(attributes, { transaction: t })
  21. }
  22. function updateCacheFile (
  23. cacheFileObject: CacheFileObject,
  24. redundancyModel: MVideoRedundancy,
  25. video: MVideoWithAllFiles,
  26. byActor: MActorId,
  27. t: Transaction
  28. ) {
  29. if (redundancyModel.actorId !== byActor.id) {
  30. throw new Error('Cannot update redundancy ' + redundancyModel.url + ' of another actor.')
  31. }
  32. const attributes = cacheFileActivityObjectToDBAttributes(cacheFileObject, video, byActor)
  33. redundancyModel.expiresOn = attributes.expiresOn
  34. redundancyModel.fileUrl = attributes.fileUrl
  35. return redundancyModel.save({ transaction: t })
  36. }
  37. function cacheFileActivityObjectToDBAttributes (cacheFileObject: CacheFileObject, video: MVideoWithAllFiles, byActor: MActorId) {
  38. if (cacheFileObject.url.mediaType === 'application/x-mpegURL') {
  39. const url = cacheFileObject.url
  40. const playlist = video.VideoStreamingPlaylists.find(t => t.type === VideoStreamingPlaylistType.HLS)
  41. if (!playlist) throw new Error('Cannot find HLS playlist of video ' + video.url)
  42. return {
  43. expiresOn: cacheFileObject.expires ? new Date(cacheFileObject.expires) : null,
  44. url: cacheFileObject.id,
  45. fileUrl: url.href,
  46. strategy: null,
  47. videoStreamingPlaylistId: playlist.id,
  48. actorId: byActor.id
  49. }
  50. }
  51. const url = cacheFileObject.url
  52. const urlFPS = exists(url.fps) // TODO: compat with < 6.1, remove in 7.0
  53. ? url.fps
  54. : url['_:fps']
  55. const videoFile = video.VideoFiles.find(f => {
  56. return f.resolution === url.height && f.fps === urlFPS
  57. })
  58. if (!videoFile) throw new Error(`Cannot find video file ${url.height} ${urlFPS} of video ${video.url}`)
  59. return {
  60. expiresOn: cacheFileObject.expires ? new Date(cacheFileObject.expires) : null,
  61. url: cacheFileObject.id,
  62. fileUrl: url.href,
  63. strategy: null,
  64. videoFileId: videoFile.id,
  65. actorId: byActor.id
  66. }
  67. }