+
+ 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
+ }
+
+ getFileUrl (video: MVideo) {
+ 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 basePath = this.isHLS()
+ ? STATIC_DOWNLOAD_PATHS.HLS_VIDEOS
+ : STATIC_DOWNLOAD_PATHS.VIDEOS
+ const path = join(basePath, this.filename)
+
+ 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 = getTorrentFilePath(this)
+ return remove(torrentPath)
+ .catch(err => logger.warn('Cannot delete torrent %s.', torrentPath, { err }))