1 import * as prompt from 'prompt'
2 import { join } from 'path'
3 import { CONFIG } from '../server/initializers/constants'
4 import { VideoModel } from '../server/models/video/video'
5 import { initDatabaseModels } from '../server/initializers'
6 import { remove, readdir } from 'fs-extra'
7 import { VideoRedundancyModel } from '../server/models/redundancy/video-redundancy'
8 import { getUUIDFromFilename } from '../server/helpers/utils'
11 .then(() => process.exit(0))
17 async function run () {
18 await initDatabaseModels(true)
20 const storageOnlyOwnedToPrune = [
21 CONFIG.STORAGE.VIDEOS_DIR,
22 CONFIG.STORAGE.TORRENTS_DIR
25 const storageForAllToPrune = [
26 CONFIG.STORAGE.PREVIEWS_DIR,
27 CONFIG.STORAGE.THUMBNAILS_DIR
30 let toDelete: string[] = []
31 for (const directory of storageOnlyOwnedToPrune) {
32 toDelete = toDelete.concat(await pruneDirectory(directory, true))
35 for (const directory of storageForAllToPrune) {
36 toDelete = toDelete.concat(await pruneDirectory(directory, false))
39 if (toDelete.length === 0) {
40 console.log('No files to delete.')
44 console.log('Will delete %d files:\n\n%s\n\n', toDelete.length, toDelete.join('\n'))
46 const res = await askConfirmation()
48 console.log('Processing delete...\n')
50 for (const path of toDelete) {
56 console.log('Exiting without deleting files.')
60 async function pruneDirectory (directory: string, onlyOwned = false) {
61 const files = await readdir(directory)
63 const toDelete: string[] = []
64 for (const file of files) {
65 const uuid = getUUIDFromFilename(file)
67 let localRedundancy: boolean
70 video = await VideoModel.loadByUUIDWithFile(uuid)
71 localRedundancy = await VideoRedundancyModel.isLocalByVideoUUIDExists(uuid)
77 (onlyOwned === true && (video.isOwned() === false && localRedundancy === false))
79 toDelete.push(join(directory, file))
86 async function askConfirmation () {
87 return new Promise((res, rej) => {
93 description: 'These following unused files can be deleted, but please check your backups first (bugs happen).' +
94 ' Can we delete these files?',
100 prompt.get(schema, function (err, result) {
101 if (err) return rej(err)
102 return res(result.confirm && result.confirm.match(/y/) !== null)