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, HLS_STREAMING_PLAYLIST_DIRECTORY } from '@server/initializers/constants'
19 import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
22 .then(() => process.exit(0))
28 async function run () {
29 const dirs = values(CONFIG.STORAGE)
31 if (uniq(dirs).length !== dirs.length) {
32 console.error('Cannot prune storage because you put multiple storage keys in the same directory.')
36 await initDatabaseModels(true)
38 let toDelete: string[] = []
40 console.log('Detecting files to remove, it could take a while...')
42 toDelete = toDelete.concat(
43 await pruneDirectory(CONFIG.STORAGE.VIDEOS_DIR, doesWebTorrentFileExist()),
45 await pruneDirectory(HLS_STREAMING_PLAYLIST_DIRECTORY, doesHLSPlaylistExist()),
47 await pruneDirectory(CONFIG.STORAGE.TORRENTS_DIR, doesTorrentFileExist()),
49 await pruneDirectory(CONFIG.STORAGE.REDUNDANCY_DIR, doesRedundancyExist),
51 await pruneDirectory(CONFIG.STORAGE.PREVIEWS_DIR, doesThumbnailExist(true, ThumbnailType.PREVIEW)),
52 await pruneDirectory(CONFIG.STORAGE.THUMBNAILS_DIR, doesThumbnailExist(false, ThumbnailType.MINIATURE)),
54 await pruneDirectory(CONFIG.STORAGE.ACTOR_IMAGES, doesActorImageExist)
57 const tmpFiles = await readdir(CONFIG.STORAGE.TMP_DIR)
58 toDelete = toDelete.concat(tmpFiles.map(t => join(CONFIG.STORAGE.TMP_DIR, t)))
60 if (toDelete.length === 0) {
61 console.log('No files to delete.')
65 console.log('Will delete %d files:\n\n%s\n\n', toDelete.length, toDelete.join('\n'))
67 const res = await askConfirmation()
69 console.log('Processing delete...\n')
71 for (const path of toDelete) {
77 console.log('Exiting without deleting files.')
81 type ExistFun = (file: string) => Promise<boolean>
82 async function pruneDirectory (directory: string, existFun: ExistFun) {
83 const files = await readdir(directory)
85 const toDelete: string[] = []
86 await map(files, async file => {
87 const filePath = join(directory, file)
89 if (await existFun(filePath) !== true) {
90 toDelete.push(filePath)
92 }, { concurrency: 20 })
97 function doesWebTorrentFileExist () {
98 return (filePath: string) => VideoFileModel.doesOwnedWebTorrentVideoFileExist(basename(filePath))
101 function doesHLSPlaylistExist () {
102 return (hlsPath: string) => VideoStreamingPlaylistModel.doesOwnedHLSPlaylistExist(basename(hlsPath))
105 function doesTorrentFileExist () {
106 return (filePath: string) => VideoFileModel.doesOwnedTorrentFileExist(basename(filePath))
109 function doesThumbnailExist (keepOnlyOwned: boolean, type: ThumbnailType) {
110 return async (filePath: string) => {
111 const thumbnail = await ThumbnailModel.loadByFilename(basename(filePath), type)
112 if (!thumbnail) return false
115 const video = await VideoModel.load(thumbnail.videoId)
116 if (video.isOwned() === false) return false
123 async function doesActorImageExist (filePath: string) {
124 const image = await ActorImageModel.loadByName(basename(filePath))
129 async function doesRedundancyExist (filePath: string) {
130 const isPlaylist = (await stat(filePath)).isDirectory()
133 // Don't delete HLS directory
134 if (filePath === HLS_REDUNDANCY_DIRECTORY) return true
136 const uuid = getUUIDFromFilename(filePath)
137 const video = await VideoModel.loadWithFiles(uuid)
138 if (!video) return false
140 const p = video.getHLSPlaylist()
143 const redundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(p.id)
147 const file = await VideoFileModel.loadByFilename(basename(filePath))
148 if (!file) return false
150 const redundancy = await VideoRedundancyModel.loadLocalByFileId(file.id)
154 async function askConfirmation () {
155 return new Promise((res, rej) => {
161 description: 'These following unused files can be deleted, but please check your backups first (bugs happen).' +
162 ' Notice PeerTube must have been stopped when your ran this script.' +
163 ' Can we delete these files?',
169 get(schema, function (err, result) {
170 if (err) return rej(err)
172 return res(result.confirm?.match(/y/) !== null)