PeerTube/packages/server-commands/src/server/servers-command.ts
chagai95 e57c3024f4
Add config option to keep original video file (basic first version) (#6157)
* testing not removing old file and adding columb to db

* implement feature

* remove unnecessary config changes

* use only keptOriginalFileName, change keptOriginalFileName to keptOriginalFilename for consistency with with videoFile table, slight refactor with basename()

* save original video files to dedicated directory original-video-files

* begin implementing object storage (bucket) support

---------

Co-authored-by: chagai.friedlander <chagai.friedlander@fairkom.eu>
Co-authored-by: Ian <ian.kraft@hotmail.com>
Co-authored-by: Chocobozzz <me@florianbigard.com>
2024-03-15 15:47:18 +01:00

103 lines
3 KiB
TypeScript

import { wait } from '@peertube/peertube-core-utils'
import { HttpStatusCode } from '@peertube/peertube-models'
import { isGithubCI, root } from '@peertube/peertube-node-utils'
import { exec } from 'child_process'
import { copy, ensureDir, remove } from 'fs-extra/esm'
import { readFile, readdir } from 'fs/promises'
import { basename, join } from 'path'
import { AbstractCommand, OverrideCommandOptions } from '../shared/index.js'
export class ServersCommand extends AbstractCommand {
static flushTests (internalServerNumber: number) {
return new Promise<void>((res, rej) => {
const suffix = ` -- ${internalServerNumber}`
return exec('npm run clean:server:test' + suffix, (err, _stdout, stderr) => {
if (err || stderr) return rej(err || new Error(stderr))
return res()
})
})
}
ping (options: OverrideCommandOptions = {}) {
return this.getRequestBody({
...options,
path: '/api/v1/ping',
implicitToken: false,
defaultExpectedStatus: HttpStatusCode.OK_200
})
}
cleanupTests () {
const promises: Promise<any>[] = []
const saveGithubLogsIfNeeded = async () => {
if (!isGithubCI()) return
await ensureDir('artifacts')
const origin = this.buildDirectory('logs/peertube.log')
const destname = `peertube-${this.server.internalServerNumber}.log`
console.log('Saving logs %s.', destname)
await copy(origin, join('artifacts', destname))
}
if (this.server.parallel) {
const promise = saveGithubLogsIfNeeded()
.then(() => ServersCommand.flushTests(this.server.internalServerNumber))
promises.push(promise)
}
if (this.server.customConfigFile) {
promises.push(remove(this.server.customConfigFile))
}
return promises
}
async waitUntilLog (str: string, count = 1, strictCount = true) {
const logfile = this.buildDirectory('logs/peertube.log')
while (true) {
const buf = await readFile(logfile)
const matches = buf.toString().match(new RegExp(str, 'g'))
if (matches && matches.length === count) return
if (matches && strictCount === false && matches.length >= count) return
await wait(1000)
}
}
buildDirectory (directory: string) {
return join(root(), 'test' + this.server.internalServerNumber, directory)
}
async countFiles (directory: string) {
const files = await readdir(this.buildDirectory(directory))
return files.length
}
// ---------------------------------------------------------------------------
buildWebVideoFilePath (fileUrl: string) {
return this.buildDirectory(join('web-videos', basename(fileUrl)))
}
buildFragmentedFilePath (videoUUID: string, fileUrl: string) {
return this.buildDirectory(join('streaming-playlists', 'hls', videoUUID, basename(fileUrl)))
}
// ---------------------------------------------------------------------------
getLogContent () {
return readFile(this.buildDirectory('logs/peertube.log'))
}
}