]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/video-file.ts
Prevent object storage mock conflicts
[github/Chocobozzz/PeerTube.git] / server / lib / video-file.ts
CommitLineData
0c9668f7 1import { FfprobeData } from 'fluent-ffmpeg'
1bb4c9ab 2import { logger } from '@server/helpers/logger'
0c9668f7 3import { VideoFileModel } from '@server/models/video/video-file'
1bb4c9ab 4import { MVideoWithAllFiles } from '@server/types/models'
0c9668f7
C
5import { getLowercaseExtension } from '@shared/core-utils'
6import { getFileSize } from '@shared/extra-utils'
7import { ffprobePromise, getVideoStreamDimensionsInfo, getVideoStreamFPS, isAudioFile } from '@shared/ffmpeg'
8import { VideoFileMetadata, VideoResolution } from '@shared/models'
1bb4c9ab 9import { lTags } from './object-storage/shared'
0c9668f7 10import { generateHLSVideoFilename, generateWebTorrentVideoFilename } from './paths'
b9393464 11import { VideoPathManager } from './video-path-manager'
0c9668f7
C
12
13async function buildNewFile (options: {
14 path: string
15 mode: 'web-video' | 'hls'
16}) {
17 const { path, mode } = options
18
19 const probe = await ffprobePromise(path)
20 const size = await getFileSize(path)
21
22 const videoFile = new VideoFileModel({
23 extname: getLowercaseExtension(path),
24 size,
25 metadata: await buildFileMetadata(path, probe)
26 })
27
28 if (await isAudioFile(path, probe)) {
29 videoFile.resolution = VideoResolution.H_NOVIDEO
30 } else {
31 videoFile.fps = await getVideoStreamFPS(path, probe)
32 videoFile.resolution = (await getVideoStreamDimensionsInfo(path, probe)).resolution
33 }
34
35 videoFile.filename = mode === 'web-video'
36 ? generateWebTorrentVideoFilename(videoFile.resolution, videoFile.extname)
37 : generateHLSVideoFilename(videoFile.resolution)
38
39 return videoFile
40}
41
42// ---------------------------------------------------------------------------
1bb4c9ab
C
43
44async function removeHLSPlaylist (video: MVideoWithAllFiles) {
45 const hls = video.getHLSPlaylist()
46 if (!hls) return
47
b9393464 48 const videoFileMutexReleaser = await VideoPathManager.Instance.lockFiles(video.uuid)
1bb4c9ab 49
b9393464
C
50 try {
51 await video.removeStreamingPlaylistFiles(hls)
52 await hls.destroy()
53
54 video.VideoStreamingPlaylists = video.VideoStreamingPlaylists.filter(p => p.id !== hls.id)
55 } finally {
56 videoFileMutexReleaser()
57 }
1bb4c9ab
C
58}
59
60async function removeHLSFile (video: MVideoWithAllFiles, fileToDeleteId: number) {
61 logger.info('Deleting HLS file %d of %s.', fileToDeleteId, video.url, lTags(video.uuid))
62
63 const hls = video.getHLSPlaylist()
64 const files = hls.VideoFiles
65
66 if (files.length === 1) {
67 await removeHLSPlaylist(video)
68 return undefined
69 }
70
b9393464 71 const videoFileMutexReleaser = await VideoPathManager.Instance.lockFiles(video.uuid)
1bb4c9ab 72
b9393464
C
73 try {
74 const toDelete = files.find(f => f.id === fileToDeleteId)
75 await video.removeStreamingPlaylistVideoFile(video.getHLSPlaylist(), toDelete)
76 await toDelete.destroy()
77
78 hls.VideoFiles = hls.VideoFiles.filter(f => f.id !== toDelete.id)
79 } finally {
80 videoFileMutexReleaser()
81 }
1bb4c9ab
C
82
83 return hls
84}
85
86// ---------------------------------------------------------------------------
87
88async function removeAllWebTorrentFiles (video: MVideoWithAllFiles) {
b9393464
C
89 const videoFileMutexReleaser = await VideoPathManager.Instance.lockFiles(video.uuid)
90
91 try {
92 for (const file of video.VideoFiles) {
93 await video.removeWebTorrentFile(file)
94 await file.destroy()
95 }
1bb4c9ab 96
b9393464
C
97 video.VideoFiles = []
98 } finally {
99 videoFileMutexReleaser()
100 }
1bb4c9ab
C
101
102 return video
103}
104
105async function removeWebTorrentFile (video: MVideoWithAllFiles, fileToDeleteId: number) {
106 const files = video.VideoFiles
107
108 if (files.length === 1) {
109 return removeAllWebTorrentFiles(video)
110 }
111
b9393464
C
112 const videoFileMutexReleaser = await VideoPathManager.Instance.lockFiles(video.uuid)
113 try {
114 const toDelete = files.find(f => f.id === fileToDeleteId)
115 await video.removeWebTorrentFile(toDelete)
116 await toDelete.destroy()
1bb4c9ab 117
b9393464
C
118 video.VideoFiles = files.filter(f => f.id !== toDelete.id)
119 } finally {
120 videoFileMutexReleaser()
121 }
1bb4c9ab
C
122
123 return video
124}
125
0c9668f7
C
126// ---------------------------------------------------------------------------
127
128async function buildFileMetadata (path: string, existingProbe?: FfprobeData) {
129 const metadata = existingProbe || await ffprobePromise(path)
130
131 return new VideoFileMetadata(metadata)
132}
133
134// ---------------------------------------------------------------------------
135
1bb4c9ab 136export {
0c9668f7
C
137 buildNewFile,
138
1bb4c9ab
C
139 removeHLSPlaylist,
140 removeHLSFile,
141 removeAllWebTorrentFiles,
0c9668f7
C
142 removeWebTorrentFile,
143
144 buildFileMetadata
1bb4c9ab 145}