1 import { registerTSPaths } from '../server/helpers/register-ts-paths'
4 import * as prompt 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 * 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'
17 import { VideoFileModel } from '@server/models/video/video-file'
20 .then(() => process.exit(0))
26 async function run () {
27 const dirs = values(CONFIG.STORAGE)
29 if (uniq(dirs).length !== dirs.length) {
30 console.error('Cannot prune storage because you put multiple storage keys in the same directory.')
34 await initDatabaseModels(true)
36 let toDelete: string[] = []
38 console.log('Detecting files to remove, it could take a while...')
40 toDelete = toDelete.concat(
41 await pruneDirectory(CONFIG.STORAGE.VIDEOS_DIR, doesWebTorrentFileExist()),
42 await pruneDirectory(CONFIG.STORAGE.TORRENTS_DIR, doesTorrentFileExist()),
44 await pruneDirectory(CONFIG.STORAGE.REDUNDANCY_DIR, doesRedundancyExist),
46 await pruneDirectory(CONFIG.STORAGE.PREVIEWS_DIR, doesThumbnailExist(true, ThumbnailType.PREVIEW)),
47 await pruneDirectory(CONFIG.STORAGE.THUMBNAILS_DIR, doesThumbnailExist(false, ThumbnailType.MINIATURE)),
49 await pruneDirectory(CONFIG.STORAGE.ACTOR_IMAGES, doesActorImageExist)
52 const tmpFiles = await readdir(CONFIG.STORAGE.TMP_DIR)
53 toDelete = toDelete.concat(tmpFiles.map(t => join(CONFIG.STORAGE.TMP_DIR, t)))
55 if (toDelete.length === 0) {
56 console.log('No files to delete.')
60 console.log('Will delete %d files:\n\n%s\n\n', toDelete.length, toDelete.join('\n'))
62 const res = await askConfirmation()
64 console.log('Processing delete...\n')
66 for (const path of toDelete) {
72 console.log('Exiting without deleting files.')
76 type ExistFun = (file: string) => Promise<boolean>
77 async function pruneDirectory (directory: string, existFun: ExistFun) {
78 const files = await readdir(directory)
80 const toDelete: string[] = []
81 await Bluebird.map(files, async file => {
82 const filePath = join(directory, file)
84 if (await existFun(filePath) !== true) {
85 toDelete.push(filePath)
87 }, { concurrency: 20 })
92 function doesWebTorrentFileExist () {
93 return (filePath: string) => VideoFileModel.doesOwnedWebTorrentVideoFileExist(basename(filePath))
96 function doesTorrentFileExist () {
97 return (filePath: string) => VideoFileModel.doesOwnedTorrentFileExist(basename(filePath))
100 function doesThumbnailExist (keepOnlyOwned: boolean, type: ThumbnailType) {
101 return async (filePath: string) => {
102 const thumbnail = await ThumbnailModel.loadByFilename(basename(filePath), type)
103 if (!thumbnail) return false
106 const video = await VideoModel.load(thumbnail.videoId)
107 if (video.isOwned() === false) return false
114 async function doesActorImageExist (filePath: string) {
115 const image = await ActorImageModel.loadByName(basename(filePath))
120 async function doesRedundancyExist (filePath: string) {
121 const isPlaylist = (await stat(filePath)).isDirectory()
124 const uuid = getUUIDFromFilename(filePath)
125 const video = await VideoModel.loadWithFiles(uuid)
126 if (!video) return false
128 const p = video.getHLSPlaylist()
131 const redundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(p.id)
135 const file = await VideoFileModel.loadByFilename(basename(filePath))
136 if (!file) return false
138 const redundancy = await VideoRedundancyModel.loadLocalByFileId(file.id)
142 async function askConfirmation () {
143 return new Promise((res, rej) => {
149 description: 'These following unused files can be deleted, but please check your backups first (bugs happen).' +
150 ' Notice PeerTube must have been stopped when your ran this script.' +
151 ' Can we delete these files?',
157 prompt.get(schema, function (err, result) {
158 if (err) return rej(err)
160 return res(result.confirm?.match(/y/) !== null)