Remove max duration/filesize constraints
[oweals/peertube.git] / server / initializers / installer.ts
index c617b16c9dc4e1576e85a64636770f2eb110cfa7..9545160571457f63decf73fb9d55ec3f316407f9 100644 (file)
@@ -1,21 +1,25 @@
 import * as passwordGenerator from 'password-generator'
 import { UserRole } from '../../shared'
 import { logger, mkdirpPromise, rimrafPromise } from '../helpers'
-import { createPrivateAndPublicKeys } from '../helpers/peertube-crypto'
 import { createUserAccountAndChannel } from '../lib'
-import { clientsExist, usersExist } from './checker'
-import { CACHE, CONFIG, LAST_MIGRATION_VERSION } from './constants'
-
+import { createLocalAccountWithoutKeys } from '../lib/user'
+import { applicationExist, clientsExist, usersExist } from './checker'
+import { CACHE, CONFIG, LAST_MIGRATION_VERSION, SERVER_ACCOUNT_NAME } from './constants'
 import { database as db } from './database'
-import { createLocalAccount } from '../lib/user'
+import { createPrivateAndPublicKeys } from '../helpers/peertube-crypto'
 
 async function installApplication () {
-  await db.sequelize.sync()
-  await removeCacheDirectories()
-  await createDirectoriesIfNotExist()
-  await createOAuthClientIfNotExist()
-  await createOAuthAdminIfNotExist()
-  await createApplicationIfNotExist()
+  try {
+    await db.sequelize.sync()
+    await removeCacheDirectories()
+    await createDirectoriesIfNotExist()
+    await createApplicationIfNotExist()
+    await createOAuthClientIfNotExist()
+    await createOAuthAdminIfNotExist()
+  } catch (err) {
+    logger.error('Cannot install application.', err)
+    throw err
+  }
 }
 
 // ---------------------------------------------------------------------------
@@ -124,9 +128,20 @@ async function createOAuthAdminIfNotExist () {
 }
 
 async function createApplicationIfNotExist () {
+  const exist = await applicationExist(db.Application)
+  // Nothing to do, application already exist
+  if (exist === true) return undefined
+
   logger.info('Creating Application table.')
   const applicationInstance = await db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION })
 
   logger.info('Creating application account.')
-  return createLocalAccount('peertube', null, applicationInstance.id, undefined)
+
+  const accountCreated = await createLocalAccountWithoutKeys(SERVER_ACCOUNT_NAME, null, applicationInstance.id, undefined)
+
+  const { publicKey, privateKey } = await createPrivateAndPublicKeys()
+  accountCreated.set('publicKey', publicKey)
+  accountCreated.set('privateKey', privateKey)
+
+  return accountCreated.save()
 }