diff options
Diffstat (limited to 'shared/server-commands/videos/videos.ts')
-rw-r--r-- | shared/server-commands/videos/videos.ts | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/shared/server-commands/videos/videos.ts b/shared/server-commands/videos/videos.ts new file mode 100644 index 000000000..2c3464aa8 --- /dev/null +++ b/shared/server-commands/videos/videos.ts | |||
@@ -0,0 +1,104 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */ | ||
2 | |||
3 | import { expect } from 'chai' | ||
4 | import { pathExists, readdir } from 'fs-extra' | ||
5 | import { basename, join } from 'path' | ||
6 | import { HttpStatusCode, VideoCaption, VideoDetails } from '@shared/models' | ||
7 | import { waitJobs } from '../server' | ||
8 | import { PeerTubeServer } from '../server/server' | ||
9 | import { VideoEdit } from './videos-command' | ||
10 | |||
11 | async function checkVideoFilesWereRemoved (options: { | ||
12 | server: PeerTubeServer | ||
13 | video: VideoDetails | ||
14 | captions?: VideoCaption[] | ||
15 | onlyVideoFiles?: boolean // default false | ||
16 | }) { | ||
17 | const { video, server, captions = [], onlyVideoFiles = false } = options | ||
18 | |||
19 | const webtorrentFiles = video.files || [] | ||
20 | const hlsFiles = video.streamingPlaylists[0]?.files || [] | ||
21 | |||
22 | const thumbnailName = basename(video.thumbnailPath) | ||
23 | const previewName = basename(video.previewPath) | ||
24 | |||
25 | const torrentNames = webtorrentFiles.concat(hlsFiles).map(f => basename(f.torrentUrl)) | ||
26 | |||
27 | const captionNames = captions.map(c => basename(c.captionPath)) | ||
28 | |||
29 | const webtorrentFilenames = webtorrentFiles.map(f => basename(f.fileUrl)) | ||
30 | const hlsFilenames = hlsFiles.map(f => basename(f.fileUrl)) | ||
31 | |||
32 | let directories: { [ directory: string ]: string[] } = { | ||
33 | videos: webtorrentFilenames, | ||
34 | redundancy: webtorrentFilenames, | ||
35 | [join('playlists', 'hls')]: hlsFilenames, | ||
36 | [join('redundancy', 'hls')]: hlsFilenames | ||
37 | } | ||
38 | |||
39 | if (onlyVideoFiles !== true) { | ||
40 | directories = { | ||
41 | ...directories, | ||
42 | |||
43 | thumbnails: [ thumbnailName ], | ||
44 | previews: [ previewName ], | ||
45 | torrents: torrentNames, | ||
46 | captions: captionNames | ||
47 | } | ||
48 | } | ||
49 | |||
50 | for (const directory of Object.keys(directories)) { | ||
51 | const directoryPath = server.servers.buildDirectory(directory) | ||
52 | |||
53 | const directoryExists = await pathExists(directoryPath) | ||
54 | if (directoryExists === false) continue | ||
55 | |||
56 | const existingFiles = await readdir(directoryPath) | ||
57 | for (const existingFile of existingFiles) { | ||
58 | for (const shouldNotExist of directories[directory]) { | ||
59 | expect(existingFile, `File ${existingFile} should not exist in ${directoryPath}`).to.not.contain(shouldNotExist) | ||
60 | } | ||
61 | } | ||
62 | } | ||
63 | } | ||
64 | |||
65 | async function saveVideoInServers (servers: PeerTubeServer[], uuid: string) { | ||
66 | for (const server of servers) { | ||
67 | server.store.videoDetails = await server.videos.get({ id: uuid }) | ||
68 | } | ||
69 | } | ||
70 | |||
71 | function checkUploadVideoParam ( | ||
72 | server: PeerTubeServer, | ||
73 | token: string, | ||
74 | attributes: Partial<VideoEdit>, | ||
75 | expectedStatus = HttpStatusCode.OK_200, | ||
76 | mode: 'legacy' | 'resumable' = 'legacy' | ||
77 | ) { | ||
78 | return mode === 'legacy' | ||
79 | ? server.videos.buildLegacyUpload({ token, attributes, expectedStatus }) | ||
80 | : server.videos.buildResumeUpload({ token, attributes, expectedStatus }) | ||
81 | } | ||
82 | |||
83 | // serverNumber starts from 1 | ||
84 | async function uploadRandomVideoOnServers ( | ||
85 | servers: PeerTubeServer[], | ||
86 | serverNumber: number, | ||
87 | additionalParams?: VideoEdit & { prefixName?: string } | ||
88 | ) { | ||
89 | const server = servers.find(s => s.serverNumber === serverNumber) | ||
90 | const res = await server.videos.randomUpload({ wait: false, additionalParams }) | ||
91 | |||
92 | await waitJobs(servers) | ||
93 | |||
94 | return res | ||
95 | } | ||
96 | |||
97 | // --------------------------------------------------------------------------- | ||
98 | |||
99 | export { | ||
100 | checkUploadVideoParam, | ||
101 | uploadRandomVideoOnServers, | ||
102 | checkVideoFilesWereRemoved, | ||
103 | saveVideoInServers | ||
104 | } | ||