]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/video-streaming-playlist.ts
Merge branch 'release/4.0.0' into develop
[github/Chocobozzz/PeerTube.git] / server / models / video / video-streaming-playlist.ts
index d591a3134f08df6d133fc011ad261b492f8aed79..9957ffee3c3b3d1a4dea8bef5ed6338769ad615a 100644 (file)
@@ -1,12 +1,27 @@
-import * as memoizee from 'memoizee'
+import memoizee from 'memoizee'
 import { join } from 'path'
 import { Op } from 'sequelize'
-import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, HasMany, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
+import {
+  AllowNull,
+  BelongsTo,
+  Column,
+  CreatedAt,
+  DataType,
+  Default,
+  ForeignKey,
+  HasMany,
+  Is,
+  Model,
+  Table,
+  UpdatedAt
+} from 'sequelize-typescript'
+import { getHLSPublicFileUrl } from '@server/lib/object-storage'
 import { VideoFileModel } from '@server/models/video/video-file'
 import { MStreamingPlaylist, MVideo } from '@server/types/models'
-import { AttributesOnly } from '@shared/core-utils'
+import { sha1 } from '@shared/extra-utils'
+import { VideoStorage } from '@shared/models'
+import { AttributesOnly } from '@shared/typescript-utils'
 import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type'
-import { sha1 } from '../../helpers/core-utils'
 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
 import { isArrayOf } from '../../helpers/custom-validators/misc'
 import { isVideoFileInfoHashValid } from '../../helpers/custom-validators/videos'
@@ -81,6 +96,11 @@ export class VideoStreamingPlaylistModel extends Model<Partial<AttributesOnly<Vi
   @Column
   videoId: number
 
+  @AllowNull(false)
+  @Default(VideoStorage.FILE_SYSTEM)
+  @Column
+  storage: VideoStorage
+
   @BelongsTo(() => VideoModel, {
     foreignKey: {
       allowNull: false
@@ -178,6 +198,15 @@ export class VideoStreamingPlaylistModel extends Model<Partial<AttributesOnly<Vi
     return Object.assign(playlist, { videoId: video.id, Video: video })
   }
 
+  static doesOwnedHLSPlaylistExist (videoUUID: string) {
+    const query = `SELECT 1 FROM "videoStreamingPlaylist" ` +
+      `INNER JOIN "video" ON "video"."id" = "videoStreamingPlaylist"."videoId" ` +
+      `AND "video"."remote" IS FALSE AND "video"."uuid" = $videoUUID ` +
+      `AND "storage" = ${VideoStorage.FILE_SYSTEM} LIMIT 1`
+
+    return doesExist(query, { videoUUID })
+  }
+
   assignP2PMediaLoaderInfoHashes (video: MVideo, files: unknown[]) {
     const masterPlaylistUrl = this.getMasterPlaylistUrl(video)
 
@@ -185,12 +214,20 @@ export class VideoStreamingPlaylistModel extends Model<Partial<AttributesOnly<Vi
   }
 
   getMasterPlaylistUrl (video: MVideo) {
+    if (this.storage === VideoStorage.OBJECT_STORAGE) {
+      return getHLSPublicFileUrl(this.playlistUrl)
+    }
+
     if (video.isOwned()) return WEBSERVER.URL + this.getMasterPlaylistStaticPath(video.uuid)
 
     return this.playlistUrl
   }
 
   getSha256SegmentsUrl (video: MVideo) {
+    if (this.storage === VideoStorage.OBJECT_STORAGE) {
+      return getHLSPublicFileUrl(this.segmentsSha256Url)
+    }
+
     if (video.isOwned()) return WEBSERVER.URL + this.getSha256SegmentsStaticPath(video.uuid, video.isLive)
 
     return this.segmentsSha256Url
@@ -211,6 +248,10 @@ export class VideoStreamingPlaylistModel extends Model<Partial<AttributesOnly<Vi
       this.videoId === other.videoId
   }
 
+  withVideo (video: MVideo) {
+    return Object.assign(this, { Video: video })
+  }
+
   private getMasterPlaylistStaticPath (videoUUID: string) {
     return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, this.playlistFilename)
   }