aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers
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 /server/helpers
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 'server/helpers')
-rw-r--r--server/helpers/core-utils.ts47
-rw-r--r--server/helpers/custom-validators/misc.ts2
-rw-r--r--server/helpers/express-utils.ts6
-rw-r--r--server/helpers/ffprobe-utils.ts173
-rw-r--r--server/helpers/image-utils.ts4
-rw-r--r--server/helpers/peertube-crypto.ts3
-rw-r--r--server/helpers/utils.ts3
-rw-r--r--server/helpers/uuid.ts32
-rw-r--r--server/helpers/webtorrent.ts3
9 files changed, 28 insertions, 245 deletions
diff --git a/server/helpers/core-utils.ts b/server/helpers/core-utils.ts
index 2cbf0f8fe..531ccfba9 100644
--- a/server/helpers/core-utils.ts
+++ b/server/helpers/core-utils.ts
@@ -6,9 +6,8 @@
6*/ 6*/
7 7
8import { exec, ExecOptions } from 'child_process' 8import { exec, ExecOptions } from 'child_process'
9import { BinaryToTextEncoding, createHash, randomBytes } from 'crypto' 9import { randomBytes } from 'crypto'
10import { truncate } from 'lodash' 10import { truncate } from 'lodash'
11import { basename, extname, isAbsolute, join, resolve } from 'path'
12import { createPrivateKey as createPrivateKey_1, getPublicKey as getPublicKey_1 } from 'pem' 11import { createPrivateKey as createPrivateKey_1, getPublicKey as getPublicKey_1 } from 'pem'
13import { pipeline } from 'stream' 12import { pipeline } from 'stream'
14import { URL } from 'url' 13import { URL } from 'url'
@@ -159,34 +158,6 @@ function getAppNumber () {
159 158
160// --------------------------------------------------------------------------- 159// ---------------------------------------------------------------------------
161 160
162let rootPath: string
163
164function root () {
165 if (rootPath) return rootPath
166
167 rootPath = __dirname
168
169 if (basename(rootPath) === 'helpers') rootPath = resolve(rootPath, '..')
170 if (basename(rootPath) === 'server') rootPath = resolve(rootPath, '..')
171 if (basename(rootPath) === 'dist') rootPath = resolve(rootPath, '..')
172
173 return rootPath
174}
175
176function buildPath (path: string) {
177 if (isAbsolute(path)) return path
178
179 return join(root(), path)
180}
181
182function getLowercaseExtension (filename: string) {
183 const ext = extname(filename) || ''
184
185 return ext.toLowerCase()
186}
187
188// ---------------------------------------------------------------------------
189
190// Consistent with .length, lodash truncate function is not 161// Consistent with .length, lodash truncate function is not
191function peertubeTruncate (str: string, options: { length: number, separator?: RegExp, omission?: string }) { 162function peertubeTruncate (str: string, options: { length: number, separator?: RegExp, omission?: string }) {
192 const truncatedStr = truncate(str, options) 163 const truncatedStr = truncate(str, options)
@@ -221,16 +192,6 @@ function parseSemVersion (s: string) {
221 192
222// --------------------------------------------------------------------------- 193// ---------------------------------------------------------------------------
223 194
224function sha256 (str: string | Buffer, encoding: BinaryToTextEncoding = 'hex') {
225 return createHash('sha256').update(str).digest(encoding)
226}
227
228function sha1 (str: string | Buffer, encoding: BinaryToTextEncoding = 'hex') {
229 return createHash('sha1').update(str).digest(encoding)
230}
231
232// ---------------------------------------------------------------------------
233
234function execShell (command: string, options?: ExecOptions) { 195function execShell (command: string, options?: ExecOptions) {
235 return new Promise<{ err?: Error, stdout: string, stderr: string }>((res, rej) => { 196 return new Promise<{ err?: Error, stdout: string, stderr: string }>((res, rej) => {
236 exec(command, options, (err, stdout, stderr) => { 197 exec(command, options, (err, stdout, stderr) => {
@@ -298,9 +259,6 @@ export {
298 objectConverter, 259 objectConverter,
299 mapToJSON, 260 mapToJSON,
300 261
301 root,
302 buildPath,
303 getLowercaseExtension,
304 sanitizeUrl, 262 sanitizeUrl,
305 sanitizeHost, 263 sanitizeHost,
306 264
@@ -309,9 +267,6 @@ export {
309 pageToStartAndCount, 267 pageToStartAndCount,
310 peertubeTruncate, 268 peertubeTruncate,
311 269
312 sha256,
313 sha1,
314
315 promisify0, 270 promisify0,
316 promisify1, 271 promisify1,
317 promisify2, 272 promisify2,
diff --git a/server/helpers/custom-validators/misc.ts b/server/helpers/custom-validators/misc.ts
index c19a3e5eb..eaabdbbea 100644
--- a/server/helpers/custom-validators/misc.ts
+++ b/server/helpers/custom-validators/misc.ts
@@ -2,7 +2,7 @@ import 'multer'
2import { UploadFilesForCheck } from 'express' 2import { UploadFilesForCheck } from 'express'
3import { sep } from 'path' 3import { sep } from 'path'
4import validator from 'validator' 4import validator from 'validator'
5import { isShortUUID, shortToUUID } from '../uuid' 5import { isShortUUID, shortToUUID } from '@shared/core-utils/uuid'
6 6
7function exists (value: any) { 7function exists (value: any) {
8 return value !== undefined && value !== null 8 return value !== undefined && value !== null
diff --git a/server/helpers/express-utils.ts b/server/helpers/express-utils.ts
index 7b81ed71b..780fd6345 100644
--- a/server/helpers/express-utils.ts
+++ b/server/helpers/express-utils.ts
@@ -1,9 +1,9 @@
1import express from 'express' 1import express, { RequestHandler } from 'express'
2import multer, { diskStorage } from 'multer' 2import multer, { diskStorage } from 'multer'
3import { HttpStatusCode } from '../../shared/models/http/http-error-codes' 3import { HttpStatusCode } from '../../shared/models/http/http-error-codes'
4import { CONFIG } from '../initializers/config' 4import { CONFIG } from '../initializers/config'
5import { REMOTE_SCHEME } from '../initializers/constants' 5import { REMOTE_SCHEME } from '../initializers/constants'
6import { getLowercaseExtension } from './core-utils' 6import { getLowercaseExtension } from '@shared/core-utils'
7import { isArray } from './custom-validators/misc' 7import { isArray } from './custom-validators/misc'
8import { logger } from './logger' 8import { logger } from './logger'
9import { deleteFileAndCatch, generateRandomString } from './utils' 9import { deleteFileAndCatch, generateRandomString } from './utils'
@@ -69,7 +69,7 @@ function createReqFiles (
69 fieldNames: string[], 69 fieldNames: string[],
70 mimeTypes: { [id: string]: string | string[] }, 70 mimeTypes: { [id: string]: string | string[] },
71 destinations: { [fieldName: string]: string } 71 destinations: { [fieldName: string]: string }
72) { 72): RequestHandler {
73 const storage = diskStorage({ 73 const storage = diskStorage({
74 destination: (req, file, cb) => { 74 destination: (req, file, cb) => {
75 cb(null, destinations[file.fieldname]) 75 cb(null, destinations[file.fieldname])
diff --git a/server/helpers/ffprobe-utils.ts b/server/helpers/ffprobe-utils.ts
index e15628e2a..595112bce 100644
--- a/server/helpers/ffprobe-utils.ts
+++ b/server/helpers/ffprobe-utils.ts
@@ -1,9 +1,22 @@
1import { ffprobe, FfprobeData } from 'fluent-ffmpeg' 1import { FfprobeData } from 'fluent-ffmpeg'
2import { getMaxBitrate } from '@shared/core-utils' 2import { getMaxBitrate } from '@shared/core-utils'
3import { VideoFileMetadata, VideoResolution, VideoTranscodingFPS } from '../../shared/models/videos' 3import { VideoResolution, VideoTranscodingFPS } from '../../shared/models/videos'
4import { CONFIG } from '../initializers/config' 4import { CONFIG } from '../initializers/config'
5import { VIDEO_TRANSCODING_FPS } from '../initializers/constants' 5import { VIDEO_TRANSCODING_FPS } from '../initializers/constants'
6import { logger } from './logger' 6import { logger } from './logger'
7import {
8 canDoQuickAudioTranscode,
9 ffprobePromise,
10 getDurationFromVideoFile,
11 getAudioStream,
12 getMaxAudioBitrate,
13 getMetadataFromFile,
14 getVideoFileBitrate,
15 getVideoFileFPS,
16 getVideoFileResolution,
17 getVideoStreamFromFile,
18 getVideoStreamSize
19} from '@shared/extra-utils/ffprobe'
7 20
8/** 21/**
9 * 22 *
@@ -11,79 +24,6 @@ import { logger } from './logger'
11 * 24 *
12 */ 25 */
13 26
14function ffprobePromise (path: string) {
15 return new Promise<FfprobeData>((res, rej) => {
16 ffprobe(path, (err, data) => {
17 if (err) return rej(err)
18
19 return res(data)
20 })
21 })
22}
23
24async function getAudioStream (videoPath: string, existingProbe?: FfprobeData) {
25 // without position, ffprobe considers the last input only
26 // we make it consider the first input only
27 // if you pass a file path to pos, then ffprobe acts on that file directly
28 const data = existingProbe || await ffprobePromise(videoPath)
29
30 if (Array.isArray(data.streams)) {
31 const audioStream = data.streams.find(stream => stream['codec_type'] === 'audio')
32
33 if (audioStream) {
34 return {
35 absolutePath: data.format.filename,
36 audioStream,
37 bitrate: parseInt(audioStream['bit_rate'] + '', 10)
38 }
39 }
40 }
41
42 return { absolutePath: data.format.filename }
43}
44
45function getMaxAudioBitrate (type: 'aac' | 'mp3' | string, bitrate: number) {
46 const maxKBitrate = 384
47 const kToBits = (kbits: number) => kbits * 1000
48
49 // If we did not manage to get the bitrate, use an average value
50 if (!bitrate) return 256
51
52 if (type === 'aac') {
53 switch (true) {
54 case bitrate > kToBits(maxKBitrate):
55 return maxKBitrate
56
57 default:
58 return -1 // we interpret it as a signal to copy the audio stream as is
59 }
60 }
61
62 /*
63 a 192kbit/sec mp3 doesn't hold as much information as a 192kbit/sec aac.
64 That's why, when using aac, we can go to lower kbit/sec. The equivalences
65 made here are not made to be accurate, especially with good mp3 encoders.
66 */
67 switch (true) {
68 case bitrate <= kToBits(192):
69 return 128
70
71 case bitrate <= kToBits(384):
72 return 256
73
74 default:
75 return maxKBitrate
76 }
77}
78
79async function getVideoStreamSize (path: string, existingProbe?: FfprobeData): Promise<{ width: number, height: number }> {
80 const videoStream = await getVideoStreamFromFile(path, existingProbe)
81
82 return videoStream === null
83 ? { width: 0, height: 0 }
84 : { width: videoStream.width, height: videoStream.height }
85}
86
87async function getVideoStreamCodec (path: string) { 27async function getVideoStreamCodec (path: string) {
88 const videoStream = await getVideoStreamFromFile(path) 28 const videoStream = await getVideoStreamFromFile(path)
89 29
@@ -143,69 +83,6 @@ async function getAudioStreamCodec (path: string, existingProbe?: FfprobeData) {
143 return 'mp4a.40.2' // Fallback 83 return 'mp4a.40.2' // Fallback
144} 84}
145 85
146async function getVideoFileResolution (path: string, existingProbe?: FfprobeData) {
147 const size = await getVideoStreamSize(path, existingProbe)
148
149 return {
150 width: size.width,
151 height: size.height,
152 ratio: Math.max(size.height, size.width) / Math.min(size.height, size.width),
153 resolution: Math.min(size.height, size.width),
154 isPortraitMode: size.height > size.width
155 }
156}
157
158async function getVideoFileFPS (path: string, existingProbe?: FfprobeData) {
159 const videoStream = await getVideoStreamFromFile(path, existingProbe)
160 if (videoStream === null) return 0
161
162 for (const key of [ 'avg_frame_rate', 'r_frame_rate' ]) {
163 const valuesText: string = videoStream[key]
164 if (!valuesText) continue
165
166 const [ frames, seconds ] = valuesText.split('/')
167 if (!frames || !seconds) continue
168
169 const result = parseInt(frames, 10) / parseInt(seconds, 10)
170 if (result > 0) return Math.round(result)
171 }
172
173 return 0
174}
175
176async function getMetadataFromFile (path: string, existingProbe?: FfprobeData) {
177 const metadata = existingProbe || await ffprobePromise(path)
178
179 return new VideoFileMetadata(metadata)
180}
181
182async function getVideoFileBitrate (path: string, existingProbe?: FfprobeData): Promise<number> {
183 const metadata = await getMetadataFromFile(path, existingProbe)
184
185 let bitrate = metadata.format.bit_rate as number
186 if (bitrate && !isNaN(bitrate)) return bitrate
187
188 const videoStream = await getVideoStreamFromFile(path, existingProbe)
189 if (!videoStream) return undefined
190
191 bitrate = videoStream?.bit_rate
192 if (bitrate && !isNaN(bitrate)) return bitrate
193
194 return undefined
195}
196
197async function getDurationFromVideoFile (path: string, existingProbe?: FfprobeData) {
198 const metadata = await getMetadataFromFile(path, existingProbe)
199
200 return Math.round(metadata.format.duration)
201}
202
203async function getVideoStreamFromFile (path: string, existingProbe?: FfprobeData) {
204 const metadata = await getMetadataFromFile(path, existingProbe)
205
206 return metadata.streams.find(s => s.codec_type === 'video') || null
207}
208
209function computeLowerResolutionsToTranscode (videoFileResolution: number, type: 'vod' | 'live') { 86function computeLowerResolutionsToTranscode (videoFileResolution: number, type: 'vod' | 'live') {
210 const configResolutions = type === 'vod' 87 const configResolutions = type === 'vod'
211 ? CONFIG.TRANSCODING.RESOLUTIONS 88 ? CONFIG.TRANSCODING.RESOLUTIONS
@@ -263,26 +140,6 @@ async function canDoQuickVideoTranscode (path: string, probe?: FfprobeData): Pro
263 return true 140 return true
264} 141}
265 142
266async function canDoQuickAudioTranscode (path: string, probe?: FfprobeData): Promise<boolean> {
267 const parsedAudio = await getAudioStream(path, probe)
268
269 if (!parsedAudio.audioStream) return true
270
271 if (parsedAudio.audioStream['codec_name'] !== 'aac') return false
272
273 const audioBitrate = parsedAudio.bitrate
274 if (!audioBitrate) return false
275
276 const maxAudioBitrate = getMaxAudioBitrate('aac', audioBitrate)
277 if (maxAudioBitrate !== -1 && audioBitrate > maxAudioBitrate) return false
278
279 const channelLayout = parsedAudio.audioStream['channel_layout']
280 // Causes playback issues with Chrome
281 if (!channelLayout || channelLayout === 'unknown') return false
282
283 return true
284}
285
286function getClosestFramerateStandard <K extends keyof Pick<VideoTranscodingFPS, 'HD_STANDARD' | 'STANDARD'>> (fps: number, type: K) { 143function getClosestFramerateStandard <K extends keyof Pick<VideoTranscodingFPS, 'HD_STANDARD' | 'STANDARD'>> (fps: number, type: K) {
287 return VIDEO_TRANSCODING_FPS[type].slice(0) 144 return VIDEO_TRANSCODING_FPS[type].slice(0)
288 .sort((a, b) => fps % a - fps % b)[0] 145 .sort((a, b) => fps % a - fps % b)[0]
diff --git a/server/helpers/image-utils.ts b/server/helpers/image-utils.ts
index 4305584d5..ced288045 100644
--- a/server/helpers/image-utils.ts
+++ b/server/helpers/image-utils.ts
@@ -1,9 +1,9 @@
1import { copy, readFile, remove, rename } from 'fs-extra' 1import { copy, readFile, remove, rename } from 'fs-extra'
2import Jimp, { read } from 'jimp' 2import Jimp, { read } from 'jimp'
3import { getLowercaseExtension } from './core-utils' 3import { getLowercaseExtension } from '@shared/core-utils'
4import { convertWebPToJPG, processGIF } from './ffmpeg-utils' 4import { convertWebPToJPG, processGIF } from './ffmpeg-utils'
5import { logger } from './logger' 5import { logger } from './logger'
6import { buildUUID } from './uuid' 6import { buildUUID } from '@shared/core-utils/uuid'
7 7
8function generateImageFilename (extension = '.jpg') { 8function generateImageFilename (extension = '.jpg') {
9 return buildUUID() + extension 9 return buildUUID() + extension
diff --git a/server/helpers/peertube-crypto.ts b/server/helpers/peertube-crypto.ts
index 44d90d9f1..31705e7fa 100644
--- a/server/helpers/peertube-crypto.ts
+++ b/server/helpers/peertube-crypto.ts
@@ -4,7 +4,8 @@ import { Request } from 'express'
4import { cloneDeep } from 'lodash' 4import { cloneDeep } from 'lodash'
5import { BCRYPT_SALT_SIZE, HTTP_SIGNATURE, PRIVATE_RSA_KEY_SIZE } from '../initializers/constants' 5import { BCRYPT_SALT_SIZE, HTTP_SIGNATURE, PRIVATE_RSA_KEY_SIZE } from '../initializers/constants'
6import { MActor } from '../types/models' 6import { MActor } from '../types/models'
7import { createPrivateKey, getPublicKey, promisify1, promisify2, sha256 } from './core-utils' 7import { sha256 } from '@shared/core-utils/crypto'
8import { createPrivateKey, getPublicKey, promisify1, promisify2 } from './core-utils'
8import { jsonld } from './custom-jsonld-signature' 9import { jsonld } from './custom-jsonld-signature'
9import { logger } from './logger' 10import { logger } from './logger'
10 11
diff --git a/server/helpers/utils.ts b/server/helpers/utils.ts
index 6c95a43b6..882f808ab 100644
--- a/server/helpers/utils.ts
+++ b/server/helpers/utils.ts
@@ -3,7 +3,8 @@ import { Instance as ParseTorrent } from 'parse-torrent'
3import { join } from 'path' 3import { join } from 'path'
4import { ResultList } from '../../shared' 4import { ResultList } from '../../shared'
5import { CONFIG } from '../initializers/config' 5import { CONFIG } from '../initializers/config'
6import { execPromise, execPromise2, randomBytesPromise, sha256 } from './core-utils' 6import { sha256 } from '@shared/core-utils/crypto'
7import { execPromise, execPromise2, randomBytesPromise } from './core-utils'
7import { logger } from './logger' 8import { logger } from './logger'
8 9
9function deleteFileAndCatch (path: string) { 10function deleteFileAndCatch (path: string) {
diff --git a/server/helpers/uuid.ts b/server/helpers/uuid.ts
deleted file mode 100644
index f3c80e046..000000000
--- a/server/helpers/uuid.ts
+++ /dev/null
@@ -1,32 +0,0 @@
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/server/helpers/webtorrent.ts b/server/helpers/webtorrent.ts
index ecc703646..67cb3971d 100644
--- a/server/helpers/webtorrent.ts
+++ b/server/helpers/webtorrent.ts
@@ -14,7 +14,8 @@ import { MVideo } from '@server/types/models/video/video'
14import { MVideoFile, MVideoFileRedundanciesOpt } from '@server/types/models/video/video-file' 14import { MVideoFile, MVideoFileRedundanciesOpt } from '@server/types/models/video/video-file'
15import { MStreamingPlaylistVideo } from '@server/types/models/video/video-streaming-playlist' 15import { MStreamingPlaylistVideo } from '@server/types/models/video/video-streaming-playlist'
16import { CONFIG } from '../initializers/config' 16import { CONFIG } from '../initializers/config'
17import { promisify2, sha1 } from './core-utils' 17import { promisify2 } from './core-utils'
18import { sha1 } from '@shared/core-utils/crypto'
18import { logger } from './logger' 19import { logger } from './logger'
19import { generateVideoImportTmpPath } from './utils' 20import { generateVideoImportTmpPath } from './utils'
20import { extractVideo } from './video' 21import { extractVideo } from './video'