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 { ActorImageModel } from '../server/models/actor/actor-image'
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 console.log('Detecting files to remove, it could take a while...')
39 toDelete = toDelete.concat(
40 await pruneDirectory(CONFIG.STORAGE.VIDEOS_DIR, doesVideoExist(true)),
41 await pruneDirectory(CONFIG.STORAGE.TORRENTS_DIR, doesVideoExist(true)),
43 await pruneDirectory(CONFIG.STORAGE.REDUNDANCY_DIR, doesRedundancyExist),
45 await pruneDirectory(CONFIG.STORAGE.PREVIEWS_DIR, doesThumbnailExist(true, ThumbnailType.PREVIEW)),
46 await pruneDirectory(CONFIG.STORAGE.THUMBNAILS_DIR, doesThumbnailExist(false, ThumbnailType.MINIATURE)),
48 await pruneDirectory(CONFIG.STORAGE.ACTOR_IMAGES, doesActorImageExist)
51 const tmpFiles = await readdir(CONFIG.STORAGE.TMP_DIR)
52 toDelete = toDelete.concat(tmpFiles.map(t => join(CONFIG.STORAGE.TMP_DIR, t)))
54 if (toDelete.length === 0) {
55 console.log('No files to delete.')
59 console.log('Will delete %d files:\n\n%s\n\n', toDelete.length, toDelete.join('\n'))
61 const res = await askConfirmation()
63 console.log('Processing delete...\n')
65 for (const path of toDelete) {
71 console.log('Exiting without deleting files.')
75 type ExistFun = (file: string) => Promise<boolean>
76 async function pruneDirectory (directory: string, existFun: ExistFun) {
77 const files = await readdir(directory)
79 const toDelete: string[] = []
80 await Bluebird.map(files, async file => {
81 if (await existFun(file) !== true) {
82 toDelete.push(join(directory, file))
84 }, { concurrency: 20 })
89 function doesVideoExist (keepOnlyOwned: boolean) {
90 return async (file: string) => {
91 const uuid = getUUIDFromFilename(file)
92 const video = await VideoModel.load(uuid)
94 return video && (keepOnlyOwned === false || video.isOwned())
98 function doesThumbnailExist (keepOnlyOwned: boolean, type: ThumbnailType) {
99 return async (file: string) => {
100 const thumbnail = await ThumbnailModel.loadByFilename(file, type)
101 if (!thumbnail) return false
104 const video = await VideoModel.load(thumbnail.videoId)
105 if (video.isOwned() === false) return false
112 async function doesActorImageExist (file: string) {
113 const image = await ActorImageModel.loadByName(file)
118 async function doesRedundancyExist (file: string) {
119 const uuid = getUUIDFromFilename(file)
120 const video = await VideoModel.loadWithFiles(uuid)
122 if (!video) return false
124 const isPlaylist = file.includes('.') === false
127 const p = video.getHLSPlaylist()
130 const redundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(p.id)
134 const resolution = parseInt(file.split('-')[5], 10)
135 if (isNaN(resolution)) {
136 console.error('Cannot prune %s because we cannot guess guess the resolution.', file)
140 const videoFile = video.getWebTorrentFile(resolution)
142 console.error('Cannot find webtorrent file of video %s - %d', video.url, resolution)
146 const redundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id)
150 async function askConfirmation () {
151 return new Promise((res, rej) => {
157 description: 'These following unused files can be deleted, but please check your backups first (bugs happen).' +
158 ' Notice PeerTube must have been stopped when your ran this script.' +
159 ' Can we delete these files?',
165 prompt.get(schema, function (err, result) {
166 if (err) return rej(err)
168 return res(result.confirm?.match(/y/) !== null)