1 import { remove } from 'fs-extra'
2 import memoizee from 'memoizee'
3 import { join } from 'path'
4 import { FindOptions, Op, 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 { getHLSPublicFileUrl, getWebTorrentPublicFileUrl } from '@server/lib/object-storage'
27 import { getFSTorrentFilePath } from '@server/lib/paths'
28 import { isStreamingPlaylist, MStreamingPlaylistVideo, MVideo, MVideoWithHost } from '@server/types/models'
29 import { AttributesOnly } from '@shared/typescript-utils'
30 import { VideoStorage } from '@shared/models'
32 isVideoFileExtnameValid,
33 isVideoFileInfoHashValid,
34 isVideoFileResolutionValid,
36 isVideoFPSResolutionValid
37 } from '../../helpers/custom-validators/videos'
43 STATIC_DOWNLOAD_PATHS,
46 } from '../../initializers/constants'
47 import { MVideoFile, MVideoFileStreamingPlaylistVideo, MVideoFileVideo } from '../../types/models/video/video-file'
48 import { VideoRedundancyModel } from '../redundancy/video-redundancy'
49 import { doesExist } from '../shared'
50 import { parseAggregateResult, throwIfNotValid } from '../utils'
51 import { VideoModel } from './video'
52 import { VideoStreamingPlaylistModel } from './video-streaming-playlist'
54 export enum ScopeNames {
55 WITH_VIDEO = 'WITH_VIDEO',
56 WITH_METADATA = 'WITH_METADATA',
57 WITH_VIDEO_OR_PLAYLIST = 'WITH_VIDEO_OR_PLAYLIST'
60 @DefaultScope(() => ({
62 exclude: [ 'metadata' ]
66 [ScopeNames.WITH_VIDEO]: {
69 model: VideoModel.unscoped(),
74 [ScopeNames.WITH_VIDEO_OR_PLAYLIST]: (options: { whereVideo?: Where } = {}) => {
78 model: VideoModel.unscoped(),
80 where: options.whereVideo
83 model: VideoStreamingPlaylistModel.unscoped(),
87 model: VideoModel.unscoped(),
89 where: options.whereVideo
96 [ScopeNames.WITH_METADATA]: {
98 include: [ 'metadata' ]
103 tableName: 'videoFile',
106 fields: [ 'videoId' ],
114 fields: [ 'videoStreamingPlaylistId' ],
116 videoStreamingPlaylistId: {
123 fields: [ 'infoHash' ]
127 fields: [ 'torrentFilename' ],
132 fields: [ 'filename' ],
137 fields: [ 'videoId', 'resolution', 'fps' ],
146 fields: [ 'videoStreamingPlaylistId', 'resolution', 'fps' ],
149 videoStreamingPlaylistId: {
156 export class VideoFileModel extends Model<Partial<AttributesOnly<VideoFileModel>>> {
164 @Is('VideoFileResolution', value => throwIfNotValid(value, isVideoFileResolutionValid, 'resolution'))
169 @Is('VideoFileSize', value => throwIfNotValid(value, isVideoFileSizeValid, 'size'))
170 @Column(DataType.BIGINT)
174 @Is('VideoFileExtname', value => throwIfNotValid(value, isVideoFileExtnameValid, 'extname'))
179 @Is('VideoFileInfohash', value => throwIfNotValid(value, isVideoFileInfoHashValid, 'info hash', true))
185 @Is('VideoFileFPS', value => throwIfNotValid(value, isVideoFPSResolutionValid, 'fps'))
190 @Column(DataType.JSONB)
197 // Could be null for remote files
202 // Could be null for live files
207 // Could be null for remote files
212 // Could be null for live files
215 torrentFilename: string
217 @ForeignKey(() => VideoModel)
222 @Default(VideoStorage.FILE_SYSTEM)
224 storage: VideoStorage
226 @BelongsTo(() => VideoModel, {
234 @ForeignKey(() => VideoStreamingPlaylistModel)
236 videoStreamingPlaylistId: number
238 @BelongsTo(() => VideoStreamingPlaylistModel, {
244 VideoStreamingPlaylist: VideoStreamingPlaylistModel
246 @HasMany(() => VideoRedundancyModel, {
253 RedundancyVideos: VideoRedundancyModel[]
255 static doesInfohashExistCached = memoizee(VideoFileModel.doesInfohashExist, {
257 max: MEMOIZE_LENGTH.INFO_HASH_EXISTS,
258 maxAge: MEMOIZE_TTL.INFO_HASH_EXISTS
261 static doesInfohashExist (infoHash: string) {
262 const query = 'SELECT 1 FROM "videoFile" WHERE "infoHash" = $infoHash LIMIT 1'
264 return doesExist(query, { infoHash })
267 static async doesVideoExistForVideoFile (id: number, videoIdOrUUID: number | string) {
268 const videoFile = await VideoFileModel.loadWithVideoOrPlaylist(id, videoIdOrUUID)
273 static async doesOwnedTorrentFileExist (filename: string) {
274 const query = 'SELECT 1 FROM "videoFile" ' +
275 'LEFT JOIN "video" "webtorrent" ON "webtorrent"."id" = "videoFile"."videoId" AND "webtorrent"."remote" IS FALSE ' +
276 'LEFT JOIN "videoStreamingPlaylist" ON "videoStreamingPlaylist"."id" = "videoFile"."videoStreamingPlaylistId" ' +
277 'LEFT JOIN "video" "hlsVideo" ON "hlsVideo"."id" = "videoStreamingPlaylist"."videoId" AND "hlsVideo"."remote" IS FALSE ' +
278 'WHERE "torrentFilename" = $filename AND ("hlsVideo"."id" IS NOT NULL OR "webtorrent"."id" IS NOT NULL) LIMIT 1'
280 return doesExist(query, { filename })
283 static async doesOwnedWebTorrentVideoFileExist (filename: string) {
284 const query = 'SELECT 1 FROM "videoFile" INNER JOIN "video" ON "video"."id" = "videoFile"."videoId" AND "video"."remote" IS FALSE ' +
285 `WHERE "filename" = $filename AND "storage" = ${VideoStorage.FILE_SYSTEM} LIMIT 1`
287 return doesExist(query, { filename })
290 static loadByFilename (filename: string) {
297 return VideoFileModel.findOne(query)
300 static loadWithVideoOrPlaylistByTorrentFilename (filename: string) {
303 torrentFilename: filename
307 return VideoFileModel.scope(ScopeNames.WITH_VIDEO_OR_PLAYLIST).findOne(query)
310 static loadWithMetadata (id: number) {
311 return VideoFileModel.scope(ScopeNames.WITH_METADATA).findByPk(id)
314 static loadWithVideo (id: number) {
315 return VideoFileModel.scope(ScopeNames.WITH_VIDEO).findByPk(id)
318 static loadWithVideoOrPlaylist (id: number, videoIdOrUUID: number | string) {
319 const whereVideo = validator.isUUID(videoIdOrUUID + '')
320 ? { uuid: videoIdOrUUID }
321 : { id: videoIdOrUUID }
329 return VideoFileModel.scope({ method: [ ScopeNames.WITH_VIDEO_OR_PLAYLIST, whereVideo ] })
332 // We used `required: false` so check we have at least a video or a streaming playlist
333 if (!file.Video && !file.VideoStreamingPlaylist) return null
339 static listByStreamingPlaylist (streamingPlaylistId: number, transaction: Transaction) {
343 model: VideoModel.unscoped(),
347 model: VideoStreamingPlaylistModel.unscoped(),
350 id: streamingPlaylistId
359 return VideoFileModel.findAll(query)
363 const webtorrentFilesQuery: FindOptions = {
368 model: VideoModel.unscoped(),
376 const hlsFilesQuery: FindOptions = {
381 model: VideoStreamingPlaylistModel.unscoped(),
385 model: VideoModel.unscoped(),
397 VideoFileModel.aggregate('size', 'SUM', webtorrentFilesQuery),
398 VideoFileModel.aggregate('size', 'SUM', hlsFilesQuery)
399 ]).then(([ webtorrentResult, hlsResult ]) => ({
400 totalLocalVideoFilesSize: parseAggregateResult(webtorrentResult) + parseAggregateResult(hlsResult)
404 // Redefine upsert because sequelize does not use an appropriate where clause in the update query with 2 unique indexes
405 static async customUpsert (
406 videoFile: MVideoFile,
407 mode: 'streaming-playlist' | 'video',
408 transaction: Transaction
412 resolution: videoFile.resolution
415 if (mode === 'streaming-playlist') Object.assign(baseWhere, { videoStreamingPlaylistId: videoFile.videoStreamingPlaylistId })
416 else Object.assign(baseWhere, { videoId: videoFile.videoId })
418 const element = await VideoFileModel.findOne({ where: baseWhere, transaction })
419 if (!element) return videoFile.save({ transaction })
421 for (const k of Object.keys(videoFile.toJSON())) {
422 element[k] = videoFile[k]
425 return element.save({ transaction })
428 static removeHLSFilesOfVideoId (videoStreamingPlaylistId: number) {
430 where: { videoStreamingPlaylistId }
433 return VideoFileModel.destroy(options)
437 return this.infoHash && this.torrentFilename
440 getVideoOrStreamingPlaylist (this: MVideoFileVideo | MVideoFileStreamingPlaylistVideo): MVideo | MStreamingPlaylistVideo {
441 if (this.videoId) return (this as MVideoFileVideo).Video
443 return (this as MVideoFileStreamingPlaylistVideo).VideoStreamingPlaylist
446 getVideo (this: MVideoFileVideo | MVideoFileStreamingPlaylistVideo): MVideo {
447 return extractVideo(this.getVideoOrStreamingPlaylist())
451 return !!MIMETYPES.AUDIO.EXT_MIMETYPE[this.extname]
455 return this.size === -1
459 return !!this.videoStreamingPlaylistId
462 getObjectStorageUrl () {
464 return getHLSPublicFileUrl(this.fileUrl)
467 return getWebTorrentPublicFileUrl(this.fileUrl)
470 getFileUrl (video: MVideo) {
471 if (this.storage === VideoStorage.OBJECT_STORAGE) {
472 return this.getObjectStorageUrl()
475 if (!this.Video) this.Video = video as VideoModel
476 if (video.isOwned()) return WEBSERVER.URL + this.getFileStaticPath(video)
481 getFileStaticPath (video: MVideo) {
482 if (this.isHLS()) return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, video.uuid, this.filename)
484 return join(STATIC_PATHS.WEBSEED, this.filename)
487 getFileDownloadUrl (video: MVideoWithHost) {
488 const path = this.isHLS()
489 ? join(STATIC_DOWNLOAD_PATHS.HLS_VIDEOS, `${video.uuid}-${this.resolution}-fragmented${this.extname}`)
490 : join(STATIC_DOWNLOAD_PATHS.VIDEOS, `${video.uuid}-${this.resolution}${this.extname}`)
492 if (video.isOwned()) return WEBSERVER.URL + path
494 // FIXME: don't guess remote URL
495 return buildRemoteVideoBaseUrl(video, path)
498 getRemoteTorrentUrl (video: MVideo) {
499 if (video.isOwned()) throw new Error(`Video ${video.url} is not a remote video`)
501 return this.torrentUrl
504 // We proxify torrent requests so use a local URL
506 if (!this.torrentFilename) return null
508 return WEBSERVER.URL + this.getTorrentStaticPath()
511 getTorrentStaticPath () {
512 if (!this.torrentFilename) return null
514 return join(LAZY_STATIC_PATHS.TORRENTS, this.torrentFilename)
517 getTorrentDownloadUrl () {
518 if (!this.torrentFilename) return null
520 return WEBSERVER.URL + join(STATIC_DOWNLOAD_PATHS.TORRENTS, this.torrentFilename)
524 if (!this.torrentFilename) return null
526 const torrentPath = getFSTorrentFilePath(this)
527 return remove(torrentPath)
528 .catch(err => logger.warn('Cannot delete torrent %s.', torrentPath, { err }))
531 hasSameUniqueKeysThan (other: MVideoFile) {
532 return this.fps === other.fps &&
533 this.resolution === other.resolution &&
535 (this.videoId !== null && this.videoId === other.videoId) ||
536 (this.videoStreamingPlaylistId !== null && this.videoStreamingPlaylistId === other.videoStreamingPlaylistId)
540 withVideoOrPlaylist (videoOrPlaylist: MVideo | MStreamingPlaylistVideo) {
541 if (isStreamingPlaylist(videoOrPlaylist)) return Object.assign(this, { VideoStreamingPlaylist: videoOrPlaylist })
543 return Object.assign(this, { Video: videoOrPlaylist })