diff options
Diffstat (limited to 'shared/server-commands/miscs')
-rw-r--r-- | shared/server-commands/miscs/index.ts | 2 | ||||
-rw-r--r-- | shared/server-commands/miscs/sql-command.ts | 146 | ||||
-rw-r--r-- | shared/server-commands/miscs/webtorrent.ts | 46 |
3 files changed, 0 insertions, 194 deletions
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 @@ | |||
1 | export * from './sql-command' | ||
2 | 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 @@ | |||
1 | import { QueryTypes, Sequelize } from 'sequelize' | ||
2 | import { forceNumber } from '@shared/core-utils' | ||
3 | import { AbstractCommand } from '../shared' | ||
4 | |||
5 | export class SQLCommand extends AbstractCommand { | ||
6 | private sequelize: Sequelize | ||
7 | |||
8 | deleteAll (table: string) { | ||
9 | const seq = this.getSequelize() | ||
10 | |||
11 | const options = { type: QueryTypes.DELETE } | ||
12 | |||
13 | return seq.query(`DELETE FROM "${table}"`, options) | ||
14 | } | ||
15 | |||
16 | async getVideoShareCount () { | ||
17 | const [ { total } ] = await this.selectQuery<{ total: string }>(`SELECT COUNT(*) as total FROM "videoShare"`) | ||
18 | if (total === null) return 0 | ||
19 | |||
20 | return parseInt(total, 10) | ||
21 | } | ||
22 | |||
23 | async getInternalFileUrl (fileId: number) { | ||
24 | return this.selectQuery<{ fileUrl: string }>(`SELECT "fileUrl" FROM "videoFile" WHERE id = :fileId`, { fileId }) | ||
25 | .then(rows => rows[0].fileUrl) | ||
26 | } | ||
27 | |||
28 | setActorField (to: string, field: string, value: string) { | ||
29 | return this.updateQuery(`UPDATE actor SET ${this.escapeColumnName(field)} = :value WHERE url = :to`, { value, to }) | ||
30 | } | ||
31 | |||
32 | setVideoField (uuid: string, field: string, value: string) { | ||
33 | return this.updateQuery(`UPDATE video SET ${this.escapeColumnName(field)} = :value WHERE uuid = :uuid`, { value, uuid }) | ||
34 | } | ||
35 | |||
36 | setPlaylistField (uuid: string, field: string, value: string) { | ||
37 | return this.updateQuery(`UPDATE "videoPlaylist" SET ${this.escapeColumnName(field)} = :value WHERE uuid = :uuid`, { value, uuid }) | ||
38 | } | ||
39 | |||
40 | async countVideoViewsOf (uuid: string) { | ||
41 | const query = 'SELECT SUM("videoView"."views") AS "total" FROM "videoView" ' + | ||
42 | `INNER JOIN "video" ON "video"."id" = "videoView"."videoId" WHERE "video"."uuid" = :uuid` | ||
43 | |||
44 | const [ { total } ] = await this.selectQuery<{ total: number }>(query, { uuid }) | ||
45 | if (!total) return 0 | ||
46 | |||
47 | return forceNumber(total) | ||
48 | } | ||
49 | |||
50 | getActorImage (filename: string) { | ||
51 | return this.selectQuery<{ width: number, height: number }>(`SELECT * FROM "actorImage" WHERE filename = :filename`, { filename }) | ||
52 | .then(rows => rows[0]) | ||
53 | } | ||
54 | |||
55 | // --------------------------------------------------------------------------- | ||
56 | |||
57 | setPluginVersion (pluginName: string, newVersion: string) { | ||
58 | return this.setPluginField(pluginName, 'version', newVersion) | ||
59 | } | ||
60 | |||
61 | setPluginLatestVersion (pluginName: string, newVersion: string) { | ||
62 | return this.setPluginField(pluginName, 'latestVersion', newVersion) | ||
63 | } | ||
64 | |||
65 | setPluginField (pluginName: string, field: string, value: string) { | ||
66 | return this.updateQuery( | ||
67 | `UPDATE "plugin" SET ${this.escapeColumnName(field)} = :value WHERE "name" = :pluginName`, | ||
68 | { pluginName, value } | ||
69 | ) | ||
70 | } | ||
71 | |||
72 | // --------------------------------------------------------------------------- | ||
73 | |||
74 | selectQuery <T extends object> (query: string, replacements: { [id: string]: string | number } = {}) { | ||
75 | const seq = this.getSequelize() | ||
76 | const options = { | ||
77 | type: QueryTypes.SELECT as QueryTypes.SELECT, | ||
78 | replacements | ||
79 | } | ||
80 | |||
81 | return seq.query<T>(query, options) | ||
82 | } | ||
83 | |||
84 | updateQuery (query: string, replacements: { [id: string]: string | number } = {}) { | ||
85 | const seq = this.getSequelize() | ||
86 | const options = { type: QueryTypes.UPDATE as QueryTypes.UPDATE, replacements } | ||
87 | |||
88 | return seq.query(query, options) | ||
89 | } | ||
90 | |||
91 | // --------------------------------------------------------------------------- | ||
92 | |||
93 | async getPlaylistInfohash (playlistId: number) { | ||
94 | const query = 'SELECT "p2pMediaLoaderInfohashes" FROM "videoStreamingPlaylist" WHERE id = :playlistId' | ||
95 | |||
96 | const result = await this.selectQuery<{ p2pMediaLoaderInfohashes: string }>(query, { playlistId }) | ||
97 | if (!result || result.length === 0) return [] | ||
98 | |||
99 | return result[0].p2pMediaLoaderInfohashes | ||
100 | } | ||
101 | |||
102 | // --------------------------------------------------------------------------- | ||
103 | |||
104 | setActorFollowScores (newScore: number) { | ||
105 | return this.updateQuery(`UPDATE "actorFollow" SET "score" = :newScore`, { newScore }) | ||
106 | } | ||
107 | |||
108 | setTokenField (accessToken: string, field: string, value: string) { | ||
109 | return this.updateQuery( | ||
110 | `UPDATE "oAuthToken" SET ${this.escapeColumnName(field)} = :value WHERE "accessToken" = :accessToken`, | ||
111 | { value, accessToken } | ||
112 | ) | ||
113 | } | ||
114 | |||
115 | async cleanup () { | ||
116 | if (!this.sequelize) return | ||
117 | |||
118 | await this.sequelize.close() | ||
119 | this.sequelize = undefined | ||
120 | } | ||
121 | |||
122 | private getSequelize () { | ||
123 | if (this.sequelize) return this.sequelize | ||
124 | |||
125 | const dbname = 'peertube_test' + this.server.internalServerNumber | ||
126 | const username = 'peertube' | ||
127 | const password = 'peertube' | ||
128 | const host = '127.0.0.1' | ||
129 | const port = 5432 | ||
130 | |||
131 | this.sequelize = new Sequelize(dbname, username, password, { | ||
132 | dialect: 'postgres', | ||
133 | host, | ||
134 | port, | ||
135 | logging: false | ||
136 | }) | ||
137 | |||
138 | return this.sequelize | ||
139 | } | ||
140 | |||
141 | private escapeColumnName (columnName: string) { | ||
142 | return this.getSequelize().escape(columnName) | ||
143 | .replace(/^'/, '"') | ||
144 | .replace(/'$/, '"') | ||
145 | } | ||
146 | } | ||
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 @@ | |||
1 | import { readFile } from 'fs-extra' | ||
2 | import parseTorrent from 'parse-torrent' | ||
3 | import { basename, join } from 'path' | ||
4 | import * as WebTorrent from 'webtorrent' | ||
5 | import { VideoFile } from '@shared/models' | ||
6 | import { PeerTubeServer } from '../server' | ||
7 | |||
8 | let webtorrent: WebTorrent.Instance | ||
9 | |||
10 | function webtorrentAdd (torrentId: string, refreshWebTorrent = false) { | ||
11 | const WebTorrent = require('webtorrent') | ||
12 | |||
13 | if (webtorrent && refreshWebTorrent) webtorrent.destroy() | ||
14 | if (!webtorrent || refreshWebTorrent) webtorrent = new WebTorrent() | ||
15 | |||
16 | webtorrent.on('error', err => console.error('Error in webtorrent', err)) | ||
17 | |||
18 | return new Promise<WebTorrent.Torrent>(res => { | ||
19 | const torrent = webtorrent.add(torrentId, res) | ||
20 | |||
21 | torrent.on('error', err => console.error('Error in webtorrent torrent', err)) | ||
22 | torrent.on('warning', warn => { | ||
23 | const msg = typeof warn === 'string' | ||
24 | ? warn | ||
25 | : warn.message | ||
26 | |||
27 | if (msg.includes('Unsupported')) return | ||
28 | |||
29 | console.error('Warning in webtorrent torrent', warn) | ||
30 | }) | ||
31 | }) | ||
32 | } | ||
33 | |||
34 | async function parseTorrentVideo (server: PeerTubeServer, file: VideoFile) { | ||
35 | const torrentName = basename(file.torrentUrl) | ||
36 | const torrentPath = server.servers.buildDirectory(join('torrents', torrentName)) | ||
37 | |||
38 | const data = await readFile(torrentPath) | ||
39 | |||
40 | return parseTorrent(data) | ||
41 | } | ||
42 | |||
43 | export { | ||
44 | webtorrentAdd, | ||
45 | parseTorrentVideo | ||
46 | } | ||