cache-file.ts 2.8 KB

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