aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared
diff options
context:
space:
mode:
authorlutangar <johan.dufour@gmail.com>2021-11-02 19:11:20 +0100
committerChocobozzz <chocobozzz@cpy.re>2021-12-16 10:08:43 +0100
commit06aad80165d09a8863ab8103149a8ff518b10641 (patch)
treea97fa31f3ade29ff807ca1b77704eb47085ab99d /shared
parent854f533c12bd2b88c70f9d5aeab770059e9a6861 (diff)
downloadPeerTube-06aad80165d09a8863ab8103149a8ff518b10641.tar.gz
PeerTube-06aad80165d09a8863ab8103149a8ff518b10641.tar.zst
PeerTube-06aad80165d09a8863ab8103149a8ff518b10641.zip
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
Diffstat (limited to 'shared')
-rw-r--r--shared/core-utils/crypto.ts14
-rw-r--r--shared/core-utils/index.ts2
-rw-r--r--shared/core-utils/path.ts34
-rw-r--r--shared/core-utils/uuid.ts32
-rw-r--r--shared/extra-utils/ffprobe.ts180
-rw-r--r--shared/extra-utils/miscs/checks.ts2
-rw-r--r--shared/extra-utils/miscs/generate.ts2
-rw-r--r--shared/extra-utils/requests/activitypub.ts42
-rw-r--r--shared/extra-utils/server/directories.ts2
-rw-r--r--shared/extra-utils/server/plugins-command.ts2
-rw-r--r--shared/extra-utils/server/server.ts3
-rw-r--r--shared/extra-utils/server/servers-command.ts2
-rw-r--r--shared/extra-utils/server/tracker.ts2
-rw-r--r--shared/extra-utils/users/actors.ts2
-rw-r--r--shared/extra-utils/videos/streaming-playlists.ts2
-rw-r--r--shared/extra-utils/videos/videos-command.ts12
-rw-r--r--shared/extra-utils/videos/videos.ts150
-rw-r--r--shared/tsconfig.json6
18 files changed, 279 insertions, 212 deletions
diff --git a/shared/core-utils/crypto.ts b/shared/core-utils/crypto.ts
new file mode 100644
index 000000000..d6d1150d0
--- /dev/null
+++ b/shared/core-utils/crypto.ts
@@ -0,0 +1,14 @@
1import { BinaryToTextEncoding, createHash } from 'crypto'
2
3function sha256 (str: string | Buffer, encoding: BinaryToTextEncoding = 'hex') {
4 return createHash('sha256').update(str).digest(encoding)
5}
6
7function sha1 (str: string | Buffer, encoding: BinaryToTextEncoding = 'hex') {
8 return createHash('sha1').update(str).digest(encoding)
9}
10
11export {
12 sha256,
13 sha1
14}
diff --git a/shared/core-utils/index.ts b/shared/core-utils/index.ts
index e0a6a8087..ee5cd4412 100644
--- a/shared/core-utils/index.ts
+++ b/shared/core-utils/index.ts
@@ -1,8 +1,10 @@
1export * from './abuse' 1export * from './abuse'
2export * from './common' 2export * from './common'
3export * from './i18n' 3export * from './i18n'
4export * from './path'
4export * from './plugins' 5export * from './plugins'
5export * from './renderer' 6export * from './renderer'
6export * from './users' 7export * from './users'
7export * from './utils' 8export * from './utils'
8export * from './videos' 9export * from './videos'
10export * from './uuid'
diff --git a/shared/core-utils/path.ts b/shared/core-utils/path.ts
new file mode 100644
index 000000000..b1a45d69b
--- /dev/null
+++ b/shared/core-utils/path.ts
@@ -0,0 +1,34 @@
1import { basename, extname, isAbsolute, join, resolve } from 'path'
2
3let rootPath: string
4
5function root () {
6 if (rootPath) return rootPath
7
8 rootPath = __dirname
9
10 if (basename(rootPath) === 'core-utils') rootPath = resolve(rootPath, '..')
11 if (basename(rootPath) === 'shared') rootPath = resolve(rootPath, '..')
12 if (basename(rootPath) === 'server') rootPath = resolve(rootPath, '..')
13 if (basename(rootPath) === 'dist') rootPath = resolve(rootPath, '..')
14
15 return rootPath
16}
17
18function buildPath (path: string) {
19 if (isAbsolute(path)) return path
20
21 return join(root(), path)
22}
23
24function getLowercaseExtension (filename: string) {
25 const ext = extname(filename) || ''
26
27 return ext.toLowerCase()
28}
29
30export {
31 root,
32 buildPath,
33 getLowercaseExtension
34}
diff --git a/shared/core-utils/uuid.ts b/shared/core-utils/uuid.ts
new file mode 100644
index 000000000..f3c80e046
--- /dev/null
+++ b/shared/core-utils/uuid.ts
@@ -0,0 +1,32 @@
1import short, { uuid } from 'short-uuid'
2
3const translator = short()
4
5function buildUUID () {
6 return uuid()
7}
8
9function uuidToShort (uuid: string) {
10 if (!uuid) return uuid
11
12 return translator.fromUUID(uuid)
13}
14
15function shortToUUID (shortUUID: string) {
16 if (!shortUUID) return shortUUID
17
18 return translator.toUUID(shortUUID)
19}
20
21function isShortUUID (value: string) {
22 if (!value) return false
23
24 return value.length === translator.maxLength
25}
26
27export {
28 buildUUID,
29 uuidToShort,
30 shortToUUID,
31 isShortUUID
32}
diff --git a/shared/extra-utils/ffprobe.ts b/shared/extra-utils/ffprobe.ts
new file mode 100644
index 000000000..9257bbd5f
--- /dev/null
+++ b/shared/extra-utils/ffprobe.ts
@@ -0,0 +1,180 @@
1import { ffprobe, FfprobeData } from 'fluent-ffmpeg'
2import { VideoFileMetadata } from '@shared/models/videos'
3
4/**
5 *
6 * Helpers to run ffprobe and extract data from the JSON output
7 *
8 */
9
10function ffprobePromise (path: string) {
11 return new Promise<FfprobeData>((res, rej) => {
12 ffprobe(path, (err, data) => {
13 if (err) return rej(err)
14
15 return res(data)
16 })
17 })
18}
19
20async function getAudioStream (videoPath: string, existingProbe?: FfprobeData) {
21 // without position, ffprobe considers the last input only
22 // we make it consider the first input only
23 // if you pass a file path to pos, then ffprobe acts on that file directly
24 const data = existingProbe || await ffprobePromise(videoPath)
25
26 if (Array.isArray(data.streams)) {
27 const audioStream = data.streams.find(stream => stream['codec_type'] === 'audio')
28
29 if (audioStream) {
30 return {
31 absolutePath: data.format.filename,
32 audioStream,
33 bitrate: parseInt(audioStream['bit_rate'] + '', 10)
34 }
35 }
36 }
37
38 return { absolutePath: data.format.filename }
39}
40
41function getMaxAudioBitrate (type: 'aac' | 'mp3' | string, bitrate: number) {
42 const maxKBitrate = 384
43 const kToBits = (kbits: number) => kbits * 1000
44
45 // If we did not manage to get the bitrate, use an average value
46 if (!bitrate) return 256
47
48 if (type === 'aac') {
49 switch (true) {
50 case bitrate > kToBits(maxKBitrate):
51 return maxKBitrate
52
53 default:
54 return -1 // we interpret it as a signal to copy the audio stream as is
55 }
56 }
57
58 /*
59 a 192kbit/sec mp3 doesn't hold as much information as a 192kbit/sec aac.
60 That's why, when using aac, we can go to lower kbit/sec. The equivalences
61 made here are not made to be accurate, especially with good mp3 encoders.
62 */
63 switch (true) {
64 case bitrate <= kToBits(192):
65 return 128
66
67 case bitrate <= kToBits(384):
68 return 256
69
70 default:
71 return maxKBitrate
72 }
73}
74
75async function getVideoStreamSize (path: string, existingProbe?: FfprobeData): Promise<{ width: number, height: number }> {
76 const videoStream = await getVideoStreamFromFile(path, existingProbe)
77
78 return videoStream === null
79 ? { width: 0, height: 0 }
80 : { width: videoStream.width, height: videoStream.height }
81}
82
83async function getVideoFileResolution (path: string, existingProbe?: FfprobeData) {
84 const size = await getVideoStreamSize(path, existingProbe)
85
86 return {
87 width: size.width,
88 height: size.height,
89 ratio: Math.max(size.height, size.width) / Math.min(size.height, size.width),
90 resolution: Math.min(size.height, size.width),
91 isPortraitMode: size.height > size.width
92 }
93}
94
95async function getVideoFileFPS (path: string, existingProbe?: FfprobeData) {
96 const videoStream = await getVideoStreamFromFile(path, existingProbe)
97 if (videoStream === null) return 0
98
99 for (const key of [ 'avg_frame_rate', 'r_frame_rate' ]) {
100 const valuesText: string = videoStream[key]
101 if (!valuesText) continue
102
103 const [ frames, seconds ] = valuesText.split('/')
104 if (!frames || !seconds) continue
105
106 const result = parseInt(frames, 10) / parseInt(seconds, 10)
107 if (result > 0) return Math.round(result)
108 }
109
110 return 0
111}
112
113async function getMetadataFromFile (path: string, existingProbe?: FfprobeData) {
114 const metadata = existingProbe || await ffprobePromise(path)
115
116 return new VideoFileMetadata(metadata)
117}
118
119async function getVideoFileBitrate (path: string, existingProbe?: FfprobeData): Promise<number> {
120 const metadata = await getMetadataFromFile(path, existingProbe)
121
122 let bitrate = metadata.format.bit_rate as number
123 if (bitrate && !isNaN(bitrate)) return bitrate
124
125 const videoStream = await getVideoStreamFromFile(path, existingProbe)
126 if (!videoStream) return undefined
127
128 bitrate = videoStream?.bit_rate
129 if (bitrate && !isNaN(bitrate)) return bitrate
130
131 return undefined
132}
133
134async function getDurationFromVideoFile (path: string, existingProbe?: FfprobeData) {
135 const metadata = await getMetadataFromFile(path, existingProbe)
136
137 return Math.round(metadata.format.duration)
138}
139
140async function getVideoStreamFromFile (path: string, existingProbe?: FfprobeData) {
141 const metadata = await getMetadataFromFile(path, existingProbe)
142
143 return metadata.streams.find(s => s.codec_type === 'video') || null
144}
145
146async function canDoQuickAudioTranscode (path: string, probe?: FfprobeData): Promise<boolean> {
147 const parsedAudio = await getAudioStream(path, probe)
148
149 if (!parsedAudio.audioStream) return true
150
151 if (parsedAudio.audioStream['codec_name'] !== 'aac') return false
152
153 const audioBitrate = parsedAudio.bitrate
154 if (!audioBitrate) return false
155
156 const maxAudioBitrate = getMaxAudioBitrate('aac', audioBitrate)
157 if (maxAudioBitrate !== -1 && audioBitrate > maxAudioBitrate) return false
158
159 const channelLayout = parsedAudio.audioStream['channel_layout']
160 // Causes playback issues with Chrome
161 if (!channelLayout || channelLayout === 'unknown') return false
162
163 return true
164}
165
166// ---------------------------------------------------------------------------
167
168export {
169 getVideoStreamSize,
170 getVideoFileResolution,
171 getMetadataFromFile,
172 getMaxAudioBitrate,
173 getVideoStreamFromFile,
174 getDurationFromVideoFile,
175 getAudioStream,
176 getVideoFileFPS,
177 ffprobePromise,
178 getVideoFileBitrate,
179 canDoQuickAudioTranscode
180}
diff --git a/shared/extra-utils/miscs/checks.ts b/shared/extra-utils/miscs/checks.ts
index b1be214b1..589928997 100644
--- a/shared/extra-utils/miscs/checks.ts
+++ b/shared/extra-utils/miscs/checks.ts
@@ -3,7 +3,7 @@
3import { expect } from 'chai' 3import { expect } from 'chai'
4import { pathExists, readFile } from 'fs-extra' 4import { pathExists, readFile } from 'fs-extra'
5import { join } from 'path' 5import { join } from 'path'
6import { root } from '@server/helpers/core-utils' 6import { root } from '@shared/core-utils'
7import { HttpStatusCode } from '@shared/models' 7import { HttpStatusCode } from '@shared/models'
8import { makeGetRequest } from '../requests' 8import { makeGetRequest } from '../requests'
9import { PeerTubeServer } from '../server' 9import { PeerTubeServer } from '../server'
diff --git a/shared/extra-utils/miscs/generate.ts b/shared/extra-utils/miscs/generate.ts
index 3b29c0ad4..93673a063 100644
--- a/shared/extra-utils/miscs/generate.ts
+++ b/shared/extra-utils/miscs/generate.ts
@@ -2,7 +2,7 @@ import { expect } from 'chai'
2import ffmpeg from 'fluent-ffmpeg' 2import ffmpeg from 'fluent-ffmpeg'
3import { ensureDir, pathExists } from 'fs-extra' 3import { ensureDir, pathExists } from 'fs-extra'
4import { dirname } from 'path' 4import { dirname } from 'path'
5import { getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '@server/helpers/ffprobe-utils' 5import { getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '@shared/extra-utils/ffprobe'
6import { getMaxBitrate } from '@shared/core-utils' 6import { getMaxBitrate } from '@shared/core-utils'
7import { buildAbsoluteFixturePath } from './tests' 7import { buildAbsoluteFixturePath } from './tests'
8 8
diff --git a/shared/extra-utils/requests/activitypub.ts b/shared/extra-utils/requests/activitypub.ts
deleted file mode 100644
index 4ae878384..000000000
--- a/shared/extra-utils/requests/activitypub.ts
+++ /dev/null
@@ -1,42 +0,0 @@
1import { activityPubContextify } from '../../../server/helpers/activitypub'
2import { doRequest } from '../../../server/helpers/requests'
3import { HTTP_SIGNATURE } from '../../../server/initializers/constants'
4import { buildGlobalHeaders } from '../../../server/lib/job-queue/handlers/utils/activitypub-http-utils'
5
6function makePOSTAPRequest (url: string, body: any, httpSignature: any, headers: any) {
7 const options = {
8 method: 'POST' as 'POST',
9 json: body,
10 httpSignature,
11 headers
12 }
13
14 return doRequest(url, options)
15}
16
17async function makeFollowRequest (to: { url: string }, by: { url: string, privateKey }) {
18 const follow = {
19 type: 'Follow',
20 id: by.url + '/' + new Date().getTime(),
21 actor: by.url,
22 object: to.url
23 }
24
25 const body = activityPubContextify(follow)
26
27 const httpSignature = {
28 algorithm: HTTP_SIGNATURE.ALGORITHM,
29 authorizationHeaderName: HTTP_SIGNATURE.HEADER_NAME,
30 keyId: by.url,
31 key: by.privateKey,
32 headers: HTTP_SIGNATURE.HEADERS_TO_SIGN
33 }
34 const headers = buildGlobalHeaders(body)
35
36 return makePOSTAPRequest(to.url + '/inbox', body, httpSignature, headers)
37}
38
39export {
40 makePOSTAPRequest,
41 makeFollowRequest
42}
diff --git a/shared/extra-utils/server/directories.ts b/shared/extra-utils/server/directories.ts
index b6465cbf4..e6f72d6fc 100644
--- a/shared/extra-utils/server/directories.ts
+++ b/shared/extra-utils/server/directories.ts
@@ -3,7 +3,7 @@
3import { expect } from 'chai' 3import { expect } from 'chai'
4import { pathExists, readdir } from 'fs-extra' 4import { pathExists, readdir } from 'fs-extra'
5import { join } from 'path' 5import { join } from 'path'
6import { root } from '@server/helpers/core-utils' 6import { root } from '@shared/core-utils'
7import { PeerTubeServer } from './server' 7import { PeerTubeServer } from './server'
8 8
9async function checkTmpIsEmpty (server: PeerTubeServer) { 9async function checkTmpIsEmpty (server: PeerTubeServer) {
diff --git a/shared/extra-utils/server/plugins-command.ts b/shared/extra-utils/server/plugins-command.ts
index 9bf24afff..1c44711da 100644
--- a/shared/extra-utils/server/plugins-command.ts
+++ b/shared/extra-utils/server/plugins-command.ts
@@ -2,7 +2,7 @@
2 2
3import { readJSON, writeJSON } from 'fs-extra' 3import { readJSON, writeJSON } from 'fs-extra'
4import { join } from 'path' 4import { join } from 'path'
5import { root } from '@server/helpers/core-utils' 5import { root } from '@shared/core-utils'
6import { 6import {
7 HttpStatusCode, 7 HttpStatusCode,
8 PeerTubePlugin, 8 PeerTubePlugin,
diff --git a/shared/extra-utils/server/server.ts b/shared/extra-utils/server/server.ts
index 9da293877..339b9cabb 100644
--- a/shared/extra-utils/server/server.ts
+++ b/shared/extra-utils/server/server.ts
@@ -1,8 +1,7 @@
1import { ChildProcess, fork } from 'child_process' 1import { ChildProcess, fork } from 'child_process'
2import { copy } from 'fs-extra' 2import { copy } from 'fs-extra'
3import { join } from 'path' 3import { join } from 'path'
4import { root } from '@server/helpers/core-utils' 4import { root, randomInt } from '@shared/core-utils'
5import { randomInt } from '@shared/core-utils'
6import { Video, VideoChannel, VideoCreateResult, VideoDetails } from '../../models/videos' 5import { Video, VideoChannel, VideoCreateResult, VideoDetails } from '../../models/videos'
7import { BulkCommand } from '../bulk' 6import { BulkCommand } from '../bulk'
8import { CLICommand } from '../cli' 7import { CLICommand } from '../cli'
diff --git a/shared/extra-utils/server/servers-command.ts b/shared/extra-utils/server/servers-command.ts
index 776d2123c..47420c95f 100644
--- a/shared/extra-utils/server/servers-command.ts
+++ b/shared/extra-utils/server/servers-command.ts
@@ -1,7 +1,7 @@
1import { exec } from 'child_process' 1import { exec } from 'child_process'
2import { copy, ensureDir, readFile, remove } from 'fs-extra' 2import { copy, ensureDir, readFile, remove } from 'fs-extra'
3import { basename, join } from 'path' 3import { basename, join } from 'path'
4import { root } from '@server/helpers/core-utils' 4import { root } from '@shared/core-utils'
5import { HttpStatusCode } from '@shared/models' 5import { HttpStatusCode } from '@shared/models'
6import { getFileSize, isGithubCI, wait } from '../miscs' 6import { getFileSize, isGithubCI, wait } from '../miscs'
7import { AbstractCommand, OverrideCommandOptions } from '../shared' 7import { AbstractCommand, OverrideCommandOptions } from '../shared'
diff --git a/shared/extra-utils/server/tracker.ts b/shared/extra-utils/server/tracker.ts
index f04e8f8a1..ed43a5924 100644
--- a/shared/extra-utils/server/tracker.ts
+++ b/shared/extra-utils/server/tracker.ts
@@ -1,5 +1,5 @@
1import { expect } from 'chai' 1import { expect } from 'chai'
2import { sha1 } from '@server/helpers/core-utils' 2import { sha1 } from '@shared/core-utils/crypto'
3import { makeGetRequest } from '../requests' 3import { makeGetRequest } from '../requests'
4 4
5async function hlsInfohashExist (serverUrl: string, masterPlaylistUrl: string, fileNumber: number) { 5async function hlsInfohashExist (serverUrl: string, masterPlaylistUrl: string, fileNumber: number) {
diff --git a/shared/extra-utils/users/actors.ts b/shared/extra-utils/users/actors.ts
index cfcc7d0a7..12c3e078a 100644
--- a/shared/extra-utils/users/actors.ts
+++ b/shared/extra-utils/users/actors.ts
@@ -3,7 +3,7 @@
3import { expect } from 'chai' 3import { expect } from 'chai'
4import { pathExists, readdir } from 'fs-extra' 4import { pathExists, readdir } from 'fs-extra'
5import { join } from 'path' 5import { join } from 'path'
6import { root } from '@server/helpers/core-utils' 6import { root } from '@shared/core-utils'
7import { Account, VideoChannel } from '@shared/models' 7import { Account, VideoChannel } from '@shared/models'
8import { PeerTubeServer } from '../server' 8import { PeerTubeServer } from '../server'
9 9
diff --git a/shared/extra-utils/videos/streaming-playlists.ts b/shared/extra-utils/videos/streaming-playlists.ts
index 6671e3fa6..0451c0efe 100644
--- a/shared/extra-utils/videos/streaming-playlists.ts
+++ b/shared/extra-utils/videos/streaming-playlists.ts
@@ -1,6 +1,6 @@
1import { expect } from 'chai' 1import { expect } from 'chai'
2import { basename } from 'path' 2import { basename } from 'path'
3import { sha256 } from '@server/helpers/core-utils' 3import { sha256 } from '@shared/core-utils/crypto'
4import { removeFragmentedMP4Ext } from '@shared/core-utils' 4import { removeFragmentedMP4Ext } from '@shared/core-utils'
5import { HttpStatusCode, VideoStreamingPlaylist } from '@shared/models' 5import { HttpStatusCode, VideoStreamingPlaylist } from '@shared/models'
6import { PeerTubeServer } from '../server' 6import { PeerTubeServer } from '../server'
diff --git a/shared/extra-utils/videos/videos-command.ts b/shared/extra-utils/videos/videos-command.ts
index 7ec9c3647..8ea828b40 100644
--- a/shared/extra-utils/videos/videos-command.ts
+++ b/shared/extra-utils/videos/videos-command.ts
@@ -5,8 +5,7 @@ import { createReadStream, stat } from 'fs-extra'
5import got, { Response as GotResponse } from 'got' 5import got, { Response as GotResponse } from 'got'
6import { omit } from 'lodash' 6import { omit } from 'lodash'
7import validator from 'validator' 7import validator from 'validator'
8import { buildUUID } from '@server/helpers/uuid' 8import { buildUUID } from '@shared/core-utils/uuid'
9import { loadLanguages } from '@server/initializers/constants'
10import { pick } from '@shared/core-utils' 9import { pick } from '@shared/core-utils'
11import { 10import {
12 HttpStatusCode, 11 HttpStatusCode,
@@ -23,7 +22,7 @@ import {
23} from '@shared/models' 22} from '@shared/models'
24import { buildAbsoluteFixturePath, wait } from '../miscs' 23import { buildAbsoluteFixturePath, wait } from '../miscs'
25import { unwrapBody } from '../requests' 24import { unwrapBody } from '../requests'
26import { PeerTubeServer, waitJobs } from '../server' 25import { waitJobs } from '../server'
27import { AbstractCommand, OverrideCommandOptions } from '../shared' 26import { AbstractCommand, OverrideCommandOptions } from '../shared'
28 27
29export type VideoEdit = Partial<Omit<VideoCreate, 'thumbnailfile' | 'previewfile'>> & { 28export type VideoEdit = Partial<Omit<VideoCreate, 'thumbnailfile' | 'previewfile'>> & {
@@ -33,13 +32,6 @@ export type VideoEdit = Partial<Omit<VideoCreate, 'thumbnailfile' | 'previewfile
33} 32}
34 33
35export class VideosCommand extends AbstractCommand { 34export class VideosCommand extends AbstractCommand {
36
37 constructor (server: PeerTubeServer) {
38 super(server)
39
40 loadLanguages()
41 }
42
43 getCategories (options: OverrideCommandOptions = {}) { 35 getCategories (options: OverrideCommandOptions = {}) {
44 const path = '/api/v1/videos/categories' 36 const path = '/api/v1/videos/categories'
45 37
diff --git a/shared/extra-utils/videos/videos.ts b/shared/extra-utils/videos/videos.ts
index c05c2be6c..2c3464aa8 100644
--- a/shared/extra-utils/videos/videos.ts
+++ b/shared/extra-utils/videos/videos.ts
@@ -3,12 +3,7 @@
3import { expect } from 'chai' 3import { expect } from 'chai'
4import { pathExists, readdir } from 'fs-extra' 4import { pathExists, readdir } from 'fs-extra'
5import { basename, join } from 'path' 5import { basename, join } from 'path'
6import { getLowercaseExtension } from '@server/helpers/core-utils'
7import { uuidRegex } from '@shared/core-utils'
8import { HttpStatusCode, VideoCaption, VideoDetails } from '@shared/models' 6import { HttpStatusCode, VideoCaption, VideoDetails } from '@shared/models'
9import { VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../server/initializers/constants'
10import { dateIsValid, testImage, webtorrentAdd } from '../miscs'
11import { makeRawRequest } from '../requests/requests'
12import { waitJobs } from '../server' 7import { waitJobs } from '../server'
13import { PeerTubeServer } from '../server/server' 8import { PeerTubeServer } from '../server/server'
14import { VideoEdit } from './videos-command' 9import { VideoEdit } from './videos-command'
@@ -85,150 +80,6 @@ function checkUploadVideoParam (
85 : server.videos.buildResumeUpload({ token, attributes, expectedStatus }) 80 : server.videos.buildResumeUpload({ token, attributes, expectedStatus })
86} 81}
87 82
88async function completeVideoCheck (
89 server: PeerTubeServer,
90 video: any,
91 attributes: {
92 name: string
93 category: number
94 licence: number
95 language: string
96 nsfw: boolean
97 commentsEnabled: boolean
98 downloadEnabled: boolean
99 description: string
100 publishedAt?: string
101 support: string
102 originallyPublishedAt?: string
103 account: {
104 name: string
105 host: string
106 }
107 isLocal: boolean
108 tags: string[]
109 privacy: number
110 likes?: number
111 dislikes?: number
112 duration: number
113 channel: {
114 displayName: string
115 name: string
116 description: string
117 isLocal: boolean
118 }
119 fixture: string
120 files: {
121 resolution: number
122 size: number
123 }[]
124 thumbnailfile?: string
125 previewfile?: string
126 }
127) {
128 if (!attributes.likes) attributes.likes = 0
129 if (!attributes.dislikes) attributes.dislikes = 0
130
131 const host = new URL(server.url).host
132 const originHost = attributes.account.host
133
134 expect(video.name).to.equal(attributes.name)
135 expect(video.category.id).to.equal(attributes.category)
136 expect(video.category.label).to.equal(attributes.category !== null ? VIDEO_CATEGORIES[attributes.category] : 'Misc')
137 expect(video.licence.id).to.equal(attributes.licence)
138 expect(video.licence.label).to.equal(attributes.licence !== null ? VIDEO_LICENCES[attributes.licence] : 'Unknown')
139 expect(video.language.id).to.equal(attributes.language)
140 expect(video.language.label).to.equal(attributes.language !== null ? VIDEO_LANGUAGES[attributes.language] : 'Unknown')
141 expect(video.privacy.id).to.deep.equal(attributes.privacy)
142 expect(video.privacy.label).to.deep.equal(VIDEO_PRIVACIES[attributes.privacy])
143 expect(video.nsfw).to.equal(attributes.nsfw)
144 expect(video.description).to.equal(attributes.description)
145 expect(video.account.id).to.be.a('number')
146 expect(video.account.host).to.equal(attributes.account.host)
147 expect(video.account.name).to.equal(attributes.account.name)
148 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
149 expect(video.channel.name).to.equal(attributes.channel.name)
150 expect(video.likes).to.equal(attributes.likes)
151 expect(video.dislikes).to.equal(attributes.dislikes)
152 expect(video.isLocal).to.equal(attributes.isLocal)
153 expect(video.duration).to.equal(attributes.duration)
154 expect(video.url).to.contain(originHost)
155 expect(dateIsValid(video.createdAt)).to.be.true
156 expect(dateIsValid(video.publishedAt)).to.be.true
157 expect(dateIsValid(video.updatedAt)).to.be.true
158
159 if (attributes.publishedAt) {
160 expect(video.publishedAt).to.equal(attributes.publishedAt)
161 }
162
163 if (attributes.originallyPublishedAt) {
164 expect(video.originallyPublishedAt).to.equal(attributes.originallyPublishedAt)
165 } else {
166 expect(video.originallyPublishedAt).to.be.null
167 }
168
169 const videoDetails = await server.videos.get({ id: video.uuid })
170
171 expect(videoDetails.files).to.have.lengthOf(attributes.files.length)
172 expect(videoDetails.tags).to.deep.equal(attributes.tags)
173 expect(videoDetails.account.name).to.equal(attributes.account.name)
174 expect(videoDetails.account.host).to.equal(attributes.account.host)
175 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
176 expect(video.channel.name).to.equal(attributes.channel.name)
177 expect(videoDetails.channel.host).to.equal(attributes.account.host)
178 expect(videoDetails.channel.isLocal).to.equal(attributes.channel.isLocal)
179 expect(dateIsValid(videoDetails.channel.createdAt.toString())).to.be.true
180 expect(dateIsValid(videoDetails.channel.updatedAt.toString())).to.be.true
181 expect(videoDetails.commentsEnabled).to.equal(attributes.commentsEnabled)
182 expect(videoDetails.downloadEnabled).to.equal(attributes.downloadEnabled)
183
184 for (const attributeFile of attributes.files) {
185 const file = videoDetails.files.find(f => f.resolution.id === attributeFile.resolution)
186 expect(file).not.to.be.undefined
187
188 let extension = getLowercaseExtension(attributes.fixture)
189 // Transcoding enabled: extension will always be .mp4
190 if (attributes.files.length > 1) extension = '.mp4'
191
192 expect(file.magnetUri).to.have.lengthOf.above(2)
193
194 expect(file.torrentDownloadUrl).to.match(new RegExp(`http://${host}/download/torrents/${uuidRegex}-${file.resolution.id}.torrent`))
195 expect(file.torrentUrl).to.match(new RegExp(`http://${host}/lazy-static/torrents/${uuidRegex}-${file.resolution.id}.torrent`))
196
197 expect(file.fileUrl).to.match(new RegExp(`http://${originHost}/static/webseed/${uuidRegex}-${file.resolution.id}${extension}`))
198 expect(file.fileDownloadUrl).to.match(new RegExp(`http://${originHost}/download/videos/${uuidRegex}-${file.resolution.id}${extension}`))
199
200 await Promise.all([
201 makeRawRequest(file.torrentUrl, 200),
202 makeRawRequest(file.torrentDownloadUrl, 200),
203 makeRawRequest(file.metadataUrl, 200)
204 ])
205
206 expect(file.resolution.id).to.equal(attributeFile.resolution)
207 expect(file.resolution.label).to.equal(attributeFile.resolution + 'p')
208
209 const minSize = attributeFile.size - ((10 * attributeFile.size) / 100)
210 const maxSize = attributeFile.size + ((10 * attributeFile.size) / 100)
211 expect(
212 file.size,
213 'File size for resolution ' + file.resolution.label + ' outside confidence interval (' + minSize + '> size <' + maxSize + ')'
214 ).to.be.above(minSize).and.below(maxSize)
215
216 const torrent = await webtorrentAdd(file.magnetUri, true)
217 expect(torrent.files).to.be.an('array')
218 expect(torrent.files.length).to.equal(1)
219 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
220 expect(torrent.files[0].name).to.equal(`${videoDetails.name} ${file.resolution.id}p${extension}`)
221 }
222
223 expect(videoDetails.thumbnailPath).to.exist
224 await testImage(server.url, attributes.thumbnailfile || attributes.fixture, videoDetails.thumbnailPath)
225
226 if (attributes.previewfile) {
227 expect(videoDetails.previewPath).to.exist
228 await testImage(server.url, attributes.previewfile, videoDetails.previewPath)
229 }
230}
231
232// serverNumber starts from 1 83// serverNumber starts from 1
233async function uploadRandomVideoOnServers ( 84async function uploadRandomVideoOnServers (
234 servers: PeerTubeServer[], 85 servers: PeerTubeServer[],
@@ -247,7 +98,6 @@ async function uploadRandomVideoOnServers (
247 98
248export { 99export {
249 checkUploadVideoParam, 100 checkUploadVideoParam,
250 completeVideoCheck,
251 uploadRandomVideoOnServers, 101 uploadRandomVideoOnServers,
252 checkVideoFilesWereRemoved, 102 checkVideoFilesWereRemoved,
253 saveVideoInServers 103 saveVideoInServers
diff --git a/shared/tsconfig.json b/shared/tsconfig.json
new file mode 100644
index 000000000..95892077b
--- /dev/null
+++ b/shared/tsconfig.json
@@ -0,0 +1,6 @@
1{
2 "extends": "../tsconfig.base.json",
3 "compilerOptions": {
4 "outDir": "../dist/shared"
5 }
6}