]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/custom-validators/misc.ts
add script printing command to generate a resolution for a given file
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / misc.ts
index 61c03f0c978fa947e6494452333fa3496aaade56..effdd98cb9c4b3c8b222138f8e97bbf4c0b80e87 100644 (file)
@@ -86,6 +86,50 @@ function toIntArray (value: any) {
   return value.map(v => validator.toInt(v))
 }
 
+function isFileFieldValid (
+  files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[],
+  field: string,
+  optional = false
+) {
+  // Should have files
+  if (!files) return optional
+  if (isArray(files)) return optional
+
+  // Should have a file
+  const fileArray = files[field]
+  if (!fileArray || fileArray.length === 0) {
+    return optional
+  }
+
+  // The file should exist
+  const file = fileArray[0]
+  if (!file || !file.originalname) return false
+  return file
+}
+
+function isFileMimeTypeValid (
+  files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[],
+  mimeTypeRegex: string,
+  field: string,
+  optional = false
+) {
+  // Should have files
+  if (!files) return optional
+  if (isArray(files)) return optional
+
+  // Should have a file
+  const fileArray = files[field]
+  if (!fileArray || fileArray.length === 0) {
+    return optional
+  }
+
+  // The file should exist
+  const file = fileArray[0]
+  if (!file || !file.originalname) return false
+
+  return new RegExp(`^${mimeTypeRegex}$`, 'i').test(file.mimetype)
+}
+
 function isFileValid (
   files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[],
   mimeTypeRegex: string,
@@ -132,5 +176,7 @@ export {
   toIntOrNull,
   toArray,
   toIntArray,
+  isFileFieldValid,
+  isFileMimeTypeValid,
   isFileValid
 }