2020-01-31 10:56:52 -05:00
|
|
|
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */
|
2017-12-29 04:46:27 -05:00
|
|
|
|
|
|
|
import { expect } from 'chai'
|
2021-07-15 04:02:54 -04:00
|
|
|
import { pathExists, readdir } from 'fs-extra'
|
2021-07-22 08:28:03 -04:00
|
|
|
import { basename, join } from 'path'
|
|
|
|
import { HttpStatusCode, VideoCaption, VideoDetails } from '@shared/models'
|
2021-07-15 04:02:54 -04:00
|
|
|
import { waitJobs } from '../server'
|
2021-07-16 03:47:51 -04:00
|
|
|
import { PeerTubeServer } from '../server/server'
|
2021-07-15 04:02:54 -04:00
|
|
|
import { VideoEdit } from './videos-command'
|
2020-11-04 08:16:57 -05:00
|
|
|
|
2021-07-22 08:28:03 -04:00
|
|
|
async function checkVideoFilesWereRemoved (options: {
|
|
|
|
server: PeerTubeServer
|
|
|
|
video: VideoDetails
|
|
|
|
captions?: VideoCaption[]
|
|
|
|
onlyVideoFiles?: boolean // default false
|
|
|
|
}) {
|
|
|
|
const { video, server, captions = [], onlyVideoFiles = false } = options
|
|
|
|
|
|
|
|
const webtorrentFiles = video.files || []
|
|
|
|
const hlsFiles = video.streamingPlaylists[0]?.files || []
|
|
|
|
|
|
|
|
const thumbnailName = basename(video.thumbnailPath)
|
|
|
|
const previewName = basename(video.previewPath)
|
|
|
|
|
|
|
|
const torrentNames = webtorrentFiles.concat(hlsFiles).map(f => basename(f.torrentUrl))
|
|
|
|
|
|
|
|
const captionNames = captions.map(c => basename(c.captionPath))
|
|
|
|
|
|
|
|
const webtorrentFilenames = webtorrentFiles.map(f => basename(f.fileUrl))
|
|
|
|
const hlsFilenames = hlsFiles.map(f => basename(f.fileUrl))
|
|
|
|
|
|
|
|
let directories: { [ directory: string ]: string[] } = {
|
|
|
|
videos: webtorrentFilenames,
|
|
|
|
redundancy: webtorrentFilenames,
|
|
|
|
[join('playlists', 'hls')]: hlsFilenames,
|
|
|
|
[join('redundancy', 'hls')]: hlsFilenames
|
|
|
|
}
|
|
|
|
|
|
|
|
if (onlyVideoFiles !== true) {
|
|
|
|
directories = {
|
|
|
|
...directories,
|
|
|
|
|
|
|
|
thumbnails: [ thumbnailName ],
|
|
|
|
previews: [ previewName ],
|
|
|
|
torrents: torrentNames,
|
|
|
|
captions: captionNames
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const directory of Object.keys(directories)) {
|
2021-07-16 03:04:35 -04:00
|
|
|
const directoryPath = server.servers.buildDirectory(directory)
|
2018-01-18 04:53:54 -05:00
|
|
|
|
2019-03-05 04:58:44 -05:00
|
|
|
const directoryExists = await pathExists(directoryPath)
|
|
|
|
if (directoryExists === false) continue
|
2018-01-18 04:53:54 -05:00
|
|
|
|
2021-07-22 08:28:03 -04:00
|
|
|
const existingFiles = await readdir(directoryPath)
|
|
|
|
for (const existingFile of existingFiles) {
|
|
|
|
for (const shouldNotExist of directories[directory]) {
|
|
|
|
expect(existingFile, `File ${existingFile} should not exist in ${directoryPath}`).to.not.contain(shouldNotExist)
|
|
|
|
}
|
2018-01-18 04:53:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 08:28:03 -04:00
|
|
|
async function saveVideoInServers (servers: PeerTubeServer[], uuid: string) {
|
|
|
|
for (const server of servers) {
|
|
|
|
server.store.videoDetails = await server.videos.get({ id: uuid })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-10 05:13:41 -04:00
|
|
|
function checkUploadVideoParam (
|
2021-07-16 03:47:51 -04:00
|
|
|
server: PeerTubeServer,
|
2021-05-10 05:13:41 -04:00
|
|
|
token: string,
|
2021-07-15 04:02:54 -04:00
|
|
|
attributes: Partial<VideoEdit>,
|
|
|
|
expectedStatus = HttpStatusCode.OK_200,
|
2021-05-10 05:13:41 -04:00
|
|
|
mode: 'legacy' | 'resumable' = 'legacy'
|
|
|
|
) {
|
|
|
|
return mode === 'legacy'
|
2021-07-16 03:04:35 -04:00
|
|
|
? server.videos.buildLegacyUpload({ token, attributes, expectedStatus })
|
|
|
|
: server.videos.buildResumeUpload({ token, attributes, expectedStatus })
|
2017-09-07 09:27:35 -04:00
|
|
|
}
|
|
|
|
|
2020-06-16 09:52:05 -04:00
|
|
|
// serverNumber starts from 1
|
2021-07-15 04:02:54 -04:00
|
|
|
async function uploadRandomVideoOnServers (
|
2021-07-16 03:47:51 -04:00
|
|
|
servers: PeerTubeServer[],
|
2021-07-15 04:02:54 -04:00
|
|
|
serverNumber: number,
|
|
|
|
additionalParams?: VideoEdit & { prefixName?: string }
|
|
|
|
) {
|
2020-06-16 09:52:05 -04:00
|
|
|
const server = servers.find(s => s.serverNumber === serverNumber)
|
2021-07-16 08:27:30 -04:00
|
|
|
const res = await server.videos.randomUpload({ wait: false, additionalParams })
|
2020-06-16 09:52:05 -04:00
|
|
|
|
|
|
|
await waitJobs(servers)
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2017-09-04 15:21:47 -04:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2021-05-10 05:13:41 -04:00
|
|
|
checkUploadVideoParam,
|
2021-07-15 04:02:54 -04:00
|
|
|
uploadRandomVideoOnServers,
|
2021-07-22 08:28:03 -04:00
|
|
|
checkVideoFilesWereRemoved,
|
|
|
|
saveVideoInServers
|
2017-09-04 15:21:47 -04:00
|
|
|
}
|