Create a dedicated table to track video thumbnails
[oweals/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 * as Sequelize from 'sequelize'
6 import { VideoRedundancyModel } from '../redundancy/video-redundancy'
7 import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type'
8 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
9 import { CONSTRAINTS_FIELDS, STATIC_PATHS, P2P_MEDIA_LOADER_PEER_VERSION } from '../../initializers/constants'
10 import { VideoFileModel } from './video-file'
11 import { join } from 'path'
12 import { sha1 } from '../../helpers/core-utils'
13 import { isArrayOf } from '../../helpers/custom-validators/misc'
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     }
29   ]
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'))
49   @Column(DataType.ARRAY(DataType.STRING))
50   p2pMediaLoaderInfohashes: string[]
51
52   @AllowNull(false)
53   @Column
54   p2pMediaLoaderPeerVersion: number
55
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 = {
85       type: Sequelize.QueryTypes.SELECT,
86       bind: { infoHash },
87       raw: true
88     }
89
90     return VideoModel.sequelize.query(query, options)
91               .then(results => {
92                 return results.length === 1
93               })
94   }
95
96   static buildP2PMediaLoaderInfoHashes (playlistUrl: string, videoFiles: VideoFileModel[]) {
97     const hashes: string[] = []
98
99     // https://github.com/Novage/p2p-media-loader/blob/master/p2p-media-loader-core/lib/p2p-media-manager.ts#L115
100     for (let i = 0; i < videoFiles.length; i++) {
101       hashes.push(sha1(`${P2P_MEDIA_LOADER_PEER_VERSION}${playlistUrl}+V${i}`))
102     }
103
104     return hashes
105   }
106
107   static listByIncorrectPeerVersion () {
108     const query = {
109       where: {
110         p2pMediaLoaderPeerVersion: {
111           [Sequelize.Op.ne]: P2P_MEDIA_LOADER_PEER_VERSION
112         }
113       }
114     }
115
116     return VideoStreamingPlaylistModel.findAll(query)
117   }
118
119   static loadWithVideo (id: number) {
120     const options = {
121       include: [
122         {
123           model: VideoModel.unscoped(),
124           required: true
125         }
126       ]
127     }
128
129     return VideoStreamingPlaylistModel.findByPk(id, options)
130   }
131
132   static getHlsPlaylistFilename (resolution: number) {
133     return resolution + '.m3u8'
134   }
135
136   static getMasterHlsPlaylistFilename () {
137     return 'master.m3u8'
138   }
139
140   static getHlsSha256SegmentsFilename () {
141     return 'segments-sha256.json'
142   }
143
144   static getHlsVideoName (uuid: string, resolution: number) {
145     return `${uuid}-${resolution}-fragmented.mp4`
146   }
147
148   static getHlsMasterPlaylistStaticPath (videoUUID: string) {
149     return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getMasterHlsPlaylistFilename())
150   }
151
152   static getHlsPlaylistStaticPath (videoUUID: string, resolution: number) {
153     return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution))
154   }
155
156   static getHlsSha256SegmentsStaticPath (videoUUID: string) {
157     return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, videoUUID, VideoStreamingPlaylistModel.getHlsSha256SegmentsFilename())
158   }
159
160   getStringType () {
161     if (this.type === VideoStreamingPlaylistType.HLS) return 'hls'
162
163     return 'unknown'
164   }
165
166   getVideoRedundancyUrl (baseUrlHttp: string) {
167     return baseUrlHttp + STATIC_PATHS.REDUNDANCY + this.getStringType() + '/' + this.Video.uuid
168   }
169
170   hasSameUniqueKeysThan (other: VideoStreamingPlaylistModel) {
171     return this.type === other.type &&
172       this.videoId === other.videoId
173   }
174 }