1 import { registerTSPaths } from '../server/helpers/register-ts-paths'
4 import { start, get } from 'prompt'
5 import { join, basename } 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, stat } from 'fs-extra'
10 import { VideoRedundancyModel } from '../server/models/redundancy/video-redundancy'
11 import { map } 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'
17 import { VideoFileModel } from '@server/models/video/video-file'
18 import { HLS_REDUNDANCY_DIRECTORY } from '@server/initializers/constants'
21 .then(() => process.exit(0))
27 async function run () {
28 const dirs = values(CONFIG.STORAGE)
30 if (uniq(dirs).length !== dirs.length) {
31 console.error('Cannot prune storage because you put multiple storage keys in the same directory.')
35 await initDatabaseModels(true)
37 let toDelete: string[] = []
39 console.log('Detecting files to remove, it could take a while...')
41 toDelete = toDelete.concat(
42 await pruneDirectory(CONFIG.STORAGE.VIDEOS_DIR, doesWebTorrentFileExist()),
43 await pruneDirectory(CONFIG.STORAGE.TORRENTS_DIR, doesTorrentFileExist()),
45 await pruneDirectory(CONFIG.STORAGE.REDUNDANCY_DIR, doesRedundancyExist),
47 await pruneDirectory(CONFIG.STORAGE.PREVIEWS_DIR, doesThumbnailExist(true, ThumbnailType.PREVIEW)),
48 await pruneDirectory(CONFIG.STORAGE.THUMBNAILS_DIR, doesThumbnailExist(false, ThumbnailType.MINIATURE)),
50 await pruneDirectory(CONFIG.STORAGE.ACTOR_IMAGES, doesActorImageExist)
53 const tmpFiles = await readdir(CONFIG.STORAGE.TMP_DIR)
54 toDelete = toDelete.concat(tmpFiles.map(t => join(CONFIG.STORAGE.TMP_DIR, t)))
56 if (toDelete.length === 0) {
57 console.log('No files to delete.')
61 console.log('Will delete %d files:\n\n%s\n\n', toDelete.length, toDelete.join('\n'))
63 const res = await askConfirmation()
65 console.log('Processing delete...\n')
67 for (const path of toDelete) {
73 console.log('Exiting without deleting files.')
77 type ExistFun = (file: string) => Promise<boolean>
78 async function pruneDirectory (directory: string, existFun: ExistFun) {
79 const files = await readdir(directory)
81 const toDelete: string[] = []
82 await map(files, async file => {
83 const filePath = join(directory, file)
85 if (await existFun(filePath) !== true) {
86 toDelete.push(filePath)
88 }, { concurrency: 20 })
93 function doesWebTorrentFileExist () {
94 return (filePath: string) => VideoFileModel.doesOwnedWebTorrentVideoFileExist(basename(filePath))
97 function doesTorrentFileExist () {
98 return (filePath: string) => VideoFileModel.doesOwnedTorrentFileExist(basename(filePath))
101 function doesThumbnailExist (keepOnlyOwned: boolean, type: ThumbnailType) {
102 return async (filePath: string) => {
103 const thumbnail = await ThumbnailModel.loadByFilename(basename(filePath), type)
104 if (!thumbnail) return false
107 const video = await VideoModel.load(thumbnail.videoId)
108 if (video.isOwned() === false) return false
115 async function doesActorImageExist (filePath: string) {
116 const image = await ActorImageModel.loadByName(basename(filePath))
121 async function doesRedundancyExist (filePath: string) {
122 const isPlaylist = (await stat(filePath)).isDirectory()
125 // Don't delete HLS directory
126 if (filePath === HLS_REDUNDANCY_DIRECTORY) return true
128 const uuid = getUUIDFromFilename(filePath)
129 const video = await VideoModel.loadWithFiles(uuid)
130 if (!video) return false
132 const p = video.getHLSPlaylist()
135 const redundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(p.id)
139 const file = await VideoFileModel.loadByFilename(basename(filePath))
140 if (!file) return false
142 const redundancy = await VideoRedundancyModel.loadLocalByFileId(file.id)
146 async function askConfirmation () {
147 return new Promise((res, rej) => {
153 description: 'These following unused files can be deleted, but please check your backups first (bugs happen).' +
154 ' Notice PeerTube must have been stopped when your ran this script.' +
155 ' Can we delete these files?',
161 get(schema, function (err, result) {
162 if (err) return rej(err)
164 return res(result.confirm?.match(/y/) !== null)