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)
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) {
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
}
// ---------------------------------------------------------------------------
export {
+ cleanUpReqFiles,
+ deleteFileAsync,
generateRandomString,
getFormattedObjects,
isSignupAllowed,