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