]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - scripts/prune-storage.ts
fix account URI in remote comment modal (partial rollback)
[github/Chocobozzz/PeerTube.git] / scripts / prune-storage.ts
index 1973725c86ebda92de21a3b14f966fa18d63d5a5..4088fa70046877e7a4620808b192f5addde15976 100755 (executable)
@@ -1,11 +1,10 @@
 import * as prompt from 'prompt'
-import { createReadStream } from 'fs'
 import { join } from 'path'
-import { createInterface } from 'readline'
-import { readdirPromise, unlinkPromise } from '../server/helpers/core-utils'
 import { CONFIG } from '../server/initializers/constants'
 import { VideoModel } from '../server/models/video/video'
 import { initDatabaseModels } from '../server/initializers'
+import { remove, readdir } from 'fs-extra'
+import { VideoRedundancyModel } from '../server/models/redundancy/video-redundancy'
 
 run()
   .then(() => process.exit(0))
@@ -17,16 +16,23 @@ run()
 async function run () {
   await initDatabaseModels(true)
 
-  const storageToPrune = [
+  const storageOnlyOwnedToPrune = [
     CONFIG.STORAGE.VIDEOS_DIR,
-    CONFIG.STORAGE.PREVIEWS_DIR,
-    CONFIG.STORAGE.THUMBNAILS_DIR,
     CONFIG.STORAGE.TORRENTS_DIR
   ]
 
+  const storageForAllToPrune = [
+    CONFIG.STORAGE.PREVIEWS_DIR,
+    CONFIG.STORAGE.THUMBNAILS_DIR
+  ]
+
   let toDelete: string[] = []
-  for (const directory of storageToPrune) {
-    toDelete = toDelete.concat(await pruneDirectory(directory))
+  for (const directory of storageOnlyOwnedToPrune) {
+    toDelete = toDelete.concat(await pruneDirectory(directory, true))
+  }
+
+  for (const directory of storageForAllToPrune) {
+    toDelete = toDelete.concat(await pruneDirectory(directory, false))
   }
 
   if (toDelete.length === 0) {
@@ -41,7 +47,7 @@ async function run () {
     console.log('Processing delete...\n')
 
     for (const path of toDelete) {
-      await unlinkPromise(path)
+      await remove(path)
     }
 
     console.log('Done!')
@@ -50,17 +56,27 @@ async function run () {
   }
 }
 
-async function pruneDirectory (directory: string) {
-  const files = await readdirPromise(directory)
+async function pruneDirectory (directory: string, onlyOwned = false) {
+  const files = await readdir(directory)
 
   const toDelete: string[] = []
   for (const file of files) {
     const uuid = getUUIDFromFilename(file)
     let video: VideoModel
+    let localRedundancy: boolean
 
-    if (uuid) video = await VideoModel.loadByUUID(uuid)
+    if (uuid) {
+      video = await VideoModel.loadByUUIDWithFile(uuid)
+      localRedundancy = await VideoRedundancyModel.isLocalByVideoUUIDExists(uuid)
+    }
 
-    if (!uuid || !video) toDelete.push(join(directory, file))
+    if (
+      !uuid ||
+      !video ||
+      (onlyOwned === true && (video.isOwned() === false && localRedundancy === false))
+    ) {
+      toDelete.push(join(directory, file))
+    }
   }
 
   return toDelete
@@ -82,7 +98,8 @@ async function askConfirmation () {
       properties: {
         confirm: {
           type: 'string',
-          description: 'Are you sure you want to delete these files? Please check carefully',
+          description: 'These following unused files can be deleted, but please check your backups first (bugs happen).' +
+            ' Can we delete these files?',
           default: 'n',
           required: true
         }