]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/initializers/installer.ts
Translated using Weblate (Russian)
[github/Chocobozzz/PeerTube.git] / server / initializers / installer.ts
index 0517e00840725ad33926d58b5db3aedff4949746..f5d8eedf1916a687333ebb118093b6f328466083 100644 (file)
@@ -1,5 +1,8 @@
-import { ensureDir, remove } from 'fs-extra'
+import { ensureDir, readdir, remove } from 'fs-extra'
 import passwordGenerator from 'password-generator'
+import { join } from 'path'
+import { isTestOrDevInstance } from '@server/helpers/core-utils'
+import { getNodeABIVersion } from '@server/helpers/version'
 import { UserRole } from '@shared/models'
 import { logger } from '../helpers/logger'
 import { buildUser, createApplicationActor, createUserAccountAndChannelAndPlaylist } from '../lib/user'
@@ -7,7 +10,7 @@ import { ApplicationModel } from '../models/application/application'
 import { OAuthClientModel } from '../models/oauth/oauth-client'
 import { applicationExist, clientsExist, usersExist } from './checker-after-init'
 import { CONFIG } from './config'
-import { FILES_CACHE, HLS_STREAMING_PLAYLIST_DIRECTORY, LAST_MIGRATION_VERSION, RESUMABLE_UPLOAD_DIRECTORY } from './constants'
+import { DIRECTORIES, FILES_CACHE, LAST_MIGRATION_VERSION } from './constants'
 import { sequelizeTypescript } from './database'
 
 async function installApplication () {
@@ -50,14 +53,28 @@ function removeCacheAndTmpDirectories () {
   // Cache directories
   for (const key of Object.keys(cacheDirectories)) {
     const dir = cacheDirectories[key]
-    tasks.push(remove(dir))
+    tasks.push(removeDirectoryOrContent(dir))
   }
 
-  tasks.push(remove(CONFIG.STORAGE.TMP_DIR))
+  tasks.push(removeDirectoryOrContent(CONFIG.STORAGE.TMP_DIR))
 
   return Promise.all(tasks)
 }
 
+async function removeDirectoryOrContent (dir: string) {
+  try {
+    await remove(dir)
+  } catch (err) {
+    logger.debug('Cannot remove directory %s. Removing content instead.', dir, { err })
+
+    const files = await readdir(dir)
+
+    for (const file of files) {
+      await remove(join(dir, file))
+    }
+  }
+}
+
 function createDirectoriesIfNotExist () {
   const storage = CONFIG.STORAGE
   const cacheDirectories = Object.keys(FILES_CACHE)
@@ -75,11 +92,13 @@ function createDirectoriesIfNotExist () {
     tasks.push(ensureDir(dir))
   }
 
-  // Playlist directories
-  tasks.push(ensureDir(HLS_STREAMING_PLAYLIST_DIRECTORY))
+  tasks.push(ensureDir(DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE))
+  tasks.push(ensureDir(DIRECTORIES.HLS_STREAMING_PLAYLIST.PUBLIC))
+  tasks.push(ensureDir(DIRECTORIES.VIDEOS.PUBLIC))
+  tasks.push(ensureDir(DIRECTORIES.VIDEOS.PRIVATE))
 
   // Resumable upload directory
-  tasks.push(ensureDir(RESUMABLE_UPLOAD_DIRECTORY))
+  tasks.push(ensureDir(DIRECTORIES.RESUMABLE_UPLOAD))
 
   return Promise.all(tasks)
 }
@@ -120,8 +139,8 @@ async function createOAuthAdminIfNotExist () {
   let validatePassword = true
   let password = ''
 
-  // Do not generate a random password for tests
-  if (process.env.NODE_ENV === 'test') {
+  // Do not generate a random password for test and dev environments
+  if (isTestOrDevInstance()) {
     password = 'test'
 
     if (process.env.NODE_APP_INSTANCE) {
@@ -159,7 +178,9 @@ async function createApplicationIfNotExist () {
   logger.info('Creating application account.')
 
   const application = await ApplicationModel.create({
-    migrationVersion: LAST_MIGRATION_VERSION
+    migrationVersion: LAST_MIGRATION_VERSION,
+    nodeVersion: process.version,
+    nodeABIVersion: getNodeABIVersion()
   })
 
   return createApplicationActor(application.id)