2020-01-31 09:56:52 -06:00
|
|
|
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */
|
2017-12-29 03:46:27 -06:00
|
|
|
|
|
|
|
import { expect } from 'chai'
|
2021-07-15 03:02:54 -05:00
|
|
|
import { pathExists, readdir } from 'fs-extra'
|
2021-07-22 07:28:03 -05:00
|
|
|
import { basename, join } from 'path'
|
|
|
|
import { HttpStatusCode, VideoCaption, VideoDetails } from '@shared/models'
|
2021-07-15 03:02:54 -05:00
|
|
|
import { waitJobs } from '../server'
|
2021-07-16 02:47:51 -05:00
|
|
|
import { PeerTubeServer } from '../server/server'
|
2021-07-15 03:02:54 -05:00
|
|
|
import { VideoEdit } from './videos-command'
|
2020-11-04 07:16:57 -06:00
|
|
|
|
2021-07-22 07:28:03 -05: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 02:04:35 -05:00
|
|
|
const directoryPath = server.servers.buildDirectory(directory)
|
2018-01-18 03:53:54 -06:00
|
|
|
|
2019-03-05 03:58:44 -06:00
|
|
|
const directoryExists = await pathExists(directoryPath)
|
|
|
|
if (directoryExists === false) continue
|
2018-01-18 03:53:54 -06:00
|
|
|
|
2021-07-22 07:28:03 -05: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 03:53:54 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:28:03 -05: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 04:13:41 -05:00
|
|
|
function checkUploadVideoParam (
|
2021-07-16 02:47:51 -05:00
|
|
|
server: PeerTubeServer,
|
2021-05-10 04:13:41 -05:00
|
|
|
token: string,
|
2021-07-15 03:02:54 -05:00
|
|
|
attributes: Partial<VideoEdit>,
|
|
|
|
expectedStatus = HttpStatusCode.OK_200,
|
2021-05-10 04:13:41 -05:00
|
|
|
mode: 'legacy' | 'resumable' = 'legacy'
|
|
|
|
) {
|
|
|
|
return mode === 'legacy'
|
2021-07-16 02:04:35 -05:00
|
|
|
? server.videos.buildLegacyUpload({ token, attributes, expectedStatus })
|
|
|
|
: server.videos.buildResumeUpload({ token, attributes, expectedStatus })
|
2017-09-07 08:27:35 -05:00
|
|
|
}
|
|
|
|
|
2020-06-16 08:52:05 -05:00
|
|
|
// serverNumber starts from 1
|
2021-07-15 03:02:54 -05:00
|
|
|
async function uploadRandomVideoOnServers (
|
2021-07-16 02:47:51 -05:00
|
|
|
servers: PeerTubeServer[],
|
2021-07-15 03:02:54 -05:00
|
|
|
serverNumber: number,
|
|
|
|
additionalParams?: VideoEdit & { prefixName?: string }
|
|
|
|
) {
|
2020-06-16 08:52:05 -05:00
|
|
|
const server = servers.find(s => s.serverNumber === serverNumber)
|
2021-07-16 07:27:30 -05:00
|
|
|
const res = await server.videos.randomUpload({ wait: false, additionalParams })
|
2020-06-16 08:52:05 -05:00
|
|
|
|
|
|
|
await waitJobs(servers)
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2017-09-04 14:21:47 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2021-05-10 04:13:41 -05:00
|
|
|
checkUploadVideoParam,
|
2021-07-15 03:02:54 -05:00
|
|
|
uploadRandomVideoOnServers,
|
2021-07-22 07:28:03 -05:00
|
|
|
checkVideoFilesWereRemoved,
|
|
|
|
saveVideoInServers
|
2017-09-04 14:21:47 -05:00
|
|
|
}
|