1 import { registerTSPaths } from '../server/helpers/register-ts-paths'
4 import * as prompt from 'prompt'
5 import { join } from 'path'
6 import { CONFIG } from '../server/initializers/config'
7 import { VideoModel } from '../server/models/video/video'
8 import { initDatabaseModels } from '../server/initializers/database'
9 import { readdir, remove } from 'fs-extra'
10 import { VideoRedundancyModel } from '../server/models/redundancy/video-redundancy'
11 import * as Bluebird from 'bluebird'
12 import { getUUIDFromFilename } from '../server/helpers/utils'
13 import { ThumbnailModel } from '../server/models/video/thumbnail'
14 import { AvatarModel } from '../server/models/avatar/avatar'
15 import { uniq, values } from 'lodash'
18 .then(() => process.exit(0))
24 async function run () {
25 const dirs = values(CONFIG.STORAGE)
27 if (uniq(dirs).length !== dirs.length) {
28 console.error('Cannot prune storage because you put multiple storage keys in the same directory.')
32 await initDatabaseModels(true)
34 let toDelete: string[] = []
36 toDelete = toDelete.concat(
37 await pruneDirectory(CONFIG.STORAGE.VIDEOS_DIR, doesVideoExist(true)),
38 await pruneDirectory(CONFIG.STORAGE.TORRENTS_DIR, doesVideoExist(true)),
40 await pruneDirectory(CONFIG.STORAGE.REDUNDANCY_DIR, doesRedundancyExist),
42 await pruneDirectory(CONFIG.STORAGE.PREVIEWS_DIR, doesThumbnailExist(true)),
43 await pruneDirectory(CONFIG.STORAGE.THUMBNAILS_DIR, doesThumbnailExist(false)),
45 await pruneDirectory(CONFIG.STORAGE.AVATARS_DIR, doesAvatarExist)
48 const tmpFiles = await readdir(CONFIG.STORAGE.TMP_DIR)
49 toDelete = toDelete.concat(tmpFiles.map(t => join(CONFIG.STORAGE.TMP_DIR, t)))
51 if (toDelete.length === 0) {
52 console.log('No files to delete.')
56 console.log('Will delete %d files:\n\n%s\n\n', toDelete.length, toDelete.join('\n'))
58 const res = await askConfirmation()
60 console.log('Processing delete...\n')
62 for (const path of toDelete) {
68 console.log('Exiting without deleting files.')
72 type ExistFun = (file: string) => Promise<boolean>
73 async function pruneDirectory (directory: string, existFun: ExistFun) {
74 const files = await readdir(directory)
76 const toDelete: string[] = []
77 await Bluebird.map(files, async file => {
78 if (await existFun(file) !== true) {
79 toDelete.push(join(directory, file))
81 }, { concurrency: 20 })
86 function doesVideoExist (keepOnlyOwned: boolean) {
87 return async (file: string) => {
88 const uuid = getUUIDFromFilename(file)
89 const video = await VideoModel.loadByUUID(uuid)
91 return video && (keepOnlyOwned === false || video.isOwned())
95 function doesThumbnailExist (keepOnlyOwned: boolean) {
96 return async (file: string) => {
97 const thumbnail = await ThumbnailModel.loadByName(file)
98 if (!thumbnail) return false
101 const video = await VideoModel.load(thumbnail.videoId)
102 if (video.isOwned() === false) return false
109 async function doesAvatarExist (file: string) {
110 const avatar = await AvatarModel.loadByName(file)
115 async function doesRedundancyExist (file: string) {
116 const uuid = getUUIDFromFilename(file)
117 const video = await VideoModel.loadWithFiles(uuid)
119 if (!video) return false
121 const isPlaylist = file.includes('.') === false
124 const p = video.getHLSPlaylist()
127 const redundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(p.id)
131 const resolution = parseInt(file.split('-')[5], 10)
132 if (isNaN(resolution)) {
133 console.error('Cannot prune %s because we cannot guess guess the resolution.', file)
137 const videoFile = video.getWebTorrentFile(resolution)
139 console.error('Cannot find webtorrent file of video %s - %d', video.url, resolution)
143 const redundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id)
147 async function askConfirmation () {
148 return new Promise((res, rej) => {
154 description: 'These following unused files can be deleted, but please check your backups first (bugs happen).' +
155 ' Notice PeerTube must have been stopped when your ran this script.' +
156 ' Can we delete these files?',
162 prompt.get(schema, function (err, result) {
163 if (err) return rej(err)
165 return res(result.confirm?.match(/y/) !== null)