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