aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/video-paths.ts
blob: 05aaca8afd64ddc104df02be39fcca27dd08cf2a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { isStreamingPlaylist, MStreamingPlaylistVideo, MVideo, MVideoFile, MVideoUUID } from '@server/typings/models'
import { join } from 'path'
import { CONFIG } from '@server/initializers/config'
import { HLS_REDUNDANCY_DIRECTORY, HLS_STREAMING_PLAYLIST_DIRECTORY } from '@server/initializers/constants'
import { extractVideo } from '@server/helpers/video'

// ################## Video file name ##################

function getVideoFilename (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) {
  const video = extractVideo(videoOrPlaylist)

  if (isStreamingPlaylist(videoOrPlaylist)) {
    return generateVideoStreamingPlaylistName(video.uuid, videoFile.resolution)
  }

  return generateWebTorrentVideoName(video.uuid, videoFile.resolution, videoFile.extname)
}

function generateVideoStreamingPlaylistName (uuid: string, resolution: number) {
  return `${uuid}-${resolution}-fragmented.mp4`
}

function generateWebTorrentVideoName (uuid: string, resolution: number, extname: string) {
  return uuid + '-' + resolution + extname
}

function getVideoFilePath (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile, isRedundancy = false) {
  if (isStreamingPlaylist(videoOrPlaylist)) {
    const video = extractVideo(videoOrPlaylist)
    return join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid, getVideoFilename(videoOrPlaylist, videoFile))
  }

  const baseDir = isRedundancy ? CONFIG.STORAGE.REDUNDANCY_DIR : CONFIG.STORAGE.VIDEOS_DIR
  return join(baseDir, getVideoFilename(videoOrPlaylist, videoFile))
}

// ################## Streaming playlist ##################

function getHLSDirectory (video: MVideoUUID, isRedundancy = false) {
  const baseDir = isRedundancy ? HLS_REDUNDANCY_DIRECTORY : HLS_STREAMING_PLAYLIST_DIRECTORY

  return join(baseDir, video.uuid)
}

// ################## Torrents ##################

function getTorrentFileName (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) {
  const video = extractVideo(videoOrPlaylist)
  const extension = '.torrent'

  if (isStreamingPlaylist(videoOrPlaylist)) {
    return `${video.uuid}-${videoFile.resolution}-${videoOrPlaylist.getStringType()}${extension}`
  }

  return video.uuid + '-' + videoFile.resolution + extension
}

function getTorrentFilePath (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, videoFile: MVideoFile) {
  return join(CONFIG.STORAGE.TORRENTS_DIR, getTorrentFileName(videoOrPlaylist, videoFile))
}

// ---------------------------------------------------------------------------

export {
  generateVideoStreamingPlaylistName,
  generateWebTorrentVideoName,
  getVideoFilename,
  getVideoFilePath,

  getTorrentFileName,
  getTorrentFilePath,

  getHLSDirectory
}