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'
9 .then(() => process.exit(0))
15 async function run () {
16 await initDatabaseModels(true)
18 const storageToPrune = [
19 CONFIG.STORAGE.VIDEOS_DIR,
20 CONFIG.STORAGE.PREVIEWS_DIR,
21 CONFIG.STORAGE.THUMBNAILS_DIR,
22 CONFIG.STORAGE.TORRENTS_DIR
25 let toDelete: string[] = []
26 for (const directory of storageToPrune) {
27 toDelete = toDelete.concat(await pruneDirectory(directory))
30 if (toDelete.length === 0) {
31 console.log('No files to delete.')
35 console.log('Will delete %d files:\n\n%s\n\n', toDelete.length, toDelete.join('\n'))
37 const res = await askConfirmation()
39 console.log('Processing delete...\n')
41 for (const path of toDelete) {
47 console.log('Exiting without deleting files.')
51 async function pruneDirectory (directory: string) {
52 const files = await readdir(directory)
54 const toDelete: string[] = []
55 for (const file of files) {
56 const uuid = getUUIDFromFilename(file)
59 if (uuid) video = await VideoModel.loadByUUIDWithFile(uuid)
61 if (!uuid || !video) toDelete.push(join(directory, file))
67 function getUUIDFromFilename (filename: string) {
68 const regex = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/
69 const result = filename.match(regex)
71 if (!result || Array.isArray(result) === false) return null
76 async function askConfirmation () {
77 return new Promise((res, rej) => {
83 description: 'Are you sure you want to delete these files? Please check carefully',
89 prompt.get(schema, function (err, result) {
90 if (err) return rej(err)
91 return res(result.confirm && result.confirm.match(/y/) !== null)