1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */
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'
11 async function checkVideoFilesWereRemoved (options: {
12 server: PeerTubeServer
14 captions?: VideoCaption[]
15 onlyVideoFiles?: boolean // default false
17 const { video, server, captions = [], onlyVideoFiles = false } = options
19 const webtorrentFiles = video.files || []
20 const hlsFiles = video.streamingPlaylists[0]?.files || []
22 const thumbnailName = basename(video.thumbnailPath)
23 const previewName = basename(video.previewPath)
25 const torrentNames = webtorrentFiles.concat(hlsFiles).map(f => basename(f.torrentUrl))
27 const captionNames = captions.map(c => basename(c.captionPath))
29 const webtorrentFilenames = webtorrentFiles.map(f => basename(f.fileUrl))
30 const hlsFilenames = hlsFiles.map(f => basename(f.fileUrl))
32 let directories: { [ directory: string ]: string[] } = {
33 videos: webtorrentFilenames,
34 redundancy: webtorrentFilenames,
35 [join('playlists', 'hls')]: hlsFilenames,
36 [join('redundancy', 'hls')]: hlsFilenames
39 if (onlyVideoFiles !== true) {
43 thumbnails: [ thumbnailName ],
44 previews: [ previewName ],
45 torrents: torrentNames,
46 captions: captionNames
50 for (const directory of Object.keys(directories)) {
51 const directoryPath = server.servers.buildDirectory(directory)
53 const directoryExists = await pathExists(directoryPath)
54 if (directoryExists === false) continue
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)
65 async function saveVideoInServers (servers: PeerTubeServer[], uuid: string) {
66 for (const server of servers) {
67 server.store.videoDetails = await server.videos.get({ id: uuid })
71 function checkUploadVideoParam (
72 server: PeerTubeServer,
74 attributes: Partial<VideoEdit>,
75 expectedStatus = HttpStatusCode.OK_200,
76 mode: 'legacy' | 'resumable' = 'legacy'
78 return mode === 'legacy'
79 ? server.videos.buildLegacyUpload({ token, attributes, expectedStatus })
80 : server.videos.buildResumeUpload({ token, attributes, expectedStatus })
83 // serverNumber starts from 1
84 async function uploadRandomVideoOnServers (
85 servers: PeerTubeServer[],
87 additionalParams?: VideoEdit & { prefixName?: string }
89 const server = servers.find(s => s.serverNumber === serverNumber)
90 const res = await server.videos.randomUpload({ wait: false, additionalParams })
92 await waitJobs(servers)
97 // ---------------------------------------------------------------------------
100 checkUploadVideoParam,
101 uploadRandomVideoOnServers,
102 checkVideoFilesWereRemoved,