+ const hlsFilesQuery: FindOptions = {
+ include: [
+ {
+ attributes: [],
+ required: true,
+ model: VideoStreamingPlaylistModel.unscoped(),
+ include: [
+ {
+ attributes: [],
+ model: VideoModel.unscoped(),
+ required: true,
+ where: {
+ remote: false
+ }
+ }
+ ]
+ }
+ ]
+ }
+
+ return Promise.all([
+ VideoFileModel.aggregate('size', 'SUM', webtorrentFilesQuery),
+ VideoFileModel.aggregate('size', 'SUM', hlsFilesQuery)
+ ]).then(([ webtorrentResult, hlsResult ]) => ({
+ totalLocalVideoFilesSize: parseAggregateResult(webtorrentResult) + parseAggregateResult(hlsResult)
+ }))
+ }
+
+ // Redefine upsert because sequelize does not use an appropriate where clause in the update query with 2 unique indexes
+ static async customUpsert (
+ videoFile: MVideoFile,
+ mode: 'streaming-playlist' | 'video',
+ transaction: Transaction
+ ) {
+ const baseWhere = {
+ fps: videoFile.fps,
+ resolution: videoFile.resolution
+ }
+
+ if (mode === 'streaming-playlist') Object.assign(baseWhere, { videoStreamingPlaylistId: videoFile.videoStreamingPlaylistId })
+ else Object.assign(baseWhere, { 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]
+ }
+
+ return element.save({ transaction })
+ }
+
+ static removeHLSFilesOfVideoId (videoStreamingPlaylistId: number) {
+ const options = {
+ where: { videoStreamingPlaylistId }
+ }
+
+ return VideoFileModel.destroy(options)
+ }
+
+ hasTorrent () {
+ return this.infoHash && this.torrentFilename
+ }
+
+ getVideoOrStreamingPlaylist (this: MVideoFileVideo | MVideoFileStreamingPlaylistVideo): MVideo | MStreamingPlaylistVideo {
+ if (this.videoId) return (this as MVideoFileVideo).Video
+
+ return (this as MVideoFileStreamingPlaylistVideo).VideoStreamingPlaylist
+ }
+
+ getVideo (this: MVideoFileVideo | MVideoFileStreamingPlaylistVideo): MVideo {
+ return extractVideo(this.getVideoOrStreamingPlaylist())
+ }
+
+ isAudio () {
+ return !!MIMETYPES.AUDIO.EXT_MIMETYPE[this.extname]
+ }
+
+ isLive () {
+ return this.size === -1
+ }
+
+ isHLS () {
+ return !!this.videoStreamingPlaylistId
+ }
+
+ getObjectStorageUrl () {
+ if (this.isHLS()) {
+ return getHLSPublicFileUrl(this.fileUrl)
+ }
+
+ return getWebTorrentPublicFileUrl(this.fileUrl)
+ }
+
+ getFileUrl (video: MVideo) {
+ 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
+ }
+
+ getFileStaticPath (video: MVideo) {
+ if (this.isHLS()) return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, video.uuid, this.filename)
+
+ return join(STATIC_PATHS.WEBSEED, this.filename)
+ }
+
+ getFileDownloadUrl (video: MVideoWithHost) {
+ 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
+
+ // FIXME: don't guess remote URL
+ return buildRemoteVideoBaseUrl(video, path)
+ }
+
+ getRemoteTorrentUrl (video: MVideo) {
+ if (video.isOwned()) throw new Error(`Video ${video.url} is not a remote video`)
+
+ return this.torrentUrl
+ }
+
+ // We proxify torrent requests so use a local URL
+ getTorrentUrl () {
+ if (!this.torrentFilename) return null
+
+ return WEBSERVER.URL + this.getTorrentStaticPath()
+ }
+
+ getTorrentStaticPath () {
+ if (!this.torrentFilename) return null
+
+ return join(LAZY_STATIC_PATHS.TORRENTS, this.torrentFilename)
+ }
+
+ getTorrentDownloadUrl () {
+ if (!this.torrentFilename) return null
+
+ return WEBSERVER.URL + join(STATIC_DOWNLOAD_PATHS.TORRENTS, this.torrentFilename)
+ }
+
+ removeTorrent () {
+ if (!this.torrentFilename) return null
+
+ const torrentPath = getFSTorrentFilePath(this)
+ return remove(torrentPath)
+ .catch(err => logger.warn('Cannot delete torrent %s.', torrentPath, { err }))