]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-streaming-playlist.ts
faad4cc2d511a62e82012f6943d7fc04adfe3600
[github/Chocobozzz/PeerTube.git] / server / models / video / video-streaming-playlist.ts
1 import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, HasMany, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
2 import { isVideoFileInfoHashValid } from '../../helpers/custom-validators/videos'
3 import { throwIfNotValid } from '../utils'
4 import { VideoModel } from './video'
5 import { VideoRedundancyModel } from '../redundancy/video-redundancy'
6 import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type'
7 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
8 import { CONSTRAINTS_FIELDS, P2P_MEDIA_LOADER_PEER_VERSION, STATIC_DOWNLOAD_PATHS, STATIC_PATHS } from '../../initializers/constants'
9 import { join } from 'path'
10 import { sha1 } from '../../helpers/core-utils'
11 import { isArrayOf } from '../../helpers/custom-validators/misc'
12 import { Op, QueryTypes } from 'sequelize'
13 import { MStreamingPlaylist, MVideoFile } from '@server/typings/models'
14 import { VideoFileModel } from '@server/models/video/video-file'
15 import { getTorrentFileName, getVideoFilename } from '@server/lib/video-paths'
16
17 @Table({
18 tableName: 'videoStreamingPlaylist',
19 indexes: [
20 {
21 fields: [ 'videoId' ]
22 },
23 {
24 fields: [ 'videoId', 'type' ],
25 unique: true
26 },
27 {
28 fields: [ 'p2pMediaLoaderInfohashes' ],
29 using: 'gin'
30 }
31 ]
32 })
33 export class VideoStreamingPlaylistModel extends Model<VideoStreamingPlaylistModel> {
34 @CreatedAt
35 createdAt: Date
36
37 @UpdatedAt
38 updatedAt: Date
39
40 @AllowNull(false)
41 @Column
42 type: VideoStreamingPlaylistType
43
44 @AllowNull(false)
45 @Is('PlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'playlist url'))
46 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
47 playlistUrl: string
48
49 @AllowNull(false)
50 @Is('VideoStreamingPlaylistInfoHashes', value => throwIfNotValid(value, v => isArrayOf(v, isVideoFileInfoHashValid), 'info hashes'))
51 @Column(DataType.ARRAY(DataType.STRING))
52 p2pMediaLoaderInfohashes: string[]
53
54 @AllowNull(false)
55 @Column
56 p2pMediaLoaderPeerVersion: number
57
58 @AllowNull(false)
59 @Is('VideoStreamingSegmentsSha256Url', value => throwIfNotValid(value, isActivityPubUrlValid, 'segments sha256 url'))
60 @Column
61 segmentsSha256Url: string
62
63 @ForeignKey(() => VideoModel)
64 @Column
65 videoId: number
66
67 @BelongsTo(() => VideoModel, {
68 foreignKey: {
69 allowNull: false
70 },
71 onDelete: 'CASCADE'
72 })
73 Video: VideoModel
74
75 @HasMany(() => VideoFileModel, {
76 foreignKey: {
77 allowNull: true
78 },
79 onDelete: 'CASCADE'
80 })
81 VideoFiles: VideoFileModel[]
82
83 @HasMany(() => VideoRedundancyModel, {
84 foreignKey: {
85 allowNull: false
86 },
87 onDelete: 'CASCADE',
88 hooks: true
89 })
90 RedundancyVideos: VideoRedundancyModel[]
91
92 static doesInfohashExist (infoHash: string) {
93 const query = 'SELECT 1 FROM "videoStreamingPlaylist" WHERE $infoHash = ANY("p2pMediaLoaderInfohashes") LIMIT 1'
94 const options = {
95 type: QueryTypes.SELECT as QueryTypes.SELECT,
96 bind: { infoHash },
97 raw: true
98 }
99
100 return VideoModel.sequelize.query<object>(query, options)
101 .then(results => results.length === 1)
102 }
103
104 static buildP2PMediaLoaderInfoHashes (playlistUrl: string, files: unknown[]) {
105 const hashes: string[] = []
106
107 // https://github.com/Novage/p2p-media-loader/blob/master/p2p-media-loader-core/lib/p2p-media-manager.ts#L115
108 for (let i = 0; i < files.length; i++) {
109 hashes.push(sha1(`${P2P_MEDIA_LOADER_PEER_VERSION}${playlistUrl}+V${i}`))
110 }
111
112 return hashes
113 }
114
115 static listByIncorrectPeerVersion () {
116 const query = {
117 where: {
118 p2pMediaLoaderPeerVersion: {
119 [Op.ne]: P2P_MEDIA_LOADER_PEER_VERSION
120 }
121 }
122 }
123
124 return VideoStreamingPlaylistModel.findAll(query)
125 }
126
127 static loadWithVideo (id: number) {
128 const options = {
129 include: [
130 {
131 model: VideoModel.unscoped(),
132 required: true
133 }
134 ]
135 }
136
137 return VideoStreamingPlaylistModel.findByPk(id, options)
138 }
139
140 static getHlsPlaylistFilename (resolution: number) {
141 return resolution + '.m3u8'
142 }
143
144 static getMasterHlsPlaylistFilename () {
145 return 'master.m3u8'
146 }
147
148 static getHlsSha256SegmentsFilename () {
149 return 'segments-sha256.json'
150 }
151
152 static getHlsMasterPlaylistStaticPath (videoUUID: string) {
153 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getMasterHlsPlaylistFilename())
154 }
155
156 static getHlsPlaylistStaticPath (videoUUID: string, resolution: number) {
157 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution))
158 }
159
160 static getHlsSha256SegmentsStaticPath (videoUUID: string) {
161 return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getHlsSha256SegmentsFilename())
162 }
163
164 getStringType () {
165 if (this.type === VideoStreamingPlaylistType.HLS) return 'hls'
166
167 return 'unknown'
168 }
169
170 getVideoRedundancyUrl (baseUrlHttp: string) {
171 return baseUrlHttp + STATIC_PATHS.REDUNDANCY + this.getStringType() + '/' + this.Video.uuid
172 }
173
174 getTorrentDownloadUrl (videoFile: MVideoFile, baseUrlHttp: string) {
175 return baseUrlHttp + STATIC_DOWNLOAD_PATHS.TORRENTS + getTorrentFileName(this, videoFile)
176 }
177
178 getVideoFileDownloadUrl (videoFile: MVideoFile, baseUrlHttp: string) {
179 return baseUrlHttp + STATIC_DOWNLOAD_PATHS.HLS_VIDEOS + getVideoFilename(this, videoFile)
180 }
181
182 getVideoFileUrl (videoFile: MVideoFile, baseUrlHttp: string) {
183 return baseUrlHttp + join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, this.Video.uuid, getVideoFilename(this, videoFile))
184 }
185
186 getTorrentUrl (videoFile: MVideoFile, baseUrlHttp: string) {
187 return baseUrlHttp + join(STATIC_PATHS.TORRENTS, getTorrentFileName(this, videoFile))
188 }
189
190 getTrackerUrls (baseUrlHttp: string, baseUrlWs: string) {
191 return [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
192 }
193
194 hasSameUniqueKeysThan (other: MStreamingPlaylist) {
195 return this.type === other.type &&
196 this.videoId === other.videoId
197 }
198 }