1 import { remove } from 'fs-extra'
2 import * as memoizee from 'memoizee'
3 import { join } from 'path'
4 import { FindOptions, Op, QueryTypes, Transaction } from 'sequelize'
20 } from 'sequelize-typescript'
21 import { Where } from 'sequelize/types/lib/utils'
22 import validator from 'validator'
23 import { buildRemoteVideoBaseUrl } from '@server/helpers/activitypub'
24 import { logger } from '@server/helpers/logger'
25 import { extractVideo } from '@server/helpers/video'
26 import { getTorrentFilePath } from '@server/lib/video-paths'
27 import { MStreamingPlaylistVideo, MVideo, MVideoWithHost } from '@server/types/models'
29 isVideoFileExtnameValid,
30 isVideoFileInfoHashValid,
31 isVideoFileResolutionValid,
33 isVideoFPSResolutionValid
34 } from '../../helpers/custom-validators/videos'
40 STATIC_DOWNLOAD_PATHS,
43 } from '../../initializers/constants'
44 import { MVideoFile, MVideoFileStreamingPlaylistVideo, MVideoFileVideo } from '../../types/models/video/video-file'
45 import { VideoRedundancyModel } from '../redundancy/video-redundancy'
46 import { parseAggregateResult, throwIfNotValid } from '../utils'
47 import { VideoModel } from './video'
48 import { VideoStreamingPlaylistModel } from './video-streaming-playlist'
50 export enum ScopeNames {
51 WITH_VIDEO = 'WITH_VIDEO',
52 WITH_METADATA = 'WITH_METADATA',
53 WITH_VIDEO_OR_PLAYLIST = 'WITH_VIDEO_OR_PLAYLIST'
56 @DefaultScope(() => ({
58 exclude: [ 'metadata' ]
62 [ScopeNames.WITH_VIDEO]: {
65 model: VideoModel.unscoped(),
70 [ScopeNames.WITH_VIDEO_OR_PLAYLIST]: (options: { whereVideo?: Where } = {}) => {
74 model: VideoModel.unscoped(),
76 where: options.whereVideo
79 model: VideoStreamingPlaylistModel.unscoped(),
83 model: VideoModel.unscoped(),
85 where: options.whereVideo
92 [ScopeNames.WITH_METADATA]: {
94 include: [ 'metadata' ]
99 tableName: 'videoFile',
102 fields: [ 'videoId' ],
110 fields: [ 'videoStreamingPlaylistId' ],
112 videoStreamingPlaylistId: {
119 fields: [ 'infoHash' ]
123 fields: [ 'torrentFilename' ],
128 fields: [ 'filename' ],
133 fields: [ 'videoId', 'resolution', 'fps' ],
142 fields: [ 'videoStreamingPlaylistId', 'resolution', 'fps' ],
145 videoStreamingPlaylistId: {
152 export class VideoFileModel extends Model {
160 @Is('VideoFileResolution', value => throwIfNotValid(value, isVideoFileResolutionValid, 'resolution'))
165 @Is('VideoFileSize', value => throwIfNotValid(value, isVideoFileSizeValid, 'size'))
166 @Column(DataType.BIGINT)
170 @Is('VideoFileExtname', value => throwIfNotValid(value, isVideoFileExtnameValid, 'extname'))
175 @Is('VideoFileInfohash', value => throwIfNotValid(value, isVideoFileInfoHashValid, 'info hash', true))
181 @Is('VideoFileFPS', value => throwIfNotValid(value, isVideoFPSResolutionValid, 'fps'))
186 @Column(DataType.JSONB)
197 // Could be null for live files
206 // Could be null for live files
209 torrentFilename: string
211 @ForeignKey(() => VideoModel)
215 @BelongsTo(() => VideoModel, {
223 @ForeignKey(() => VideoStreamingPlaylistModel)
225 videoStreamingPlaylistId: number
227 @BelongsTo(() => VideoStreamingPlaylistModel, {
233 VideoStreamingPlaylist: VideoStreamingPlaylistModel
235 @HasMany(() => VideoRedundancyModel, {
242 RedundancyVideos: VideoRedundancyModel[]
244 static doesInfohashExistCached = memoizee(VideoFileModel.doesInfohashExist, {
246 max: MEMOIZE_LENGTH.INFO_HASH_EXISTS,
247 maxAge: MEMOIZE_TTL.INFO_HASH_EXISTS
250 static doesInfohashExist (infoHash: string) {
251 const query = 'SELECT 1 FROM "videoFile" WHERE "infoHash" = $infoHash LIMIT 1'
253 type: QueryTypes.SELECT as QueryTypes.SELECT,
258 return VideoModel.sequelize.query(query, options)
259 .then(results => results.length === 1)
262 static async doesVideoExistForVideoFile (id: number, videoIdOrUUID: number | string) {
263 const videoFile = await VideoFileModel.loadWithVideoOrPlaylist(id, videoIdOrUUID)
268 static loadWithVideoOrPlaylistByTorrentFilename (filename: string) {
271 torrentFilename: filename
275 return VideoFileModel.scope(ScopeNames.WITH_VIDEO_OR_PLAYLIST).findOne(query)
278 static loadWithMetadata (id: number) {
279 return VideoFileModel.scope(ScopeNames.WITH_METADATA).findByPk(id)
282 static loadWithVideo (id: number) {
283 return VideoFileModel.scope(ScopeNames.WITH_VIDEO).findByPk(id)
286 static loadWithVideoOrPlaylist (id: number, videoIdOrUUID: number | string) {
287 const whereVideo = validator.isUUID(videoIdOrUUID + '')
288 ? { uuid: videoIdOrUUID }
289 : { id: videoIdOrUUID }
297 return VideoFileModel.scope({ method: [ ScopeNames.WITH_VIDEO_OR_PLAYLIST, whereVideo ] })
300 // We used `required: false` so check we have at least a video or a streaming playlist
301 if (!file.Video && !file.VideoStreamingPlaylist) return null
307 static listByStreamingPlaylist (streamingPlaylistId: number, transaction: Transaction) {
311 model: VideoModel.unscoped(),
315 model: VideoStreamingPlaylistModel.unscoped(),
318 id: streamingPlaylistId
327 return VideoFileModel.findAll(query)
331 const webtorrentFilesQuery: FindOptions = {
336 model: VideoModel.unscoped(),
344 const hlsFilesQuery: FindOptions = {
349 model: VideoStreamingPlaylistModel.unscoped(),
353 model: VideoModel.unscoped(),
365 VideoFileModel.aggregate('size', 'SUM', webtorrentFilesQuery),
366 VideoFileModel.aggregate('size', 'SUM', hlsFilesQuery)
367 ]).then(([ webtorrentResult, hlsResult ]) => ({
368 totalLocalVideoFilesSize: parseAggregateResult(webtorrentResult) + parseAggregateResult(hlsResult)
372 // Redefine upsert because sequelize does not use an appropriate where clause in the update query with 2 unique indexes
373 static async customUpsert (
374 videoFile: MVideoFile,
375 mode: 'streaming-playlist' | 'video',
376 transaction: Transaction
380 resolution: videoFile.resolution
383 if (mode === 'streaming-playlist') Object.assign(baseWhere, { videoStreamingPlaylistId: videoFile.videoStreamingPlaylistId })
384 else Object.assign(baseWhere, { videoId: videoFile.videoId })
386 const element = await VideoFileModel.findOne({ where: baseWhere, transaction })
387 if (!element) return videoFile.save({ transaction })
389 for (const k of Object.keys(videoFile.toJSON())) {
390 element[k] = videoFile[k]
393 return element.save({ transaction })
396 static removeHLSFilesOfVideoId (videoStreamingPlaylistId: number) {
398 where: { videoStreamingPlaylistId }
401 return VideoFileModel.destroy(options)
404 getVideoOrStreamingPlaylist (this: MVideoFileVideo | MVideoFileStreamingPlaylistVideo): MVideo | MStreamingPlaylistVideo {
405 if (this.videoId) return (this as MVideoFileVideo).Video
407 return (this as MVideoFileStreamingPlaylistVideo).VideoStreamingPlaylist
410 getVideo (this: MVideoFileVideo | MVideoFileStreamingPlaylistVideo): MVideo {
411 return extractVideo(this.getVideoOrStreamingPlaylist())
415 return !!MIMETYPES.AUDIO.EXT_MIMETYPE[this.extname]
419 return this.size === -1
423 return !!this.videoStreamingPlaylistId
426 getFileUrl (video: MVideoWithHost) {
427 if (!this.Video) this.Video = video as VideoModel
429 if (video.isOwned()) return WEBSERVER.URL + this.getFileStaticPath(video)
430 if (this.fileUrl) return this.fileUrl
432 // Fallback if we don't have a file URL
433 return buildRemoteVideoBaseUrl(video, this.getFileStaticPath(video))
436 getFileStaticPath (video: MVideo) {
437 if (this.isHLS()) return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, video.uuid, this.filename)
439 return join(STATIC_PATHS.WEBSEED, this.filename)
442 getFileDownloadUrl (video: MVideoWithHost) {
443 const basePath = this.isHLS()
444 ? STATIC_DOWNLOAD_PATHS.HLS_VIDEOS
445 : STATIC_DOWNLOAD_PATHS.VIDEOS
446 const path = join(basePath, this.filename)
448 if (video.isOwned()) return WEBSERVER.URL + path
450 // FIXME: don't guess remote URL
451 return buildRemoteVideoBaseUrl(video, path)
454 getRemoteTorrentUrl (video: MVideoWithHost) {
455 if (video.isOwned()) throw new Error(`Video ${video.url} is not a remote video`)
457 if (this.torrentUrl) return this.torrentUrl
459 // Fallback if we don't have a torrent URL
460 return buildRemoteVideoBaseUrl(video, this.getTorrentStaticPath())
463 // We proxify torrent requests so use a local URL
465 return WEBSERVER.URL + this.getTorrentStaticPath()
468 getTorrentStaticPath () {
469 return join(LAZY_STATIC_PATHS.TORRENTS, this.torrentFilename)
472 getTorrentDownloadUrl () {
473 return WEBSERVER.URL + join(STATIC_DOWNLOAD_PATHS.TORRENTS, this.torrentFilename)
477 const torrentPath = getTorrentFilePath(this)
478 return remove(torrentPath)
479 .catch(err => logger.warn('Cannot delete torrent %s.', torrentPath, { err }))
482 hasSameUniqueKeysThan (other: MVideoFile) {
483 return this.fps === other.fps &&
484 this.resolution === other.resolution &&
486 (this.videoId !== null && this.videoId === other.videoId) ||
487 (this.videoStreamingPlaylistId !== null && this.videoStreamingPlaylistId === other.videoStreamingPlaylistId)