aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/video-paths.ts
blob: 0385e89cce650b3ef2ee4d10ccc3ef28c7ea918a (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { join } from 'path'
import { extractVideo } from '@server/helpers/video'
import { CONFIG } from '@server/initializers/config'
import { HLS_REDUNDANCY_DIRECTORY, HLS_STREAMING_PLAYLIST_DIRECTORY, STATIC_PATHS, WEBSERVER } from '@server/initializers/constants'
import { isStreamingPlaylist, MStreamingPlaylist, MStreamingPlaylistVideo, MVideo, MVideoFile, MVideoUUID } from '@server/types/models'

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

function generateVideoFilename (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, isHls: boolean, resolution: number, extname: string) {
  const video = extractVideo(videoOrPlaylist)

  // FIXME: use a generated uuid instead, that will break compatibility with PeerTube < 3.2
  // const uuid = uuidv4()
  const uuid = video.uuid

  if (isHls) {
    return generateVideoStreamingPlaylistName(uuid, resolution)
  }

  return generateWebTorrentVideoName(uuid, resolution, 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 (videoFile.isHLS()) {
    const video = extractVideo(videoOrPlaylist)

    return join(getHLSDirectory(video), videoFile.filename)
  }

  const baseDir = isRedundancy
    ? CONFIG.STORAGE.REDUNDANCY_DIR
    : CONFIG.STORAGE.VIDEOS_DIR

  return join(baseDir, videoFile.filename)
}

// ################## Redundancy ##################

function generateHLSRedundancyUrl (video: MVideo, playlist: MStreamingPlaylist) {
  // Base URL used by our HLS player
  return WEBSERVER.URL + STATIC_PATHS.REDUNDANCY + playlist.getStringType() + '/' + video.uuid
}

function generateWebTorrentRedundancyUrl (file: MVideoFile) {
  return WEBSERVER.URL + STATIC_PATHS.REDUNDANCY + file.filename
}

// ################## 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 generateTorrentFileName (videoOrPlaylist: MVideo | MStreamingPlaylistVideo, resolution: number) {
  const video = extractVideo(videoOrPlaylist)
  const extension = '.torrent'

  // FIXME: use a generated uuid instead, that will break compatibility with PeerTube < 3.2
  // const uuid = uuidv4()
  const uuid = video.uuid

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

  return uuid + '-' + resolution + extension
}

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

// ################## Meta data ##################

function getLocalVideoFileMetadataUrl (video: MVideoUUID, videoFile: MVideoFile) {
  const path = '/api/v1/videos/'

  return WEBSERVER.URL + path + video.uuid + '/metadata/' + videoFile.id
}

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

export {
  generateVideoStreamingPlaylistName,
  generateWebTorrentVideoName,
  generateVideoFilename,
  getVideoFilePath,

  generateTorrentFileName,
  getTorrentFilePath,

  getHLSDirectory,

  getLocalVideoFileMetadataUrl,

  generateWebTorrentRedundancyUrl,
  generateHLSRedundancyUrl
}