]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/utils.ts
Add tests regarding video import
[github/Chocobozzz/PeerTube.git] / server / helpers / utils.ts
index 9efc89d92ec1f7df574eeb4e61e80d5703f71146..7abcec5d727fe3f08ea7607d755cfe92a01fcc66 100644 (file)
@@ -6,11 +6,35 @@ import { CONFIG } from '../initializers'
 import { UserModel } from '../models/account/user'
 import { ActorModel } from '../models/activitypub/actor'
 import { ApplicationModel } from '../models/application/application'
-import { pseudoRandomBytesPromise } from './core-utils'
+import { pseudoRandomBytesPromise, unlinkPromise } from './core-utils'
 import { logger } from './logger'
+import { isArray } from './custom-validators/misc'
 
 const isCidr = require('is-cidr')
 
+function cleanUpReqFiles (req: { files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[] }) {
+  const files = req.files
+
+  if (!files) return
+
+  if (isArray(files)) {
+    (files as Express.Multer.File[]).forEach(f => deleteFileAsync(f.path))
+    return
+  }
+
+  for (const key of Object.keys(files)) {
+    const file = files[key]
+
+    if (isArray(file)) file.forEach(f => deleteFileAsync(f.path))
+    else deleteFileAsync(file.path)
+  }
+}
+
+function deleteFileAsync (path: string) {
+  unlinkPromise(path)
+    .catch(err => logger.error('Cannot delete the file %s asynchronously.', path, { err }))
+}
+
 async function generateRandomString (size: number) {
   const raw = await pseudoRandomBytesPromise(size)
 
@@ -52,7 +76,7 @@ async function isSignupAllowed () {
 function isSignupAllowedForCurrentIP (ip: string) {
   const addr = ipaddr.parse(ip)
   let excludeList = [ 'blacklist' ]
-  let matched: string
+  let matched = ''
 
   // if there is a valid, non-empty whitelist, we exclude all unknown adresses too
   if (CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isCidr(cidr)).length > 0) {
@@ -144,6 +168,8 @@ let serverActor: ActorModel
 async function getServerActor () {
   if (serverActor === undefined) {
     const application = await ApplicationModel.load()
+    if (!application) throw Error('Could not load Application from database.')
+
     serverActor = application.Account.Actor
   }
 
@@ -160,6 +186,8 @@ type SortType = { sortModel: any, sortValue: string }
 // ---------------------------------------------------------------------------
 
 export {
+  cleanUpReqFiles,
+  deleteFileAsync,
   generateRandomString,
   getFormattedObjects,
   isSignupAllowed,