1 import * as prompt from 'prompt'
2 import { join } from 'path'
3 import { CONFIG } from '../server/initializers/config'
4 import { VideoModel } from '../server/models/video/video'
5 import { initDatabaseModels } from '../server/initializers'
6 import { readdir, remove } from 'fs-extra'
7 import { VideoRedundancyModel } from '../server/models/redundancy/video-redundancy'
8 import * as Bluebird from 'bluebird'
9 import { getUUIDFromFilename } from '../server/helpers/utils'
10 import { ThumbnailModel } from '../server/models/video/thumbnail'
11 import { AvatarModel } from '../server/models/avatar/avatar'
14 .then(() => process.exit(0))
20 async function run () {
21 await initDatabaseModels(true)
23 let toDelete: string[] = []
25 toDelete = toDelete.concat(
26 await pruneDirectory(CONFIG.STORAGE.VIDEOS_DIR, doesVideoExist(true)),
27 await pruneDirectory(CONFIG.STORAGE.TORRENTS_DIR, doesVideoExist(true)),
29 await pruneDirectory(CONFIG.STORAGE.REDUNDANCY_DIR, doesRedundancyExist),
31 await pruneDirectory(CONFIG.STORAGE.PREVIEWS_DIR, doesThumbnailExist(true)),
32 await pruneDirectory(CONFIG.STORAGE.THUMBNAILS_DIR, doesThumbnailExist(false)),
34 await pruneDirectory(CONFIG.STORAGE.AVATARS_DIR, doesAvatarExist)
37 const tmpFiles = await readdir(CONFIG.STORAGE.TMP_DIR)
38 toDelete = toDelete.concat(tmpFiles.map(t => join(CONFIG.STORAGE.TMP_DIR, t)))
40 if (toDelete.length === 0) {
41 console.log('No files to delete.')
45 console.log('Will delete %d files:\n\n%s\n\n', toDelete.length, toDelete.join('\n'))
47 const res = await askConfirmation()
49 console.log('Processing delete...\n')
51 for (const path of toDelete) {
57 console.log('Exiting without deleting files.')
61 type ExistFun = (file: string) => Promise<boolean>
62 async function pruneDirectory (directory: string, existFun: ExistFun) {
63 const files = await readdir(directory)
65 const toDelete: string[] = []
66 await Bluebird.map(files, async file => {
67 if (await existFun(file) !== true) {
68 toDelete.push(join(directory, file))
70 }, { concurrency: 20 })
75 function doesVideoExist (keepOnlyOwned: boolean) {
76 return async (file: string) => {
77 const uuid = getUUIDFromFilename(file)
78 const video = await VideoModel.loadByUUID(uuid)
80 return video && (keepOnlyOwned === false || video.isOwned())
84 function doesThumbnailExist (keepOnlyOwned: boolean) {
85 return async (file: string) => {
86 const thumbnail = await ThumbnailModel.loadByName(file)
87 if (!thumbnail) return false
90 const video = await VideoModel.load(thumbnail.videoId)
91 if (video.isOwned() === false) return false
98 async function doesAvatarExist (file: string) {
99 const avatar = await AvatarModel.loadByName(file)
104 async function doesRedundancyExist (file: string) {
105 const uuid = getUUIDFromFilename(file)
106 const video = await VideoModel.loadWithFiles(uuid)
108 if (!video) return false
110 const isPlaylist = file.includes('.') === false
113 const p = video.getHLSPlaylist()
116 const redundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(p.id)
120 const resolution = parseInt(file.split('-')[5], 10)
121 if (isNaN(resolution)) {
122 console.error('Cannot prune %s because we cannot guess guess the resolution.', file)
126 const videoFile = video.getFile(resolution)
128 console.error('Cannot find file of video %s - %d', video.url, resolution)
132 const redundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id)
136 async function askConfirmation () {
137 return new Promise((res, rej) => {
143 description: 'These following unused files can be deleted, but please check your backups first (bugs happen).' +
144 ' Notice PeerTube must have been stopped when your ran this script.' +
145 ' Can we delete these files?',
151 prompt.get(schema, function (err, result) {
152 if (err) return rej(err)
153 return res(result.confirm && result.confirm.match(/y/) !== null)