diff options
Diffstat (limited to 'server/helpers')
-rw-r--r-- | server/helpers/custom-validators/follows.ts | 20 | ||||
-rw-r--r-- | server/helpers/custom-validators/misc.ts | 15 | ||||
-rw-r--r-- | server/helpers/custom-validators/servers.ts | 1 | ||||
-rw-r--r-- | server/helpers/custom-validators/video-channels.ts | 11 | ||||
-rw-r--r-- | server/helpers/custom-validators/video-ownership.ts | 2 | ||||
-rw-r--r-- | server/helpers/database-utils.ts | 34 | ||||
-rw-r--r-- | server/helpers/express-utils.ts | 2 | ||||
-rw-r--r-- | server/helpers/ffmpeg-utils.ts | 21 | ||||
-rw-r--r-- | server/helpers/logger.ts | 23 | ||||
-rw-r--r-- | server/helpers/query.ts | 74 | ||||
-rw-r--r-- | server/helpers/webtorrent.ts | 5 | ||||
-rw-r--r-- | server/helpers/youtube-dl.ts | 2 |
12 files changed, 172 insertions, 38 deletions
diff --git a/server/helpers/custom-validators/follows.ts b/server/helpers/custom-validators/follows.ts index fbef7ad87..8f65552c3 100644 --- a/server/helpers/custom-validators/follows.ts +++ b/server/helpers/custom-validators/follows.ts | |||
@@ -1,4 +1,4 @@ | |||
1 | import { exists } from './misc' | 1 | import { exists, isArray } from './misc' |
2 | import { FollowState } from '@shared/models' | 2 | import { FollowState } from '@shared/models' |
3 | 3 | ||
4 | function isFollowStateValid (value: FollowState) { | 4 | function isFollowStateValid (value: FollowState) { |
@@ -7,8 +7,24 @@ function isFollowStateValid (value: FollowState) { | |||
7 | return value === 'pending' || value === 'accepted' | 7 | return value === 'pending' || value === 'accepted' |
8 | } | 8 | } |
9 | 9 | ||
10 | function isRemoteHandleValid (value: string) { | ||
11 | if (!exists(value)) return false | ||
12 | if (typeof value !== 'string') return false | ||
13 | |||
14 | return value.includes('@') | ||
15 | } | ||
16 | |||
17 | function isEachUniqueHandleValid (handles: string[]) { | ||
18 | return isArray(handles) && | ||
19 | handles.every(handle => { | ||
20 | return isRemoteHandleValid(handle) && handles.indexOf(handle) === handles.lastIndexOf(handle) | ||
21 | }) | ||
22 | } | ||
23 | |||
10 | // --------------------------------------------------------------------------- | 24 | // --------------------------------------------------------------------------- |
11 | 25 | ||
12 | export { | 26 | export { |
13 | isFollowStateValid | 27 | isFollowStateValid, |
28 | isRemoteHandleValid, | ||
29 | isEachUniqueHandleValid | ||
14 | } | 30 | } |
diff --git a/server/helpers/custom-validators/misc.ts b/server/helpers/custom-validators/misc.ts index 528bfcfb8..c19a3e5eb 100644 --- a/server/helpers/custom-validators/misc.ts +++ b/server/helpers/custom-validators/misc.ts | |||
@@ -23,6 +23,10 @@ function isNotEmptyIntArray (value: any) { | |||
23 | return Array.isArray(value) && value.every(v => validator.isInt('' + v)) && value.length !== 0 | 23 | return Array.isArray(value) && value.every(v => validator.isInt('' + v)) && value.length !== 0 |
24 | } | 24 | } |
25 | 25 | ||
26 | function isNotEmptyStringArray (value: any) { | ||
27 | return Array.isArray(value) && value.every(v => typeof v === 'string' && v.length !== 0) && value.length !== 0 | ||
28 | } | ||
29 | |||
26 | function isArrayOf (value: any, validator: (value: any) => boolean) { | 30 | function isArrayOf (value: any, validator: (value: any) => boolean) { |
27 | return isArray(value) && value.every(v => validator(v)) | 31 | return isArray(value) && value.every(v => validator(v)) |
28 | } | 32 | } |
@@ -39,6 +43,10 @@ function isUUIDValid (value: string) { | |||
39 | return exists(value) && validator.isUUID('' + value, 4) | 43 | return exists(value) && validator.isUUID('' + value, 4) |
40 | } | 44 | } |
41 | 45 | ||
46 | function areUUIDsValid (values: string[]) { | ||
47 | return isArray(values) && values.every(v => isUUIDValid(v)) | ||
48 | } | ||
49 | |||
42 | function isIdOrUUIDValid (value: string) { | 50 | function isIdOrUUIDValid (value: string) { |
43 | return isIdValid(value) || isUUIDValid(value) | 51 | return isIdValid(value) || isUUIDValid(value) |
44 | } | 52 | } |
@@ -132,6 +140,10 @@ function toCompleteUUID (value: string) { | |||
132 | return value | 140 | return value |
133 | } | 141 | } |
134 | 142 | ||
143 | function toCompleteUUIDs (values: string[]) { | ||
144 | return values.map(v => toCompleteUUID(v)) | ||
145 | } | ||
146 | |||
135 | function toIntOrNull (value: string) { | 147 | function toIntOrNull (value: string) { |
136 | const v = toValueOrNull(value) | 148 | const v = toValueOrNull(value) |
137 | 149 | ||
@@ -179,7 +191,9 @@ export { | |||
179 | isIntOrNull, | 191 | isIntOrNull, |
180 | isIdValid, | 192 | isIdValid, |
181 | isSafePath, | 193 | isSafePath, |
194 | isNotEmptyStringArray, | ||
182 | isUUIDValid, | 195 | isUUIDValid, |
196 | toCompleteUUIDs, | ||
183 | toCompleteUUID, | 197 | toCompleteUUID, |
184 | isIdOrUUIDValid, | 198 | isIdOrUUIDValid, |
185 | isDateValid, | 199 | isDateValid, |
@@ -187,6 +201,7 @@ export { | |||
187 | toBooleanOrNull, | 201 | toBooleanOrNull, |
188 | isBooleanValid, | 202 | isBooleanValid, |
189 | toIntOrNull, | 203 | toIntOrNull, |
204 | areUUIDsValid, | ||
190 | toArray, | 205 | toArray, |
191 | toIntArray, | 206 | toIntArray, |
192 | isFileFieldValid, | 207 | isFileFieldValid, |
diff --git a/server/helpers/custom-validators/servers.ts b/server/helpers/custom-validators/servers.ts index adf1ea497..c0f8b6aeb 100644 --- a/server/helpers/custom-validators/servers.ts +++ b/server/helpers/custom-validators/servers.ts | |||
@@ -19,7 +19,6 @@ function isHostValid (host: string) { | |||
19 | 19 | ||
20 | function isEachUniqueHostValid (hosts: string[]) { | 20 | function isEachUniqueHostValid (hosts: string[]) { |
21 | return isArray(hosts) && | 21 | return isArray(hosts) && |
22 | hosts.length !== 0 && | ||
23 | hosts.every(host => { | 22 | hosts.every(host => { |
24 | return isHostValid(host) && hosts.indexOf(host) === hosts.lastIndexOf(host) | 23 | return isHostValid(host) && hosts.indexOf(host) === hosts.lastIndexOf(host) |
25 | }) | 24 | }) |
diff --git a/server/helpers/custom-validators/video-channels.ts b/server/helpers/custom-validators/video-channels.ts index ded5d5171..249083f39 100644 --- a/server/helpers/custom-validators/video-channels.ts +++ b/server/helpers/custom-validators/video-channels.ts | |||
@@ -1,14 +1,20 @@ | |||
1 | import validator from 'validator' | 1 | import validator from 'validator' |
2 | import { CONSTRAINTS_FIELDS } from '../../initializers/constants' | 2 | import { CONSTRAINTS_FIELDS } from '../../initializers/constants' |
3 | import { exists } from './misc' | 3 | import { exists } from './misc' |
4 | import { isUserUsernameValid } from './users' | ||
4 | 5 | ||
5 | const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS | 6 | const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS |
6 | 7 | ||
8 | function isVideoChannelUsernameValid (value: string) { | ||
9 | // Use the same constraints than user username | ||
10 | return isUserUsernameValid(value) | ||
11 | } | ||
12 | |||
7 | function isVideoChannelDescriptionValid (value: string) { | 13 | function isVideoChannelDescriptionValid (value: string) { |
8 | return value === null || validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.DESCRIPTION) | 14 | return value === null || validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.DESCRIPTION) |
9 | } | 15 | } |
10 | 16 | ||
11 | function isVideoChannelNameValid (value: string) { | 17 | function isVideoChannelDisplayNameValid (value: string) { |
12 | return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME) | 18 | return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME) |
13 | } | 19 | } |
14 | 20 | ||
@@ -19,7 +25,8 @@ function isVideoChannelSupportValid (value: string) { | |||
19 | // --------------------------------------------------------------------------- | 25 | // --------------------------------------------------------------------------- |
20 | 26 | ||
21 | export { | 27 | export { |
28 | isVideoChannelUsernameValid, | ||
22 | isVideoChannelDescriptionValid, | 29 | isVideoChannelDescriptionValid, |
23 | isVideoChannelNameValid, | 30 | isVideoChannelDisplayNameValid, |
24 | isVideoChannelSupportValid | 31 | isVideoChannelSupportValid |
25 | } | 32 | } |
diff --git a/server/helpers/custom-validators/video-ownership.ts b/server/helpers/custom-validators/video-ownership.ts index 0e1c63bad..cf15b385a 100644 --- a/server/helpers/custom-validators/video-ownership.ts +++ b/server/helpers/custom-validators/video-ownership.ts | |||
@@ -1,7 +1,7 @@ | |||
1 | import { Response } from 'express' | 1 | import { Response } from 'express' |
2 | import { MUserId } from '@server/types/models' | 2 | import { MUserId } from '@server/types/models' |
3 | import { MVideoChangeOwnershipFull } from '@server/types/models/video/video-change-ownership' | 3 | import { MVideoChangeOwnershipFull } from '@server/types/models/video/video-change-ownership' |
4 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | 4 | import { HttpStatusCode } from '../../../shared/models/http/http-error-codes' |
5 | 5 | ||
6 | function checkUserCanTerminateOwnershipChange (user: MUserId, videoChangeOwnership: MVideoChangeOwnershipFull, res: Response) { | 6 | function checkUserCanTerminateOwnershipChange (user: MUserId, videoChangeOwnership: MVideoChangeOwnershipFull, res: Response) { |
7 | if (videoChangeOwnership.NextOwner.userId === user.id) { | 7 | if (videoChangeOwnership.NextOwner.userId === user.id) { |
diff --git a/server/helpers/database-utils.ts b/server/helpers/database-utils.ts index b5dc70c17..ec35295df 100644 --- a/server/helpers/database-utils.ts +++ b/server/helpers/database-utils.ts | |||
@@ -1,12 +1,12 @@ | |||
1 | import * as retry from 'async/retry' | 1 | import * as retry from 'async/retry' |
2 | import * as Bluebird from 'bluebird' | 2 | import * as Bluebird from 'bluebird' |
3 | import { QueryTypes, Transaction } from 'sequelize' | 3 | import { Transaction } from 'sequelize' |
4 | import { Model } from 'sequelize-typescript' | 4 | import { Model } from 'sequelize-typescript' |
5 | import { sequelizeTypescript } from '@server/initializers/database' | 5 | import { sequelizeTypescript } from '@server/initializers/database' |
6 | import { logger } from './logger' | 6 | import { logger } from './logger' |
7 | 7 | ||
8 | function retryTransactionWrapper <T, A, B, C, D> ( | 8 | function retryTransactionWrapper <T, A, B, C, D> ( |
9 | functionToRetry: (arg1: A, arg2: B, arg3: C, arg4: D) => Promise<T> | Bluebird<T>, | 9 | functionToRetry: (arg1: A, arg2: B, arg3: C, arg4: D) => Promise<T>, |
10 | arg1: A, | 10 | arg1: A, |
11 | arg2: B, | 11 | arg2: B, |
12 | arg3: C, | 12 | arg3: C, |
@@ -14,20 +14,20 @@ function retryTransactionWrapper <T, A, B, C, D> ( | |||
14 | ): Promise<T> | 14 | ): Promise<T> |
15 | 15 | ||
16 | function retryTransactionWrapper <T, A, B, C> ( | 16 | function retryTransactionWrapper <T, A, B, C> ( |
17 | functionToRetry: (arg1: A, arg2: B, arg3: C) => Promise<T> | Bluebird<T>, | 17 | functionToRetry: (arg1: A, arg2: B, arg3: C) => Promise<T>, |
18 | arg1: A, | 18 | arg1: A, |
19 | arg2: B, | 19 | arg2: B, |
20 | arg3: C | 20 | arg3: C |
21 | ): Promise<T> | 21 | ): Promise<T> |
22 | 22 | ||
23 | function retryTransactionWrapper <T, A, B> ( | 23 | function retryTransactionWrapper <T, A, B> ( |
24 | functionToRetry: (arg1: A, arg2: B) => Promise<T> | Bluebird<T>, | 24 | functionToRetry: (arg1: A, arg2: B) => Promise<T>, |
25 | arg1: A, | 25 | arg1: A, |
26 | arg2: B | 26 | arg2: B |
27 | ): Promise<T> | 27 | ): Promise<T> |
28 | 28 | ||
29 | function retryTransactionWrapper <T, A> ( | 29 | function retryTransactionWrapper <T, A> ( |
30 | functionToRetry: (arg1: A) => Promise<T> | Bluebird<T>, | 30 | functionToRetry: (arg1: A) => Promise<T>, |
31 | arg1: A | 31 | arg1: A |
32 | ): Promise<T> | 32 | ): Promise<T> |
33 | 33 | ||
@@ -36,7 +36,7 @@ function retryTransactionWrapper <T> ( | |||
36 | ): Promise<T> | 36 | ): Promise<T> |
37 | 37 | ||
38 | function retryTransactionWrapper <T> ( | 38 | function retryTransactionWrapper <T> ( |
39 | functionToRetry: (...args: any[]) => Promise<T> | Bluebird<T>, | 39 | functionToRetry: (...args: any[]) => Promise<T>, |
40 | ...args: any[] | 40 | ...args: any[] |
41 | ): Promise<T> { | 41 | ): Promise<T> { |
42 | return transactionRetryer<T>(callback => { | 42 | return transactionRetryer<T>(callback => { |
@@ -84,25 +84,15 @@ function resetSequelizeInstance (instance: Model<any>, savedFields: object) { | |||
84 | }) | 84 | }) |
85 | } | 85 | } |
86 | 86 | ||
87 | function deleteNonExistingModels <T extends { hasSameUniqueKeysThan (other: T): boolean } & Pick<Model, 'destroy'>> ( | 87 | function filterNonExistingModels <T extends { hasSameUniqueKeysThan (other: T): boolean }> ( |
88 | fromDatabase: T[], | 88 | fromDatabase: T[], |
89 | newModels: T[], | 89 | newModels: T[] |
90 | t: Transaction | ||
91 | ) { | 90 | ) { |
92 | return fromDatabase.filter(f => !newModels.find(newModel => newModel.hasSameUniqueKeysThan(f))) | 91 | return fromDatabase.filter(f => !newModels.find(newModel => newModel.hasSameUniqueKeysThan(f))) |
93 | .map(f => f.destroy({ transaction: t })) | ||
94 | } | 92 | } |
95 | 93 | ||
96 | // Sequelize always skip the update if we only update updatedAt field | 94 | function deleteAllModels <T extends Pick<Model, 'destroy'>> (models: T[], transaction: Transaction) { |
97 | function setAsUpdated (table: string, id: number, transaction?: Transaction) { | 95 | return Promise.all(models.map(f => f.destroy({ transaction }))) |
98 | return sequelizeTypescript.query( | ||
99 | `UPDATE "${table}" SET "updatedAt" = :updatedAt WHERE id = :id`, | ||
100 | { | ||
101 | replacements: { table, id, updatedAt: new Date() }, | ||
102 | type: QueryTypes.UPDATE, | ||
103 | transaction | ||
104 | } | ||
105 | ) | ||
106 | } | 96 | } |
107 | 97 | ||
108 | // --------------------------------------------------------------------------- | 98 | // --------------------------------------------------------------------------- |
@@ -127,7 +117,7 @@ export { | |||
127 | transactionRetryer, | 117 | transactionRetryer, |
128 | updateInstanceWithAnother, | 118 | updateInstanceWithAnother, |
129 | afterCommitIfTransaction, | 119 | afterCommitIfTransaction, |
130 | deleteNonExistingModels, | 120 | filterNonExistingModels, |
131 | setAsUpdated, | 121 | deleteAllModels, |
132 | runInReadCommittedTransaction | 122 | runInReadCommittedTransaction |
133 | } | 123 | } |
diff --git a/server/helpers/express-utils.ts b/server/helpers/express-utils.ts index 0ff113274..c299b70f1 100644 --- a/server/helpers/express-utils.ts +++ b/server/helpers/express-utils.ts | |||
@@ -1,6 +1,6 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import * as multer from 'multer' | 2 | import * as multer from 'multer' |
3 | import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes' | 3 | import { HttpStatusCode } from '../../shared/models/http/http-error-codes' |
4 | import { CONFIG } from '../initializers/config' | 4 | import { CONFIG } from '../initializers/config' |
5 | import { REMOTE_SCHEME } from '../initializers/constants' | 5 | import { REMOTE_SCHEME } from '../initializers/constants' |
6 | import { getLowercaseExtension } from './core-utils' | 6 | import { getLowercaseExtension } from './core-utils' |
diff --git a/server/helpers/ffmpeg-utils.ts b/server/helpers/ffmpeg-utils.ts index 6f5a71b4a..61c8a6db2 100644 --- a/server/helpers/ffmpeg-utils.ts +++ b/server/helpers/ffmpeg-utils.ts | |||
@@ -212,14 +212,17 @@ async function transcode (options: TranscodeOptions) { | |||
212 | 212 | ||
213 | async function getLiveTranscodingCommand (options: { | 213 | async function getLiveTranscodingCommand (options: { |
214 | rtmpUrl: string | 214 | rtmpUrl: string |
215 | |||
215 | outPath: string | 216 | outPath: string |
217 | masterPlaylistName: string | ||
218 | |||
216 | resolutions: number[] | 219 | resolutions: number[] |
217 | fps: number | 220 | fps: number |
218 | 221 | ||
219 | availableEncoders: AvailableEncoders | 222 | availableEncoders: AvailableEncoders |
220 | profile: string | 223 | profile: string |
221 | }) { | 224 | }) { |
222 | const { rtmpUrl, outPath, resolutions, fps, availableEncoders, profile } = options | 225 | const { rtmpUrl, outPath, resolutions, fps, availableEncoders, profile, masterPlaylistName } = options |
223 | const input = rtmpUrl | 226 | const input = rtmpUrl |
224 | 227 | ||
225 | const command = getFFmpeg(input, 'live') | 228 | const command = getFFmpeg(input, 'live') |
@@ -301,14 +304,14 @@ async function getLiveTranscodingCommand (options: { | |||
301 | 304 | ||
302 | command.complexFilter(complexFilter) | 305 | command.complexFilter(complexFilter) |
303 | 306 | ||
304 | addDefaultLiveHLSParams(command, outPath) | 307 | addDefaultLiveHLSParams(command, outPath, masterPlaylistName) |
305 | 308 | ||
306 | command.outputOption('-var_stream_map', varStreamMap.join(' ')) | 309 | command.outputOption('-var_stream_map', varStreamMap.join(' ')) |
307 | 310 | ||
308 | return command | 311 | return command |
309 | } | 312 | } |
310 | 313 | ||
311 | function getLiveMuxingCommand (rtmpUrl: string, outPath: string) { | 314 | function getLiveMuxingCommand (rtmpUrl: string, outPath: string, masterPlaylistName: string) { |
312 | const command = getFFmpeg(rtmpUrl, 'live') | 315 | const command = getFFmpeg(rtmpUrl, 'live') |
313 | 316 | ||
314 | command.outputOption('-c:v copy') | 317 | command.outputOption('-c:v copy') |
@@ -316,7 +319,7 @@ function getLiveMuxingCommand (rtmpUrl: string, outPath: string) { | |||
316 | command.outputOption('-map 0:a?') | 319 | command.outputOption('-map 0:a?') |
317 | command.outputOption('-map 0:v?') | 320 | command.outputOption('-map 0:v?') |
318 | 321 | ||
319 | addDefaultLiveHLSParams(command, outPath) | 322 | addDefaultLiveHLSParams(command, outPath, masterPlaylistName) |
320 | 323 | ||
321 | return command | 324 | return command |
322 | } | 325 | } |
@@ -371,12 +374,12 @@ function addDefaultEncoderParams (options: { | |||
371 | } | 374 | } |
372 | } | 375 | } |
373 | 376 | ||
374 | function addDefaultLiveHLSParams (command: ffmpeg.FfmpegCommand, outPath: string) { | 377 | function addDefaultLiveHLSParams (command: ffmpeg.FfmpegCommand, outPath: string, masterPlaylistName: string) { |
375 | command.outputOption('-hls_time ' + VIDEO_LIVE.SEGMENT_TIME_SECONDS) | 378 | command.outputOption('-hls_time ' + VIDEO_LIVE.SEGMENT_TIME_SECONDS) |
376 | command.outputOption('-hls_list_size ' + VIDEO_LIVE.SEGMENTS_LIST_SIZE) | 379 | command.outputOption('-hls_list_size ' + VIDEO_LIVE.SEGMENTS_LIST_SIZE) |
377 | command.outputOption('-hls_flags delete_segments+independent_segments') | 380 | command.outputOption('-hls_flags delete_segments+independent_segments') |
378 | command.outputOption(`-hls_segment_filename ${join(outPath, '%v-%06d.ts')}`) | 381 | command.outputOption(`-hls_segment_filename ${join(outPath, '%v-%06d.ts')}`) |
379 | command.outputOption('-master_pl_name master.m3u8') | 382 | command.outputOption('-master_pl_name ' + masterPlaylistName) |
380 | command.outputOption(`-f hls`) | 383 | command.outputOption(`-f hls`) |
381 | 384 | ||
382 | command.output(join(outPath, '%v.m3u8')) | 385 | command.output(join(outPath, '%v.m3u8')) |
@@ -700,6 +703,10 @@ async function runCommand (options: { | |||
700 | const { command, silent = false, job } = options | 703 | const { command, silent = false, job } = options |
701 | 704 | ||
702 | return new Promise<void>((res, rej) => { | 705 | return new Promise<void>((res, rej) => { |
706 | let shellCommand: string | ||
707 | |||
708 | command.on('start', cmdline => { shellCommand = cmdline }) | ||
709 | |||
703 | command.on('error', (err, stdout, stderr) => { | 710 | command.on('error', (err, stdout, stderr) => { |
704 | if (silent !== true) logger.error('Error in ffmpeg.', { stdout, stderr }) | 711 | if (silent !== true) logger.error('Error in ffmpeg.', { stdout, stderr }) |
705 | 712 | ||
@@ -707,7 +714,7 @@ async function runCommand (options: { | |||
707 | }) | 714 | }) |
708 | 715 | ||
709 | command.on('end', (stdout, stderr) => { | 716 | command.on('end', (stdout, stderr) => { |
710 | logger.debug('FFmpeg command ended.', { stdout, stderr }) | 717 | logger.debug('FFmpeg command ended.', { stdout, stderr, shellCommand }) |
711 | 718 | ||
712 | res() | 719 | res() |
713 | }) | 720 | }) |
diff --git a/server/helpers/logger.ts b/server/helpers/logger.ts index 29e06860d..20c3c3edb 100644 --- a/server/helpers/logger.ts +++ b/server/helpers/logger.ts | |||
@@ -1,5 +1,5 @@ | |||
1 | // Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/ | 1 | // Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/ |
2 | import { mkdirpSync } from 'fs-extra' | 2 | import { mkdirpSync, stat } from 'fs-extra' |
3 | import { omit } from 'lodash' | 3 | import { omit } from 'lodash' |
4 | import * as path from 'path' | 4 | import * as path from 'path' |
5 | import { format as sqlFormat } from 'sql-formatter' | 5 | import { format as sqlFormat } from 'sql-formatter' |
@@ -158,6 +158,26 @@ function loggerTagsFactory (...defaultTags: string[]): LoggerTagsFn { | |||
158 | } | 158 | } |
159 | } | 159 | } |
160 | 160 | ||
161 | async function mtimeSortFilesDesc (files: string[], basePath: string) { | ||
162 | const promises = [] | ||
163 | const out: { file: string, mtime: number }[] = [] | ||
164 | |||
165 | for (const file of files) { | ||
166 | const p = stat(basePath + '/' + file) | ||
167 | .then(stats => { | ||
168 | if (stats.isFile()) out.push({ file, mtime: stats.mtime.getTime() }) | ||
169 | }) | ||
170 | |||
171 | promises.push(p) | ||
172 | } | ||
173 | |||
174 | await Promise.all(promises) | ||
175 | |||
176 | out.sort((a, b) => b.mtime - a.mtime) | ||
177 | |||
178 | return out | ||
179 | } | ||
180 | |||
161 | // --------------------------------------------------------------------------- | 181 | // --------------------------------------------------------------------------- |
162 | 182 | ||
163 | export { | 183 | export { |
@@ -168,6 +188,7 @@ export { | |||
168 | labelFormatter, | 188 | labelFormatter, |
169 | consoleLoggerFormat, | 189 | consoleLoggerFormat, |
170 | jsonLoggerFormat, | 190 | jsonLoggerFormat, |
191 | mtimeSortFilesDesc, | ||
171 | logger, | 192 | logger, |
172 | loggerTagsFactory, | 193 | loggerTagsFactory, |
173 | bunyanLogger | 194 | bunyanLogger |
diff --git a/server/helpers/query.ts b/server/helpers/query.ts new file mode 100644 index 000000000..e711b15f2 --- /dev/null +++ b/server/helpers/query.ts | |||
@@ -0,0 +1,74 @@ | |||
1 | import { pick } from '@shared/core-utils' | ||
2 | import { | ||
3 | VideoChannelsSearchQueryAfterSanitize, | ||
4 | VideoPlaylistsSearchQueryAfterSanitize, | ||
5 | VideosCommonQueryAfterSanitize, | ||
6 | VideosSearchQueryAfterSanitize | ||
7 | } from '@shared/models' | ||
8 | |||
9 | function pickCommonVideoQuery (query: VideosCommonQueryAfterSanitize) { | ||
10 | return pick(query, [ | ||
11 | 'start', | ||
12 | 'count', | ||
13 | 'sort', | ||
14 | 'nsfw', | ||
15 | 'isLive', | ||
16 | 'categoryOneOf', | ||
17 | 'licenceOneOf', | ||
18 | 'languageOneOf', | ||
19 | 'tagsOneOf', | ||
20 | 'tagsAllOf', | ||
21 | 'filter', | ||
22 | 'skipCount' | ||
23 | ]) | ||
24 | } | ||
25 | |||
26 | function pickSearchVideoQuery (query: VideosSearchQueryAfterSanitize) { | ||
27 | return { | ||
28 | ...pickCommonVideoQuery(query), | ||
29 | |||
30 | ...pick(query, [ | ||
31 | 'searchTarget', | ||
32 | 'search', | ||
33 | 'host', | ||
34 | 'startDate', | ||
35 | 'endDate', | ||
36 | 'originallyPublishedStartDate', | ||
37 | 'originallyPublishedEndDate', | ||
38 | 'durationMin', | ||
39 | 'durationMax', | ||
40 | 'uuids' | ||
41 | ]) | ||
42 | } | ||
43 | } | ||
44 | |||
45 | function pickSearchChannelQuery (query: VideoChannelsSearchQueryAfterSanitize) { | ||
46 | return pick(query, [ | ||
47 | 'searchTarget', | ||
48 | 'search', | ||
49 | 'start', | ||
50 | 'count', | ||
51 | 'sort', | ||
52 | 'host', | ||
53 | 'handles' | ||
54 | ]) | ||
55 | } | ||
56 | |||
57 | function pickSearchPlaylistQuery (query: VideoPlaylistsSearchQueryAfterSanitize) { | ||
58 | return pick(query, [ | ||
59 | 'searchTarget', | ||
60 | 'search', | ||
61 | 'start', | ||
62 | 'count', | ||
63 | 'sort', | ||
64 | 'host', | ||
65 | 'uuids' | ||
66 | ]) | ||
67 | } | ||
68 | |||
69 | export { | ||
70 | pickCommonVideoQuery, | ||
71 | pickSearchVideoQuery, | ||
72 | pickSearchPlaylistQuery, | ||
73 | pickSearchChannelQuery | ||
74 | } | ||
diff --git a/server/helpers/webtorrent.ts b/server/helpers/webtorrent.ts index d8220ba9c..ecf63e93e 100644 --- a/server/helpers/webtorrent.ts +++ b/server/helpers/webtorrent.ts | |||
@@ -103,6 +103,11 @@ async function createTorrentAndSetInfoHash ( | |||
103 | 103 | ||
104 | await writeFile(torrentPath, torrent) | 104 | await writeFile(torrentPath, torrent) |
105 | 105 | ||
106 | // Remove old torrent file if it existed | ||
107 | if (videoFile.hasTorrent()) { | ||
108 | await remove(join(CONFIG.STORAGE.TORRENTS_DIR, videoFile.torrentFilename)) | ||
109 | } | ||
110 | |||
106 | const parsedTorrent = parseTorrent(torrent) | 111 | const parsedTorrent = parseTorrent(torrent) |
107 | videoFile.infoHash = parsedTorrent.infoHash | 112 | videoFile.infoHash = parsedTorrent.infoHash |
108 | videoFile.torrentFilename = torrentFilename | 113 | videoFile.torrentFilename = torrentFilename |
diff --git a/server/helpers/youtube-dl.ts b/server/helpers/youtube-dl.ts index fdd361390..3c80e7d41 100644 --- a/server/helpers/youtube-dl.ts +++ b/server/helpers/youtube-dl.ts | |||
@@ -3,7 +3,7 @@ import { ensureDir, move, pathExists, remove, writeFile } from 'fs-extra' | |||
3 | import got from 'got' | 3 | import got from 'got' |
4 | import { join } from 'path' | 4 | import { join } from 'path' |
5 | import { CONFIG } from '@server/initializers/config' | 5 | import { CONFIG } from '@server/initializers/config' |
6 | import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes' | 6 | import { HttpStatusCode } from '../../shared/models/http/http-error-codes' |
7 | import { VideoResolution } from '../../shared/models/videos' | 7 | import { VideoResolution } from '../../shared/models/videos' |
8 | import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES } from '../initializers/constants' | 8 | import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES } from '../initializers/constants' |
9 | import { peertubeTruncate, pipelinePromise, root } from './core-utils' | 9 | import { peertubeTruncate, pipelinePromise, root } from './core-utils' |