diff options
author | lutangar <johan.dufour@gmail.com> | 2021-11-02 19:11:20 +0100 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2021-12-16 10:08:43 +0100 |
commit | 06aad80165d09a8863ab8103149a8ff518b10641 (patch) | |
tree | a97fa31f3ade29ff807ca1b77704eb47085ab99d /shared/extra-utils | |
parent | 854f533c12bd2b88c70f9d5aeab770059e9a6861 (diff) | |
download | PeerTube-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/extra-utils')
-rw-r--r-- | shared/extra-utils/ffprobe.ts | 180 | ||||
-rw-r--r-- | shared/extra-utils/miscs/checks.ts | 2 | ||||
-rw-r--r-- | shared/extra-utils/miscs/generate.ts | 2 | ||||
-rw-r--r-- | shared/extra-utils/requests/activitypub.ts | 42 | ||||
-rw-r--r-- | shared/extra-utils/server/directories.ts | 2 | ||||
-rw-r--r-- | shared/extra-utils/server/plugins-command.ts | 2 | ||||
-rw-r--r-- | shared/extra-utils/server/server.ts | 3 | ||||
-rw-r--r-- | shared/extra-utils/server/servers-command.ts | 2 | ||||
-rw-r--r-- | shared/extra-utils/server/tracker.ts | 2 | ||||
-rw-r--r-- | shared/extra-utils/users/actors.ts | 2 | ||||
-rw-r--r-- | shared/extra-utils/videos/streaming-playlists.ts | 2 | ||||
-rw-r--r-- | shared/extra-utils/videos/videos-command.ts | 12 | ||||
-rw-r--r-- | shared/extra-utils/videos/videos.ts | 150 |
13 files changed, 191 insertions, 212 deletions
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 @@ | |||
1 | import { ffprobe, FfprobeData } from 'fluent-ffmpeg' | ||
2 | import { VideoFileMetadata } from '@shared/models/videos' | ||
3 | |||
4 | /** | ||
5 | * | ||
6 | * Helpers to run ffprobe and extract data from the JSON output | ||
7 | * | ||
8 | */ | ||
9 | |||
10 | function 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 | |||
20 | async 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 | |||
41 | function 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 | |||
75 | async 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 | |||
83 | async 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 | |||
95 | async 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 | |||
113 | async function getMetadataFromFile (path: string, existingProbe?: FfprobeData) { | ||
114 | const metadata = existingProbe || await ffprobePromise(path) | ||
115 | |||
116 | return new VideoFileMetadata(metadata) | ||
117 | } | ||
118 | |||
119 | async 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 | |||
134 | async function getDurationFromVideoFile (path: string, existingProbe?: FfprobeData) { | ||
135 | const metadata = await getMetadataFromFile(path, existingProbe) | ||
136 | |||
137 | return Math.round(metadata.format.duration) | ||
138 | } | ||
139 | |||
140 | async 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 | |||
146 | async 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 | |||
168 | export { | ||
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 @@ | |||
3 | import { expect } from 'chai' | 3 | import { expect } from 'chai' |
4 | import { pathExists, readFile } from 'fs-extra' | 4 | import { pathExists, readFile } from 'fs-extra' |
5 | import { join } from 'path' | 5 | import { join } from 'path' |
6 | import { root } from '@server/helpers/core-utils' | 6 | import { root } from '@shared/core-utils' |
7 | import { HttpStatusCode } from '@shared/models' | 7 | import { HttpStatusCode } from '@shared/models' |
8 | import { makeGetRequest } from '../requests' | 8 | import { makeGetRequest } from '../requests' |
9 | import { PeerTubeServer } from '../server' | 9 | import { 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' | |||
2 | import ffmpeg from 'fluent-ffmpeg' | 2 | import ffmpeg from 'fluent-ffmpeg' |
3 | import { ensureDir, pathExists } from 'fs-extra' | 3 | import { ensureDir, pathExists } from 'fs-extra' |
4 | import { dirname } from 'path' | 4 | import { dirname } from 'path' |
5 | import { getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '@server/helpers/ffprobe-utils' | 5 | import { getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '@shared/extra-utils/ffprobe' |
6 | import { getMaxBitrate } from '@shared/core-utils' | 6 | import { getMaxBitrate } from '@shared/core-utils' |
7 | import { buildAbsoluteFixturePath } from './tests' | 7 | import { 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 @@ | |||
1 | import { activityPubContextify } from '../../../server/helpers/activitypub' | ||
2 | import { doRequest } from '../../../server/helpers/requests' | ||
3 | import { HTTP_SIGNATURE } from '../../../server/initializers/constants' | ||
4 | import { buildGlobalHeaders } from '../../../server/lib/job-queue/handlers/utils/activitypub-http-utils' | ||
5 | |||
6 | function 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 | |||
17 | async 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 | |||
39 | export { | ||
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 @@ | |||
3 | import { expect } from 'chai' | 3 | import { expect } from 'chai' |
4 | import { pathExists, readdir } from 'fs-extra' | 4 | import { pathExists, readdir } from 'fs-extra' |
5 | import { join } from 'path' | 5 | import { join } from 'path' |
6 | import { root } from '@server/helpers/core-utils' | 6 | import { root } from '@shared/core-utils' |
7 | import { PeerTubeServer } from './server' | 7 | import { PeerTubeServer } from './server' |
8 | 8 | ||
9 | async function checkTmpIsEmpty (server: PeerTubeServer) { | 9 | async 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 | ||
3 | import { readJSON, writeJSON } from 'fs-extra' | 3 | import { readJSON, writeJSON } from 'fs-extra' |
4 | import { join } from 'path' | 4 | import { join } from 'path' |
5 | import { root } from '@server/helpers/core-utils' | 5 | import { root } from '@shared/core-utils' |
6 | import { | 6 | import { |
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 @@ | |||
1 | import { ChildProcess, fork } from 'child_process' | 1 | import { ChildProcess, fork } from 'child_process' |
2 | import { copy } from 'fs-extra' | 2 | import { copy } from 'fs-extra' |
3 | import { join } from 'path' | 3 | import { join } from 'path' |
4 | import { root } from '@server/helpers/core-utils' | 4 | import { root, randomInt } from '@shared/core-utils' |
5 | import { randomInt } from '@shared/core-utils' | ||
6 | import { Video, VideoChannel, VideoCreateResult, VideoDetails } from '../../models/videos' | 5 | import { Video, VideoChannel, VideoCreateResult, VideoDetails } from '../../models/videos' |
7 | import { BulkCommand } from '../bulk' | 6 | import { BulkCommand } from '../bulk' |
8 | import { CLICommand } from '../cli' | 7 | import { 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 @@ | |||
1 | import { exec } from 'child_process' | 1 | import { exec } from 'child_process' |
2 | import { copy, ensureDir, readFile, remove } from 'fs-extra' | 2 | import { copy, ensureDir, readFile, remove } from 'fs-extra' |
3 | import { basename, join } from 'path' | 3 | import { basename, join } from 'path' |
4 | import { root } from '@server/helpers/core-utils' | 4 | import { root } from '@shared/core-utils' |
5 | import { HttpStatusCode } from '@shared/models' | 5 | import { HttpStatusCode } from '@shared/models' |
6 | import { getFileSize, isGithubCI, wait } from '../miscs' | 6 | import { getFileSize, isGithubCI, wait } from '../miscs' |
7 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | 7 | import { 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 @@ | |||
1 | import { expect } from 'chai' | 1 | import { expect } from 'chai' |
2 | import { sha1 } from '@server/helpers/core-utils' | 2 | import { sha1 } from '@shared/core-utils/crypto' |
3 | import { makeGetRequest } from '../requests' | 3 | import { makeGetRequest } from '../requests' |
4 | 4 | ||
5 | async function hlsInfohashExist (serverUrl: string, masterPlaylistUrl: string, fileNumber: number) { | 5 | async 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 @@ | |||
3 | import { expect } from 'chai' | 3 | import { expect } from 'chai' |
4 | import { pathExists, readdir } from 'fs-extra' | 4 | import { pathExists, readdir } from 'fs-extra' |
5 | import { join } from 'path' | 5 | import { join } from 'path' |
6 | import { root } from '@server/helpers/core-utils' | 6 | import { root } from '@shared/core-utils' |
7 | import { Account, VideoChannel } from '@shared/models' | 7 | import { Account, VideoChannel } from '@shared/models' |
8 | import { PeerTubeServer } from '../server' | 8 | import { 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 @@ | |||
1 | import { expect } from 'chai' | 1 | import { expect } from 'chai' |
2 | import { basename } from 'path' | 2 | import { basename } from 'path' |
3 | import { sha256 } from '@server/helpers/core-utils' | 3 | import { sha256 } from '@shared/core-utils/crypto' |
4 | import { removeFragmentedMP4Ext } from '@shared/core-utils' | 4 | import { removeFragmentedMP4Ext } from '@shared/core-utils' |
5 | import { HttpStatusCode, VideoStreamingPlaylist } from '@shared/models' | 5 | import { HttpStatusCode, VideoStreamingPlaylist } from '@shared/models' |
6 | import { PeerTubeServer } from '../server' | 6 | import { 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' | |||
5 | import got, { Response as GotResponse } from 'got' | 5 | import got, { Response as GotResponse } from 'got' |
6 | import { omit } from 'lodash' | 6 | import { omit } from 'lodash' |
7 | import validator from 'validator' | 7 | import validator from 'validator' |
8 | import { buildUUID } from '@server/helpers/uuid' | 8 | import { buildUUID } from '@shared/core-utils/uuid' |
9 | import { loadLanguages } from '@server/initializers/constants' | ||
10 | import { pick } from '@shared/core-utils' | 9 | import { pick } from '@shared/core-utils' |
11 | import { | 10 | import { |
12 | HttpStatusCode, | 11 | HttpStatusCode, |
@@ -23,7 +22,7 @@ import { | |||
23 | } from '@shared/models' | 22 | } from '@shared/models' |
24 | import { buildAbsoluteFixturePath, wait } from '../miscs' | 23 | import { buildAbsoluteFixturePath, wait } from '../miscs' |
25 | import { unwrapBody } from '../requests' | 24 | import { unwrapBody } from '../requests' |
26 | import { PeerTubeServer, waitJobs } from '../server' | 25 | import { waitJobs } from '../server' |
27 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | 26 | import { AbstractCommand, OverrideCommandOptions } from '../shared' |
28 | 27 | ||
29 | export type VideoEdit = Partial<Omit<VideoCreate, 'thumbnailfile' | 'previewfile'>> & { | 28 | export type VideoEdit = Partial<Omit<VideoCreate, 'thumbnailfile' | 'previewfile'>> & { |
@@ -33,13 +32,6 @@ export type VideoEdit = Partial<Omit<VideoCreate, 'thumbnailfile' | 'previewfile | |||
33 | } | 32 | } |
34 | 33 | ||
35 | export class VideosCommand extends AbstractCommand { | 34 | export 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 @@ | |||
3 | import { expect } from 'chai' | 3 | import { expect } from 'chai' |
4 | import { pathExists, readdir } from 'fs-extra' | 4 | import { pathExists, readdir } from 'fs-extra' |
5 | import { basename, join } from 'path' | 5 | import { basename, join } from 'path' |
6 | import { getLowercaseExtension } from '@server/helpers/core-utils' | ||
7 | import { uuidRegex } from '@shared/core-utils' | ||
8 | import { HttpStatusCode, VideoCaption, VideoDetails } from '@shared/models' | 6 | import { HttpStatusCode, VideoCaption, VideoDetails } from '@shared/models' |
9 | import { VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../server/initializers/constants' | ||
10 | import { dateIsValid, testImage, webtorrentAdd } from '../miscs' | ||
11 | import { makeRawRequest } from '../requests/requests' | ||
12 | import { waitJobs } from '../server' | 7 | import { waitJobs } from '../server' |
13 | import { PeerTubeServer } from '../server/server' | 8 | import { PeerTubeServer } from '../server/server' |
14 | import { VideoEdit } from './videos-command' | 9 | import { 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 | ||
88 | async 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 |
233 | async function uploadRandomVideoOnServers ( | 84 | async function uploadRandomVideoOnServers ( |
234 | servers: PeerTubeServer[], | 85 | servers: PeerTubeServer[], |
@@ -247,7 +98,6 @@ async function uploadRandomVideoOnServers ( | |||
247 | 98 | ||
248 | export { | 99 | export { |
249 | checkUploadVideoParam, | 100 | checkUploadVideoParam, |
250 | completeVideoCheck, | ||
251 | uploadRandomVideoOnServers, | 101 | uploadRandomVideoOnServers, |
252 | checkVideoFilesWereRemoved, | 102 | checkVideoFilesWereRemoved, |
253 | saveVideoInServers | 103 | saveVideoInServers |