X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fvideo-file.ts;h=07bc13de1a9938d29f161047318e29712cb07aef;hb=8c4bbd946d2247c2e239cbbf8773d2d31c1a57aa;hp=22cf638046afb6e3fb1ee0bf2e33285cc5484859;hpb=96d52038788dda2f479362ce8e980f895369eec7;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video-file.ts b/server/models/video/video-file.ts index 22cf63804..07bc13de1 100644 --- a/server/models/video/video-file.ts +++ b/server/models/video/video-file.ts @@ -1,7 +1,7 @@ import { remove } from 'fs-extra' -import * as memoizee from 'memoizee' +import memoizee from 'memoizee' import { join } from 'path' -import { FindOptions, Op, QueryTypes, Transaction } from 'sequelize' +import { FindOptions, Op, Transaction, WhereOptions } from 'sequelize' import { AllowNull, BelongsTo, @@ -18,14 +18,22 @@ import { Table, UpdatedAt } from 'sequelize-typescript' -import { Where } from 'sequelize/types/lib/utils' import validator from 'validator' -import { buildRemoteVideoBaseUrl } from '@server/helpers/activitypub' import { logger } from '@server/helpers/logger' import { extractVideo } from '@server/helpers/video' -import { getTorrentFilePath } from '@server/lib/video-paths' -import { MStreamingPlaylistVideo, MVideo, MVideoWithHost } from '@server/types/models' -import { AttributesOnly } from '@shared/core-utils' +import { CONFIG } from '@server/initializers/config' +import { buildRemoteVideoBaseUrl } from '@server/lib/activitypub/url' +import { + getHLSPrivateFileUrl, + getHLSPublicFileUrl, + getWebTorrentPrivateFileUrl, + getWebTorrentPublicFileUrl +} from '@server/lib/object-storage' +import { getFSTorrentFilePath } from '@server/lib/paths' +import { isVideoInPrivateDirectory } from '@server/lib/video-privacy' +import { isStreamingPlaylist, MStreamingPlaylistVideo, MVideo, MVideoWithHost } from '@server/types/models' +import { VideoResolution, VideoStorage } from '@shared/models' +import { AttributesOnly } from '@shared/typescript-utils' import { isVideoFileExtnameValid, isVideoFileInfoHashValid, @@ -37,14 +45,13 @@ import { LAZY_STATIC_PATHS, MEMOIZE_LENGTH, MEMOIZE_TTL, - MIMETYPES, STATIC_DOWNLOAD_PATHS, STATIC_PATHS, WEBSERVER } from '../../initializers/constants' import { MVideoFile, MVideoFileStreamingPlaylistVideo, MVideoFileVideo } from '../../types/models/video/video-file' import { VideoRedundancyModel } from '../redundancy/video-redundancy' -import { parseAggregateResult, throwIfNotValid } from '../utils' +import { doesExist, parseAggregateResult, throwIfNotValid } from '../shared' import { VideoModel } from './video' import { VideoStreamingPlaylistModel } from './video-streaming-playlist' @@ -68,7 +75,7 @@ export enum ScopeNames { } ] }, - [ScopeNames.WITH_VIDEO_OR_PLAYLIST]: (options: { whereVideo?: Where } = {}) => { + [ScopeNames.WITH_VIDEO_OR_PLAYLIST]: (options: { whereVideo?: WhereOptions } = {}) => { return { include: [ { @@ -191,6 +198,7 @@ export class VideoFileModel extends Model @Column metadataUrl: string + // Could be null for remote files @AllowNull(true) @Column fileUrl: string @@ -200,6 +208,7 @@ export class VideoFileModel extends Model @Column filename: string + // Could be null for remote files @AllowNull(true) @Column torrentUrl: string @@ -213,6 +222,11 @@ export class VideoFileModel extends Model @Column videoId: number + @AllowNull(false) + @Default(VideoStorage.FILE_SYSTEM) + @Column + storage: VideoStorage + @BelongsTo(() => VideoModel, { foreignKey: { allowNull: true @@ -250,14 +264,8 @@ export class VideoFileModel extends Model static doesInfohashExist (infoHash: string) { const query = 'SELECT 1 FROM "videoFile" WHERE "infoHash" = $infoHash LIMIT 1' - const options = { - type: QueryTypes.SELECT as QueryTypes.SELECT, - bind: { infoHash }, - raw: true - } - return VideoModel.sequelize.query(query, options) - .then(results => results.length === 1) + return doesExist(this.sequelize, query, { infoHash }) } static async doesVideoExistForVideoFile (id: number, videoIdOrUUID: number | string) { @@ -266,6 +274,43 @@ export class VideoFileModel extends Model return !!videoFile } + static async doesOwnedTorrentFileExist (filename: string) { + const query = 'SELECT 1 FROM "videoFile" ' + + 'LEFT JOIN "video" "webtorrent" ON "webtorrent"."id" = "videoFile"."videoId" AND "webtorrent"."remote" IS FALSE ' + + 'LEFT JOIN "videoStreamingPlaylist" ON "videoStreamingPlaylist"."id" = "videoFile"."videoStreamingPlaylistId" ' + + 'LEFT JOIN "video" "hlsVideo" ON "hlsVideo"."id" = "videoStreamingPlaylist"."videoId" AND "hlsVideo"."remote" IS FALSE ' + + 'WHERE "torrentFilename" = $filename AND ("hlsVideo"."id" IS NOT NULL OR "webtorrent"."id" IS NOT NULL) LIMIT 1' + + return doesExist(this.sequelize, query, { filename }) + } + + static async doesOwnedWebTorrentVideoFileExist (filename: string) { + const query = 'SELECT 1 FROM "videoFile" INNER JOIN "video" ON "video"."id" = "videoFile"."videoId" AND "video"."remote" IS FALSE ' + + `WHERE "filename" = $filename AND "storage" = ${VideoStorage.FILE_SYSTEM} LIMIT 1` + + return doesExist(this.sequelize, query, { filename }) + } + + static loadByFilename (filename: string) { + const query = { + where: { + filename + } + } + + return VideoFileModel.findOne(query) + } + + static loadWithVideoByFilename (filename: string): Promise { + const query = { + where: { + filename + } + } + + return VideoFileModel.scope(ScopeNames.WITH_VIDEO_OR_PLAYLIST).findOne(query) + } + static loadWithVideoOrPlaylistByTorrentFilename (filename: string) { const query = { where: { @@ -276,6 +321,10 @@ export class VideoFileModel extends Model return VideoFileModel.scope(ScopeNames.WITH_VIDEO_OR_PLAYLIST).findOne(query) } + static load (id: number): Promise { + return VideoFileModel.findByPk(id) + } + static loadWithMetadata (id: number) { return VideoFileModel.scope(ScopeNames.WITH_METADATA).findByPk(id) } @@ -376,24 +425,55 @@ export class VideoFileModel extends Model mode: 'streaming-playlist' | 'video', transaction: Transaction ) { - const baseWhere = { + const baseFind = { fps: videoFile.fps, - resolution: videoFile.resolution + resolution: videoFile.resolution, + transaction } - if (mode === 'streaming-playlist') Object.assign(baseWhere, { videoStreamingPlaylistId: videoFile.videoStreamingPlaylistId }) - else Object.assign(baseWhere, { videoId: videoFile.videoId }) + const element = mode === 'streaming-playlist' + ? await VideoFileModel.loadHLSFile({ ...baseFind, playlistId: videoFile.videoStreamingPlaylistId }) + : await VideoFileModel.loadWebTorrentFile({ ...baseFind, videoId: videoFile.videoId }) - const element = await VideoFileModel.findOne({ where: baseWhere, transaction }) if (!element) return videoFile.save({ transaction }) for (const k of Object.keys(videoFile.toJSON())) { - element[k] = videoFile[k] + element.set(k, videoFile[k]) } return element.save({ transaction }) } + static async loadWebTorrentFile (options: { + videoId: number + fps: number + resolution: number + transaction?: Transaction + }) { + const where = { + fps: options.fps, + resolution: options.resolution, + videoId: options.videoId + } + + return VideoFileModel.findOne({ where, transaction: options.transaction }) + } + + static async loadHLSFile (options: { + playlistId: number + fps: number + resolution: number + transaction?: Transaction + }) { + const where = { + fps: options.fps, + resolution: options.resolution, + videoStreamingPlaylistId: options.playlistId + } + + return VideoFileModel.findOne({ where, transaction: options.transaction }) + } + static removeHLSFilesOfVideoId (videoStreamingPlaylistId: number) { const options = { where: { videoStreamingPlaylistId } @@ -407,7 +487,7 @@ export class VideoFileModel extends Model } getVideoOrStreamingPlaylist (this: MVideoFileVideo | MVideoFileStreamingPlaylistVideo): MVideo | MStreamingPlaylistVideo { - if (this.videoId) return (this as MVideoFileVideo).Video + if (this.videoId || (this as MVideoFileVideo).Video) return (this as MVideoFileVideo).Video return (this as MVideoFileStreamingPlaylistVideo).VideoStreamingPlaylist } @@ -417,7 +497,7 @@ export class VideoFileModel extends Model } isAudio () { - return !!MIMETYPES.AUDIO.EXT_MIMETYPE[this.extname] + return this.resolution === VideoResolution.H_NOVIDEO } isLive () { @@ -428,25 +508,76 @@ export class VideoFileModel extends Model return !!this.videoStreamingPlaylistId } + // --------------------------------------------------------------------------- + + getObjectStorageUrl (video: MVideo) { + if (video.hasPrivateStaticPath() && CONFIG.OBJECT_STORAGE.PROXY.PROXIFY_PRIVATE_FILES === true) { + return this.getPrivateObjectStorageUrl(video) + } + + return this.getPublicObjectStorageUrl() + } + + private getPrivateObjectStorageUrl (video: MVideo) { + if (this.isHLS()) { + return getHLSPrivateFileUrl(video, this.filename) + } + + return getWebTorrentPrivateFileUrl(this.filename) + } + + private getPublicObjectStorageUrl () { + if (this.isHLS()) { + return getHLSPublicFileUrl(this.fileUrl) + } + + return getWebTorrentPublicFileUrl(this.fileUrl) + } + + // --------------------------------------------------------------------------- + getFileUrl (video: MVideo) { - if (!this.Video) this.Video = video as VideoModel + if (video.isOwned()) { + if (this.storage === VideoStorage.OBJECT_STORAGE) { + return this.getObjectStorageUrl(video) + } - if (video.isOwned()) return WEBSERVER.URL + this.getFileStaticPath(video) + return WEBSERVER.URL + this.getFileStaticPath(video) + } return this.fileUrl } + // --------------------------------------------------------------------------- + getFileStaticPath (video: MVideo) { - if (this.isHLS()) return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, video.uuid, this.filename) + if (this.isHLS()) return this.getHLSFileStaticPath(video) + + return this.getWebTorrentFileStaticPath(video) + } + + private getWebTorrentFileStaticPath (video: MVideo) { + if (isVideoInPrivateDirectory(video.privacy)) { + return join(STATIC_PATHS.PRIVATE_WEBSEED, this.filename) + } return join(STATIC_PATHS.WEBSEED, this.filename) } + private getHLSFileStaticPath (video: MVideo) { + if (isVideoInPrivateDirectory(video.privacy)) { + return join(STATIC_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS, video.uuid, this.filename) + } + + return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, video.uuid, this.filename) + } + + // --------------------------------------------------------------------------- + getFileDownloadUrl (video: MVideoWithHost) { - const basePath = this.isHLS() - ? STATIC_DOWNLOAD_PATHS.HLS_VIDEOS - : STATIC_DOWNLOAD_PATHS.VIDEOS - const path = join(basePath, this.filename) + const path = this.isHLS() + ? join(STATIC_DOWNLOAD_PATHS.HLS_VIDEOS, `${video.uuid}-${this.resolution}-fragmented${this.extname}`) + : join(STATIC_DOWNLOAD_PATHS.VIDEOS, `${video.uuid}-${this.resolution}${this.extname}`) if (video.isOwned()) return WEBSERVER.URL + path @@ -482,7 +613,7 @@ export class VideoFileModel extends Model removeTorrent () { if (!this.torrentFilename) return null - const torrentPath = getTorrentFilePath(this) + const torrentPath = getFSTorrentFilePath(this) return remove(torrentPath) .catch(err => logger.warn('Cannot delete torrent %s.', torrentPath, { err })) } @@ -495,4 +626,10 @@ export class VideoFileModel extends Model (this.videoStreamingPlaylistId !== null && this.videoStreamingPlaylistId === other.videoStreamingPlaylistId) ) } + + withVideoOrPlaylist (videoOrPlaylist: MVideo | MStreamingPlaylistVideo) { + if (isStreamingPlaylist(videoOrPlaylist)) return Object.assign(this, { VideoStreamingPlaylist: videoOrPlaylist }) + + return Object.assign(this, { Video: videoOrPlaylist }) + } }