import validator from 'validator'
import { logger } from '@server/helpers/logger'
import { extractVideo } from '@server/helpers/video'
+import { CONFIG } from '@server/initializers/config'
import { buildRemoteVideoBaseUrl } from '@server/lib/activitypub/url'
-import { getHLSPublicFileUrl, getWebTorrentPublicFileUrl } from '@server/lib/object-storage'
+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'
} 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 { doesExist, parseAggregateResult, throwIfNotValid } from '../shared'
import { VideoModel } from './video'
import { VideoStreamingPlaylistModel } from './video-streaming-playlist'
static doesInfohashExist (infoHash: string) {
const query = 'SELECT 1 FROM "videoFile" WHERE "infoHash" = $infoHash LIMIT 1'
- return doesExist(query, { infoHash })
+ return doesExist(this.sequelize, query, { infoHash })
}
static async doesVideoExistForVideoFile (id: number, videoIdOrUUID: number | string) {
'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(query, { filename })
+ 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(query, { filename })
+ return doesExist(this.sequelize, query, { filename })
}
static loadByFilename (filename: string) {
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 })
return !!this.videoStreamingPlaylistId
}
- getObjectStorageUrl () {
+ // ---------------------------------------------------------------------------
+
+ 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.storage === VideoStorage.OBJECT_STORAGE) {
- return this.getObjectStorageUrl()
- }
+ if (video.isOwned()) {
+ if (this.storage === VideoStorage.OBJECT_STORAGE) {
+ return this.getObjectStorageUrl(video)
+ }
- if (!this.Video) this.Video = video as VideoModel
- if (video.isOwned()) return WEBSERVER.URL + this.getFileStaticPath(video)
+ return WEBSERVER.URL + this.getFileStaticPath(video)
+ }
return this.fileUrl
}
+ // ---------------------------------------------------------------------------
+
getFileStaticPath (video: MVideo) {
- if (this.isHLS()) {
- if (isVideoInPrivateDirectory(video.privacy)) {
- return join(STATIC_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS, video.uuid, this.filename)
- }
+ if (this.isHLS()) return this.getHLSFileStaticPath(video)
- return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, video.uuid, this.filename)
- }
+ 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 path = this.isHLS()
? join(STATIC_DOWNLOAD_PATHS.HLS_VIDEOS, `${video.uuid}-${this.resolution}-fragmented${this.extname}`)