1 import { map } from 'bluebird'
2 import { readdir, remove, stat } from 'fs-extra'
3 import { uniq, values } from 'lodash'
4 import { basename, join } from 'path'
5 import { get, start } from 'prompt'
6 import { HLS_REDUNDANCY_DIRECTORY, HLS_STREAMING_PLAYLIST_DIRECTORY } from '@server/initializers/constants'
7 import { VideoFileModel } from '@server/models/video/video-file'
8 import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
9 import { ThumbnailType } from '@shared/models'
10 import { getUUIDFromFilename } from '../server/helpers/utils'
11 import { CONFIG } from '../server/initializers/config'
12 import { initDatabaseModels } from '../server/initializers/database'
13 import { ActorImageModel } from '../server/models/actor/actor-image'
14 import { VideoRedundancyModel } from '../server/models/redundancy/video-redundancy'
15 import { ThumbnailModel } from '../server/models/video/thumbnail'
16 import { VideoModel } from '../server/models/video/video'
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, doesWebTorrentFileExist()),
42 await pruneDirectory(HLS_STREAMING_PLAYLIST_DIRECTORY, doesHLSPlaylistExist()),
44 await pruneDirectory(CONFIG.STORAGE.TORRENTS_DIR, doesTorrentFileExist()),
46 await pruneDirectory(CONFIG.STORAGE.REDUNDANCY_DIR, doesRedundancyExist),
48 await pruneDirectory(CONFIG.STORAGE.PREVIEWS_DIR, doesThumbnailExist(true, ThumbnailType.PREVIEW)),
49 await pruneDirectory(CONFIG.STORAGE.THUMBNAILS_DIR, doesThumbnailExist(false, ThumbnailType.MINIATURE)),
51 await pruneDirectory(CONFIG.STORAGE.ACTOR_IMAGES, doesActorImageExist)
54 const tmpFiles = await readdir(CONFIG.STORAGE.TMP_DIR)
55 toDelete = toDelete.concat(tmpFiles.map(t => join(CONFIG.STORAGE.TMP_DIR, t)))
57 if (toDelete.length === 0) {
58 console.log('No files to delete.')
62 console.log('Will delete %d files:\n\n%s\n\n', toDelete.length, toDelete.join('\n'))
64 const res = await askConfirmation()
66 console.log('Processing delete...\n')
68 for (const path of toDelete) {
74 console.log('Exiting without deleting files.')
78 type ExistFun = (file: string) => Promise<boolean>
79 async function pruneDirectory (directory: string, existFun: ExistFun) {
80 const files = await readdir(directory)
82 const toDelete: string[] = []
83 await map(files, async file => {
84 const filePath = join(directory, file)
86 if (await existFun(filePath) !== true) {
87 toDelete.push(filePath)
89 }, { concurrency: 20 })
94 function doesWebTorrentFileExist () {
95 return (filePath: string) => VideoFileModel.doesOwnedWebTorrentVideoFileExist(basename(filePath))
98 function doesHLSPlaylistExist () {
99 return (hlsPath: string) => VideoStreamingPlaylistModel.doesOwnedHLSPlaylistExist(basename(hlsPath))
102 function doesTorrentFileExist () {
103 return (filePath: string) => VideoFileModel.doesOwnedTorrentFileExist(basename(filePath))
106 function doesThumbnailExist (keepOnlyOwned: boolean, type: ThumbnailType) {
107 return async (filePath: string) => {
108 const thumbnail = await ThumbnailModel.loadByFilename(basename(filePath), type)
109 if (!thumbnail) return false
112 const video = await VideoModel.load(thumbnail.videoId)
113 if (video.isOwned() === false) return false
120 async function doesActorImageExist (filePath: string) {
121 const image = await ActorImageModel.loadByName(basename(filePath))
126 async function doesRedundancyExist (filePath: string) {
127 const isPlaylist = (await stat(filePath)).isDirectory()
130 // Don't delete HLS directory
131 if (filePath === HLS_REDUNDANCY_DIRECTORY) return true
133 const uuid = getUUIDFromFilename(filePath)
134 const video = await VideoModel.loadWithFiles(uuid)
135 if (!video) return false
137 const p = video.getHLSPlaylist()
140 const redundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(p.id)
144 const file = await VideoFileModel.loadByFilename(basename(filePath))
145 if (!file) return false
147 const redundancy = await VideoRedundancyModel.loadLocalByFileId(file.id)
151 async function askConfirmation () {
152 return new Promise((res, rej) => {
158 description: 'These following unused files can be deleted, but please check your backups first (bugs happen).' +
159 ' Notice PeerTube must have been stopped when your ran this script.' +
160 ' Can we delete these files?',
166 get(schema, function (err, result) {
167 if (err) return rej(err)
169 return res(result.confirm?.match(/y/) !== null)