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