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'
16 import { ThumbnailType } from '@shared/models'
19 .then(() => process.exit(0))
25 async function run () {
26 const dirs = values(CONFIG.STORAGE)
28 if (uniq(dirs).length !== dirs.length) {
29 console.error('Cannot prune storage because you put multiple storage keys in the same directory.')
33 await initDatabaseModels(true)
35 let toDelete: string[] = []
37 toDelete = toDelete.concat(
38 await pruneDirectory(CONFIG.STORAGE.VIDEOS_DIR, doesVideoExist(true)),
39 await pruneDirectory(CONFIG.STORAGE.TORRENTS_DIR, doesVideoExist(true)),
41 await pruneDirectory(CONFIG.STORAGE.REDUNDANCY_DIR, doesRedundancyExist),
43 await pruneDirectory(CONFIG.STORAGE.PREVIEWS_DIR, doesThumbnailExist(true, ThumbnailType.PREVIEW)),
44 await pruneDirectory(CONFIG.STORAGE.THUMBNAILS_DIR, doesThumbnailExist(false, ThumbnailType.MINIATURE)),
46 await pruneDirectory(CONFIG.STORAGE.AVATARS_DIR, doesAvatarExist)
49 const tmpFiles = await readdir(CONFIG.STORAGE.TMP_DIR)
50 toDelete = toDelete.concat(tmpFiles.map(t => join(CONFIG.STORAGE.TMP_DIR, t)))
52 if (toDelete.length === 0) {
53 console.log('No files to delete.')
57 console.log('Will delete %d files:\n\n%s\n\n', toDelete.length, toDelete.join('\n'))
59 const res = await askConfirmation()
61 console.log('Processing delete...\n')
63 for (const path of toDelete) {
69 console.log('Exiting without deleting files.')
73 type ExistFun = (file: string) => Promise<boolean>
74 async function pruneDirectory (directory: string, existFun: ExistFun) {
75 const files = await readdir(directory)
77 const toDelete: string[] = []
78 await Bluebird.map(files, async file => {
79 if (await existFun(file) !== true) {
80 toDelete.push(join(directory, file))
82 }, { concurrency: 20 })
87 function doesVideoExist (keepOnlyOwned: boolean) {
88 return async (file: string) => {
89 const uuid = getUUIDFromFilename(file)
90 const video = await VideoModel.loadByUUID(uuid)
92 return video && (keepOnlyOwned === false || video.isOwned())
96 function doesThumbnailExist (keepOnlyOwned: boolean, type: ThumbnailType) {
97 return async (file: string) => {
98 const thumbnail = await ThumbnailModel.loadByFilename(file, type)
99 if (!thumbnail) return false
102 const video = await VideoModel.load(thumbnail.videoId)
103 if (video.isOwned() === false) return false
110 async function doesAvatarExist (file: string) {
111 const avatar = await AvatarModel.loadByName(file)
116 async function doesRedundancyExist (file: string) {
117 const uuid = getUUIDFromFilename(file)
118 const video = await VideoModel.loadWithFiles(uuid)
120 if (!video) return false
122 const isPlaylist = file.includes('.') === false
125 const p = video.getHLSPlaylist()
128 const redundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(p.id)
132 const resolution = parseInt(file.split('-')[5], 10)
133 if (isNaN(resolution)) {
134 console.error('Cannot prune %s because we cannot guess guess the resolution.', file)
138 const videoFile = video.getWebTorrentFile(resolution)
140 console.error('Cannot find webtorrent file of video %s - %d', video.url, resolution)
144 const redundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id)
148 async function askConfirmation () {
149 return new Promise((res, rej) => {
155 description: 'These following unused files can be deleted, but please check your backups first (bugs happen).' +
156 ' Notice PeerTube must have been stopped when your ran this script.' +
157 ' Can we delete these files?',
163 prompt.get(schema, function (err, result) {
164 if (err) return rej(err)
166 return res(result.confirm?.match(/y/) !== null)