]>
Commit | Line | Data |
---|---|---|
453e83ea | 1 | import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, HasMany, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' |
09209296 C |
2 | import { isVideoFileInfoHashValid } from '../../helpers/custom-validators/videos' |
3 | import { throwIfNotValid } from '../utils' | |
4 | import { VideoModel } from './video' | |
09209296 C |
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' | |
d7a25329 | 8 | import { CONSTRAINTS_FIELDS, P2P_MEDIA_LOADER_PEER_VERSION, STATIC_DOWNLOAD_PATHS, STATIC_PATHS } from '../../initializers/constants' |
09209296 C |
9 | import { join } from 'path' |
10 | import { sha1 } from '../../helpers/core-utils' | |
11 | import { isArrayOf } from '../../helpers/custom-validators/misc' | |
453e83ea C |
12 | import { Op, QueryTypes } from 'sequelize' |
13 | import { MStreamingPlaylist, MVideoFile } from '@server/typings/models' | |
d7a25329 C |
14 | import { VideoFileModel } from '@server/models/video/video-file' |
15 | import { getTorrentFileName, getVideoFilename } from '@server/lib/video-paths' | |
09209296 C |
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 | } | |
3acc5084 | 31 | ] |
09209296 C |
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')) | |
3acc5084 | 51 | @Column(DataType.ARRAY(DataType.STRING)) |
09209296 C |
52 | p2pMediaLoaderInfohashes: string[] |
53 | ||
ae9bbed4 C |
54 | @AllowNull(false) |
55 | @Column | |
56 | p2pMediaLoaderPeerVersion: number | |
57 | ||
09209296 C |
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 | ||
d7a25329 C |
75 | @HasMany(() => VideoFileModel, { |
76 | foreignKey: { | |
77 | allowNull: true | |
78 | }, | |
79 | onDelete: 'CASCADE' | |
80 | }) | |
81 | VideoFiles: VideoFileModel[] | |
82 | ||
09209296 C |
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 = { | |
1735c825 | 95 | type: QueryTypes.SELECT as QueryTypes.SELECT, |
09209296 C |
96 | bind: { infoHash }, |
97 | raw: true | |
98 | } | |
99 | ||
3acc5084 | 100 | return VideoModel.sequelize.query<object>(query, options) |
1735c825 | 101 | .then(results => results.length === 1) |
09209296 C |
102 | } |
103 | ||
d7a25329 | 104 | static buildP2PMediaLoaderInfoHashes (playlistUrl: string, files: unknown[]) { |
09209296 C |
105 | const hashes: string[] = [] |
106 | ||
ae9bbed4 | 107 | // https://github.com/Novage/p2p-media-loader/blob/master/p2p-media-loader-core/lib/p2p-media-manager.ts#L115 |
d7a25329 | 108 | for (let i = 0; i < files.length; i++) { |
ae9bbed4 | 109 | hashes.push(sha1(`${P2P_MEDIA_LOADER_PEER_VERSION}${playlistUrl}+V${i}`)) |
09209296 C |
110 | } |
111 | ||
112 | return hashes | |
113 | } | |
114 | ||
ae9bbed4 C |
115 | static listByIncorrectPeerVersion () { |
116 | const query = { | |
117 | where: { | |
118 | p2pMediaLoaderPeerVersion: { | |
1735c825 | 119 | [Op.ne]: P2P_MEDIA_LOADER_PEER_VERSION |
ae9bbed4 C |
120 | } |
121 | } | |
122 | } | |
123 | ||
124 | return VideoStreamingPlaylistModel.findAll(query) | |
125 | } | |
126 | ||
09209296 C |
127 | static loadWithVideo (id: number) { |
128 | const options = { | |
129 | include: [ | |
130 | { | |
131 | model: VideoModel.unscoped(), | |
132 | required: true | |
133 | } | |
134 | ] | |
135 | } | |
136 | ||
9b39106d | 137 | return VideoStreamingPlaylistModel.findByPk(id, options) |
09209296 C |
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) { | |
9c6ca37f | 153 | return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getMasterHlsPlaylistFilename()) |
09209296 C |
154 | } |
155 | ||
156 | static getHlsPlaylistStaticPath (videoUUID: string, resolution: number) { | |
9c6ca37f | 157 | return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution)) |
09209296 C |
158 | } |
159 | ||
160 | static getHlsSha256SegmentsStaticPath (videoUUID: string) { | |
9c6ca37f | 161 | return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getHlsSha256SegmentsFilename()) |
09209296 C |
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 | ||
d7a25329 C |
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 | ||
453e83ea | 194 | hasSameUniqueKeysThan (other: MStreamingPlaylist) { |
09209296 C |
195 | return this.type === other.type && |
196 | this.videoId === other.videoId | |
197 | } | |
198 | } |