aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-06-28 17:30:59 +0200
committerChocobozzz <chocobozzz@cpy.re>2021-06-29 14:56:35 +0200
commitd4a8e7a65f97bb3257facc13e1ae8ffdbad61ddb (patch)
treea4cb07318100031951c3dffc61f4f2cb95d2cbd0 /server/helpers
parent62ddc31a9e4b92d7d27898ccfc363f68ab044139 (diff)
downloadPeerTube-d4a8e7a65f97bb3257facc13e1ae8ffdbad61ddb.tar.gz
PeerTube-d4a8e7a65f97bb3257facc13e1ae8ffdbad61ddb.tar.zst
PeerTube-d4a8e7a65f97bb3257facc13e1ae8ffdbad61ddb.zip
Support short uuid for GET video/playlist
Diffstat (limited to 'server/helpers')
-rw-r--r--server/helpers/custom-validators/misc.ts84
-rw-r--r--server/helpers/image-utils.ts4
-rw-r--r--server/helpers/uuid.ts32
3 files changed, 82 insertions, 38 deletions
diff --git a/server/helpers/custom-validators/misc.ts b/server/helpers/custom-validators/misc.ts
index 229e9f03c..528bfcfb8 100644
--- a/server/helpers/custom-validators/misc.ts
+++ b/server/helpers/custom-validators/misc.ts
@@ -2,6 +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'
5 6
6function exists (value: any) { 7function exists (value: any) {
7 return value !== undefined && value !== null 8 return value !== undefined && value !== null
@@ -50,42 +51,7 @@ function isIntOrNull (value: any) {
50 return value === null || validator.isInt('' + value) 51 return value === null || validator.isInt('' + value)
51} 52}
52 53
53function toIntOrNull (value: string) { 54// ---------------------------------------------------------------------------
54 const v = toValueOrNull(value)
55
56 if (v === null || v === undefined) return v
57 if (typeof v === 'number') return v
58
59 return validator.toInt('' + v)
60}
61
62function toBooleanOrNull (value: any) {
63 const v = toValueOrNull(value)
64
65 if (v === null || v === undefined) return v
66 if (typeof v === 'boolean') return v
67
68 return validator.toBoolean('' + v)
69}
70
71function toValueOrNull (value: string) {
72 if (value === 'null') return null
73
74 return value
75}
76
77function toArray (value: any) {
78 if (value && isArray(value) === false) return [ value ]
79
80 return value
81}
82
83function toIntArray (value: any) {
84 if (!value) return []
85 if (isArray(value) === false) return [ validator.toInt(value) ]
86
87 return value.map(v => validator.toInt(v))
88}
89 55
90function isFileFieldValid ( 56function isFileFieldValid (
91 files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], 57 files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[],
@@ -160,6 +126,51 @@ function isFileValid (
160 126
161// --------------------------------------------------------------------------- 127// ---------------------------------------------------------------------------
162 128
129function toCompleteUUID (value: string) {
130 if (isShortUUID(value)) return shortToUUID(value)
131
132 return value
133}
134
135function toIntOrNull (value: string) {
136 const v = toValueOrNull(value)
137
138 if (v === null || v === undefined) return v
139 if (typeof v === 'number') return v
140
141 return validator.toInt('' + v)
142}
143
144function toBooleanOrNull (value: any) {
145 const v = toValueOrNull(value)
146
147 if (v === null || v === undefined) return v
148 if (typeof v === 'boolean') return v
149
150 return validator.toBoolean('' + v)
151}
152
153function toValueOrNull (value: string) {
154 if (value === 'null') return null
155
156 return value
157}
158
159function toArray (value: any) {
160 if (value && isArray(value) === false) return [ value ]
161
162 return value
163}
164
165function toIntArray (value: any) {
166 if (!value) return []
167 if (isArray(value) === false) return [ validator.toInt(value) ]
168
169 return value.map(v => validator.toInt(v))
170}
171
172// ---------------------------------------------------------------------------
173
163export { 174export {
164 exists, 175 exists,
165 isArrayOf, 176 isArrayOf,
@@ -169,6 +180,7 @@ export {
169 isIdValid, 180 isIdValid,
170 isSafePath, 181 isSafePath,
171 isUUIDValid, 182 isUUIDValid,
183 toCompleteUUID,
172 isIdOrUUIDValid, 184 isIdOrUUIDValid,
173 isDateValid, 185 isDateValid,
174 toValueOrNull, 186 toValueOrNull,
diff --git a/server/helpers/image-utils.ts b/server/helpers/image-utils.ts
index 122fb009d..c76ed545b 100644
--- a/server/helpers/image-utils.ts
+++ b/server/helpers/image-utils.ts
@@ -1,12 +1,12 @@
1import { copy, readFile, remove, rename } from 'fs-extra' 1import { copy, readFile, remove, rename } from 'fs-extra'
2import * as Jimp from 'jimp' 2import * as Jimp from 'jimp'
3import { v4 as uuidv4 } from 'uuid'
4import { getLowercaseExtension } from './core-utils' 3import { getLowercaseExtension } from './core-utils'
5import { convertWebPToJPG, processGIF } from './ffmpeg-utils' 4import { convertWebPToJPG, processGIF } from './ffmpeg-utils'
6import { logger } from './logger' 5import { logger } from './logger'
6import { buildUUID } from './uuid'
7 7
8function generateImageFilename (extension = '.jpg') { 8function generateImageFilename (extension = '.jpg') {
9 return uuidv4() + extension 9 return buildUUID() + extension
10} 10}
11 11
12async function processImage ( 12async function processImage (
diff --git a/server/helpers/uuid.ts b/server/helpers/uuid.ts
new file mode 100644
index 000000000..3eb06c773
--- /dev/null
+++ b/server/helpers/uuid.ts
@@ -0,0 +1,32 @@
1import * as short from 'short-uuid'
2
3const translator = short()
4
5function buildUUID () {
6 return short.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}