From d102de1b38f2877463529c3b27bd35ffef4fd8bf Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 21 Apr 2023 15:00:01 +0200 Subject: Add runner server tests --- shared/server-commands/miscs/index.ts | 2 - shared/server-commands/miscs/sql-command.ts | 146 ---------------------------- shared/server-commands/miscs/webtorrent.ts | 46 --------- 3 files changed, 194 deletions(-) delete mode 100644 shared/server-commands/miscs/index.ts delete mode 100644 shared/server-commands/miscs/sql-command.ts delete mode 100644 shared/server-commands/miscs/webtorrent.ts (limited to 'shared/server-commands/miscs') diff --git a/shared/server-commands/miscs/index.ts b/shared/server-commands/miscs/index.ts deleted file mode 100644 index a1d14e998..000000000 --- a/shared/server-commands/miscs/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './sql-command' -export * from './webtorrent' diff --git a/shared/server-commands/miscs/sql-command.ts b/shared/server-commands/miscs/sql-command.ts deleted file mode 100644 index 35cc2253f..000000000 --- a/shared/server-commands/miscs/sql-command.ts +++ /dev/null @@ -1,146 +0,0 @@ -import { QueryTypes, Sequelize } from 'sequelize' -import { forceNumber } from '@shared/core-utils' -import { AbstractCommand } from '../shared' - -export class SQLCommand extends AbstractCommand { - private sequelize: Sequelize - - deleteAll (table: string) { - const seq = this.getSequelize() - - const options = { type: QueryTypes.DELETE } - - return seq.query(`DELETE FROM "${table}"`, options) - } - - async getVideoShareCount () { - const [ { total } ] = await this.selectQuery<{ total: string }>(`SELECT COUNT(*) as total FROM "videoShare"`) - if (total === null) return 0 - - return parseInt(total, 10) - } - - async getInternalFileUrl (fileId: number) { - return this.selectQuery<{ fileUrl: string }>(`SELECT "fileUrl" FROM "videoFile" WHERE id = :fileId`, { fileId }) - .then(rows => rows[0].fileUrl) - } - - setActorField (to: string, field: string, value: string) { - return this.updateQuery(`UPDATE actor SET ${this.escapeColumnName(field)} = :value WHERE url = :to`, { value, to }) - } - - setVideoField (uuid: string, field: string, value: string) { - return this.updateQuery(`UPDATE video SET ${this.escapeColumnName(field)} = :value WHERE uuid = :uuid`, { value, uuid }) - } - - setPlaylistField (uuid: string, field: string, value: string) { - return this.updateQuery(`UPDATE "videoPlaylist" SET ${this.escapeColumnName(field)} = :value WHERE uuid = :uuid`, { value, uuid }) - } - - async countVideoViewsOf (uuid: string) { - const query = 'SELECT SUM("videoView"."views") AS "total" FROM "videoView" ' + - `INNER JOIN "video" ON "video"."id" = "videoView"."videoId" WHERE "video"."uuid" = :uuid` - - const [ { total } ] = await this.selectQuery<{ total: number }>(query, { uuid }) - if (!total) return 0 - - return forceNumber(total) - } - - getActorImage (filename: string) { - return this.selectQuery<{ width: number, height: number }>(`SELECT * FROM "actorImage" WHERE filename = :filename`, { filename }) - .then(rows => rows[0]) - } - - // --------------------------------------------------------------------------- - - setPluginVersion (pluginName: string, newVersion: string) { - return this.setPluginField(pluginName, 'version', newVersion) - } - - setPluginLatestVersion (pluginName: string, newVersion: string) { - return this.setPluginField(pluginName, 'latestVersion', newVersion) - } - - setPluginField (pluginName: string, field: string, value: string) { - return this.updateQuery( - `UPDATE "plugin" SET ${this.escapeColumnName(field)} = :value WHERE "name" = :pluginName`, - { pluginName, value } - ) - } - - // --------------------------------------------------------------------------- - - selectQuery (query: string, replacements: { [id: string]: string | number } = {}) { - const seq = this.getSequelize() - const options = { - type: QueryTypes.SELECT as QueryTypes.SELECT, - replacements - } - - return seq.query(query, options) - } - - updateQuery (query: string, replacements: { [id: string]: string | number } = {}) { - const seq = this.getSequelize() - const options = { type: QueryTypes.UPDATE as QueryTypes.UPDATE, replacements } - - return seq.query(query, options) - } - - // --------------------------------------------------------------------------- - - async getPlaylistInfohash (playlistId: number) { - const query = 'SELECT "p2pMediaLoaderInfohashes" FROM "videoStreamingPlaylist" WHERE id = :playlistId' - - const result = await this.selectQuery<{ p2pMediaLoaderInfohashes: string }>(query, { playlistId }) - if (!result || result.length === 0) return [] - - return result[0].p2pMediaLoaderInfohashes - } - - // --------------------------------------------------------------------------- - - setActorFollowScores (newScore: number) { - return this.updateQuery(`UPDATE "actorFollow" SET "score" = :newScore`, { newScore }) - } - - setTokenField (accessToken: string, field: string, value: string) { - return this.updateQuery( - `UPDATE "oAuthToken" SET ${this.escapeColumnName(field)} = :value WHERE "accessToken" = :accessToken`, - { value, accessToken } - ) - } - - async cleanup () { - if (!this.sequelize) return - - await this.sequelize.close() - this.sequelize = undefined - } - - private getSequelize () { - if (this.sequelize) return this.sequelize - - const dbname = 'peertube_test' + this.server.internalServerNumber - const username = 'peertube' - const password = 'peertube' - const host = '127.0.0.1' - const port = 5432 - - this.sequelize = new Sequelize(dbname, username, password, { - dialect: 'postgres', - host, - port, - logging: false - }) - - return this.sequelize - } - - private escapeColumnName (columnName: string) { - return this.getSequelize().escape(columnName) - .replace(/^'/, '"') - .replace(/'$/, '"') - } -} diff --git a/shared/server-commands/miscs/webtorrent.ts b/shared/server-commands/miscs/webtorrent.ts deleted file mode 100644 index 0683f8893..000000000 --- a/shared/server-commands/miscs/webtorrent.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { readFile } from 'fs-extra' -import parseTorrent from 'parse-torrent' -import { basename, join } from 'path' -import * as WebTorrent from 'webtorrent' -import { VideoFile } from '@shared/models' -import { PeerTubeServer } from '../server' - -let webtorrent: WebTorrent.Instance - -function webtorrentAdd (torrentId: string, refreshWebTorrent = false) { - const WebTorrent = require('webtorrent') - - if (webtorrent && refreshWebTorrent) webtorrent.destroy() - if (!webtorrent || refreshWebTorrent) webtorrent = new WebTorrent() - - webtorrent.on('error', err => console.error('Error in webtorrent', err)) - - return new Promise(res => { - const torrent = webtorrent.add(torrentId, res) - - torrent.on('error', err => console.error('Error in webtorrent torrent', err)) - torrent.on('warning', warn => { - const msg = typeof warn === 'string' - ? warn - : warn.message - - if (msg.includes('Unsupported')) return - - console.error('Warning in webtorrent torrent', warn) - }) - }) -} - -async function parseTorrentVideo (server: PeerTubeServer, file: VideoFile) { - const torrentName = basename(file.torrentUrl) - const torrentPath = server.servers.buildDirectory(join('torrents', torrentName)) - - const data = await readFile(torrentPath) - - return parseTorrent(data) -} - -export { - webtorrentAdd, - parseTorrentVideo -} -- cgit v1.2.3