aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/paths.ts
blob: d8cf812e3bce15d2384fbf26489c32c3159da4b6 (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
75
76
77
78
79
80
81
82
import { join } from 'path'
import { buildUUID } from '@shared/core-utils/uuid'
import { CONFIG } from '@server/initializers/config'
import { HLS_REDUNDANCY_DIRECTORY, HLS_STREAMING_PLAYLIST_DIRECTORY } from '@server/initializers/constants'
import { isStreamingPlaylist, MStreamingPlaylistVideo, MVideo, MVideoFile, MVideoUUID } from '@server/types/models'
import { removeFragmentedMP4Ext } from '@shared/core-utils'

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

function generateWebTorrentVideoFilename (resolution: number, extname: string) {
  return buildUUID() + '-' + resolution + extname
}

function generateHLSVideoFilename (resolution: number) {
  return `${buildUUID()}-${resolution}-fragmented.mp4`
}

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

function getLiveDirectory (video: MVideoUUID) {
  return getHLSDirectory(video)
}

function getHLSDirectory (video: MVideoUUID) {
  return join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid)
}

function getHLSRedundancyDirectory (video: MVideoUUID) {
  return join(HLS_REDUNDANCY_DIRECTORY, video.uuid)
}

function getHlsResolutionPlaylistFilename (videoFilename: string) {
  // Video file name already contain resolution
  return removeFragmentedMP4Ext(videoFilename) + '.m3u8'
}

function generateHLSMasterPlaylistFilename (isLive = false) {
  if (isLive) return 'master.m3u8'

  return buildUUID() + '-master.m3u8'
}

function generateHlsSha256SegmentsFilename (isLive = false) {
  if (isLive) return 'segments-sha256.json'

  return buildUUID() + '-segments-sha256.json'
}

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

function generateTorrentFileName (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, resolution: number) {
  const extension = '.torrent'
  const uuid = buildUUID()

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

  return uuid + '-' + resolution + extension
}

function getFSTorrentFilePath (videoFile: MVideoFile) {
  return join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename)
}

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

export {
  generateHLSVideoFilename,
  generateWebTorrentVideoFilename,

  generateTorrentFileName,
  getFSTorrentFilePath,

  getHLSDirectory,
  getLiveDirectory,
  getHLSRedundancyDirectory,

  generateHLSMasterPlaylistFilename,
  generateHlsSha256SegmentsFilename,
  getHlsResolutionPlaylistFilename
}