X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fvideo-file.ts;h=4aaee1ffadbf7827814a73777b1d7dc572eccf3c;hb=714e33a7428b71ef98129ce85a4bd64140bcd912;hp=797a85a4e44dd2a71ab4639e0c31a84a1184b6f2;hpb=764b1a14fc494f2cfd7ea590d2f07b01df65c7ad;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video-file.ts b/server/models/video/video-file.ts index 797a85a4e..4aaee1ffa 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, Transaction } from 'sequelize' +import { FindOptions, Op, Transaction, WhereOptions } from 'sequelize' import { AllowNull, BelongsTo, @@ -18,15 +18,15 @@ import { Table, UpdatedAt } from 'sequelize-typescript' -import { Where } from 'sequelize/types/lib/utils' import validator from 'validator' -import { buildRemoteVideoBaseUrl } from '@server/helpers/activitypub' -import { doesExist } from '@server/helpers/database-utils' 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 { buildRemoteVideoBaseUrl } from '@server/lib/activitypub/url' +import { getHLSPublicFileUrl, getWebTorrentPublicFileUrl } from '@server/lib/object-storage' +import { getFSTorrentFilePath } from '@server/lib/paths' +import { isStreamingPlaylist, MStreamingPlaylistVideo, MVideo, MVideoWithHost } from '@server/types/models' +import { VideoResolution, VideoStorage } from '@shared/models' +import { AttributesOnly } from '@shared/typescript-utils' import { isVideoFileExtnameValid, isVideoFileInfoHashValid, @@ -38,13 +38,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 { doesExist } from '../shared' import { parseAggregateResult, throwIfNotValid } from '../utils' import { VideoModel } from './video' import { VideoStreamingPlaylistModel } from './video-streaming-playlist' @@ -69,7 +69,7 @@ export enum ScopeNames { } ] }, - [ScopeNames.WITH_VIDEO_OR_PLAYLIST]: (options: { whereVideo?: Where } = {}) => { + [ScopeNames.WITH_VIDEO_OR_PLAYLIST]: (options: { whereVideo?: WhereOptions } = {}) => { return { include: [ { @@ -192,6 +192,7 @@ export class VideoFileModel extends Model @Column metadataUrl: string + // Could be null for remote files @AllowNull(true) @Column fileUrl: string @@ -201,6 +202,7 @@ export class VideoFileModel extends Model @Column filename: string + // Could be null for remote files @AllowNull(true) @Column torrentUrl: string @@ -214,6 +216,11 @@ export class VideoFileModel extends Model @Column videoId: number + @AllowNull(false) + @Default(VideoStorage.FILE_SYSTEM) + @Column + storage: VideoStorage + @BelongsTo(() => VideoModel, { foreignKey: { allowNull: true @@ -273,7 +280,7 @@ export class VideoFileModel extends Model 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 LIMIT 1' + `WHERE "filename" = $filename AND "storage" = ${VideoStorage.FILE_SYSTEM} LIMIT 1` return doesExist(query, { filename }) } @@ -439,7 +446,7 @@ export class VideoFileModel extends Model } isAudio () { - return !!MIMETYPES.AUDIO.EXT_MIMETYPE[this.extname] + return this.resolution === VideoResolution.H_NOVIDEO } isLive () { @@ -450,9 +457,20 @@ export class VideoFileModel extends Model return !!this.videoStreamingPlaylistId } + getObjectStorageUrl () { + if (this.isHLS()) { + return getHLSPublicFileUrl(this.fileUrl) + } + + return getWebTorrentPublicFileUrl(this.fileUrl) + } + getFileUrl (video: MVideo) { - if (!this.Video) this.Video = video as VideoModel + if (this.storage === VideoStorage.OBJECT_STORAGE) { + return this.getObjectStorageUrl() + } + if (!this.Video) this.Video = video as VideoModel if (video.isOwned()) return WEBSERVER.URL + this.getFileStaticPath(video) return this.fileUrl @@ -503,7 +521,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 })) } @@ -516,4 +534,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 }) + } }