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'
12 import { uniq, values } from 'lodash'
15 .then(() => process.exit(0))
21 async function run () {
22 const dirs = values(CONFIG.STORAGE)
24 if (uniq(dirs).length !== dirs.length) {
25 console.error('Cannot prune storage because you put multiple storage keys in the same directory.')
29 await initDatabaseModels(true)
31 let toDelete: string[] = []
33 toDelete = toDelete.concat(
34 await pruneDirectory(CONFIG.STORAGE.VIDEOS_DIR, doesVideoExist(true)),
35 await pruneDirectory(CONFIG.STORAGE.TORRENTS_DIR, doesVideoExist(true)),
37 await pruneDirectory(CONFIG.STORAGE.REDUNDANCY_DIR, doesRedundancyExist),
39 await pruneDirectory(CONFIG.STORAGE.PREVIEWS_DIR, doesThumbnailExist(true)),
40 await pruneDirectory(CONFIG.STORAGE.THUMBNAILS_DIR, doesThumbnailExist(false)),
42 await pruneDirectory(CONFIG.STORAGE.AVATARS_DIR, doesAvatarExist)
45 const tmpFiles = await readdir(CONFIG.STORAGE.TMP_DIR)
46 toDelete = toDelete.concat(tmpFiles.map(t => join(CONFIG.STORAGE.TMP_DIR, t)))
48 if (toDelete.length === 0) {
49 console.log('No files to delete.')
53 console.log('Will delete %d files:\n\n%s\n\n', toDelete.length, toDelete.join('\n'))
55 const res = await askConfirmation()
57 console.log('Processing delete...\n')
59 for (const path of toDelete) {
65 console.log('Exiting without deleting files.')
69 type ExistFun = (file: string) => Promise<boolean>
70 async function pruneDirectory (directory: string, existFun: ExistFun) {
71 const files = await readdir(directory)
73 const toDelete: string[] = []
74 await Bluebird.map(files, async file => {
75 if (await existFun(file) !== true) {
76 toDelete.push(join(directory, file))
78 }, { concurrency: 20 })
83 function doesVideoExist (keepOnlyOwned: boolean) {
84 return async (file: string) => {
85 const uuid = getUUIDFromFilename(file)
86 const video = await VideoModel.loadByUUID(uuid)
88 return video && (keepOnlyOwned === false || video.isOwned())
92 function doesThumbnailExist (keepOnlyOwned: boolean) {
93 return async (file: string) => {
94 const thumbnail = await ThumbnailModel.loadByName(file)
95 if (!thumbnail) return false
98 const video = await VideoModel.load(thumbnail.videoId)
99 if (video.isOwned() === false) return false
106 async function doesAvatarExist (file: string) {
107 const avatar = await AvatarModel.loadByName(file)
112 async function doesRedundancyExist (file: string) {
113 const uuid = getUUIDFromFilename(file)
114 const video = await VideoModel.loadWithFiles(uuid)
116 if (!video) return false
118 const isPlaylist = file.includes('.') === false
121 const p = video.getHLSPlaylist()
124 const redundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(p.id)
128 const resolution = parseInt(file.split('-')[5], 10)
129 if (isNaN(resolution)) {
130 console.error('Cannot prune %s because we cannot guess guess the resolution.', file)
134 const videoFile = video.getFile(resolution)
136 console.error('Cannot find file of video %s - %d', video.url, resolution)
140 const redundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id)
144 async function askConfirmation () {
145 return new Promise((res, rej) => {
151 description: 'These following unused files can be deleted, but please check your backups first (bugs happen).' +
152 ' Notice PeerTube must have been stopped when your ran this script.' +
153 ' Can we delete these files?',
159 prompt.get(schema, function (err, result) {
160 if (err) return rej(err)
161 return res(result.confirm && result.confirm.match(/y/) !== null)