1
0
Fork 0
peertube/shared/extra-utils/videos/videos.ts
lutangar 06aad80165 chore(refactor): remove shared folder dependencies to the server
Many files from the `shared` folder were importing files from the `server` folder.
When attempting to use Typescript project references to describe dependencies,
it highlighted a circular dependency beetween `shared` <-> `server`.

The Typescript project forbid such usages.
Using project references greatly improve performance by rebuilding only
the updated project and not all source files.
> see https://www.typescriptlang.org/docs/handbook/project-references.html
2021-12-16 10:08:43 +01:00

104 lines
3.2 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */
import { expect } from 'chai'
import { pathExists, readdir } from 'fs-extra'
import { basename, join } from 'path'
import { HttpStatusCode, VideoCaption, VideoDetails } from '@shared/models'
import { waitJobs } from '../server'
import { PeerTubeServer } from '../server/server'
import { VideoEdit } from './videos-command'
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)) {
const directoryPath = server.servers.buildDirectory(directory)
const directoryExists = await pathExists(directoryPath)
if (directoryExists === false) continue
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)
}
}
}
}
async function saveVideoInServers (servers: PeerTubeServer[], uuid: string) {
for (const server of servers) {
server.store.videoDetails = await server.videos.get({ id: uuid })
}
}
function checkUploadVideoParam (
server: PeerTubeServer,
token: string,
attributes: Partial<VideoEdit>,
expectedStatus = HttpStatusCode.OK_200,
mode: 'legacy' | 'resumable' = 'legacy'
) {
return mode === 'legacy'
? server.videos.buildLegacyUpload({ token, attributes, expectedStatus })
: server.videos.buildResumeUpload({ token, attributes, expectedStatus })
}
// serverNumber starts from 1
async function uploadRandomVideoOnServers (
servers: PeerTubeServer[],
serverNumber: number,
additionalParams?: VideoEdit & { prefixName?: string }
) {
const server = servers.find(s => s.serverNumber === serverNumber)
const res = await server.videos.randomUpload({ wait: false, additionalParams })
await waitJobs(servers)
return res
}
// ---------------------------------------------------------------------------
export {
checkUploadVideoParam,
uploadRandomVideoOnServers,
checkVideoFilesWereRemoved,
saveVideoInServers
}